vendor/sonata-project/block-bundle/src/Block/Service/AbstractBlockService.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\BlockBundle\Block\Service;
  12. use Sonata\BlockBundle\Block\BlockContextInterface;
  13. use Sonata\BlockBundle\Model\BlockInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use Twig\Environment;
  17. /**
  18.  * @author Sullivan Senechal <soullivaneuh@gmail.com>
  19.  */
  20. abstract class AbstractBlockService implements BlockServiceInterface
  21. {
  22.     public function __construct(private Environment $twig)
  23.     {
  24.     }
  25.     /**
  26.      * Returns a Response object than can be cacheable.
  27.      *
  28.      * @param array<string, mixed> $parameters
  29.      */
  30.     public function renderResponse(string $view, array $parameters = [], ?Response $response null): Response
  31.     {
  32.         $response ??= new Response();
  33.         $response->setContent($this->twig->render($view$parameters));
  34.         return $response;
  35.     }
  36.     /**
  37.      * Returns a Response object that cannot be cacheable, this must be used if the Response is related to the user.
  38.      * A good solution to make the page cacheable is to configure the block to be cached with javascript ...
  39.      *
  40.      * @deprecated since sonata-project/block-bundle 4.12 and will be removed in 5.0.
  41.      *
  42.      * NEXT_MAJOR: remove
  43.      *
  44.      * @param array<string, mixed> $parameters
  45.      */
  46.     public function renderPrivateResponse(string $view, array $parameters = [], ?Response $response null): Response
  47.     {
  48.         @trigger_error(
  49.             sprintf(
  50.                 'Method "%s" is deprecated since sonata-project/block-bundle 4.12 and will be removed in 5.0.',
  51.                 __METHOD__
  52.             ),
  53.             \E_USER_DEPRECATED
  54.         );
  55.         return $this->renderResponse($view$parameters$response)
  56.             ->setTtl(0)
  57.             ->setPrivate();
  58.     }
  59.     /**
  60.      * Define the default options for the block.
  61.      */
  62.     public function configureSettings(OptionsResolver $resolver): void
  63.     {
  64.     }
  65.     public function getCacheKeys(BlockInterface $block): array
  66.     {
  67.         $updatedAt $block->getUpdatedAt();
  68.         return [
  69.             'block_id' => $block->getId(),
  70.             'updated_at' => null !== $updatedAt $updatedAt->format('U') : null,
  71.         ];
  72.     }
  73.     public function load(BlockInterface $block): void
  74.     {
  75.     }
  76.     public function execute(BlockContextInterface $blockContext, ?Response $response null): Response
  77.     {
  78.         $template $blockContext->getTemplate();
  79.         \assert(null !== $template);
  80.         return $this->renderResponse($template, [
  81.             'block_context' => $blockContext,
  82.             'block' => $blockContext->getBlock(),
  83.         ], $response);
  84.     }
  85.     public function getTwig(): Environment
  86.     {
  87.         return $this->twig;
  88.     }
  89. }