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

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 Symfony\Component\OptionsResolver\OptionsResolverInterface;
  17. use Symfony\Component\Templating\EngineInterface;
  18. use Twig\Environment;
  19. /**
  20.  * @author Sullivan Senechal <soullivaneuh@gmail.com>
  21.  */
  22. abstract class AbstractBlockService implements BlockServiceInterface
  23. {
  24.     /**
  25.      * @var string|null
  26.      */
  27.     protected $name;
  28.     /**
  29.      * NEXT_MAJOR: Remove this property.
  30.      *
  31.      * @var EngineInterface|null
  32.      */
  33.     protected $templating;
  34.     /**
  35.      * @var Environment
  36.      */
  37.     private $twig;
  38.     /**
  39.      * NEXT_MAJOR: Make `$twig` argument mandatory and remove other arguments.
  40.      *
  41.      * @param Environment|EngineInterface|string $templatingOrDeprecatedName
  42.      */
  43.     public function __construct($templatingOrDeprecatedName null, ?EngineInterface $templating null)
  44.     {
  45.         // $this->twig = $twig;
  46.         // NEXT_MAJOR: Uncomment the previous assignment and remove the following lines in this method.
  47.         if ($templatingOrDeprecatedName instanceof Environment) {
  48.             $this->name '';
  49.             $this->twig $templatingOrDeprecatedName;
  50.         } else {
  51.             if (!== strpos(static::class, __NAMESPACE__.'\\')) {
  52.                 $class 'c' === static::class[0] && === strpos(static::class, "class@anonymous\0") ? get_parent_class(static::class).'@anonymous' : static::class;
  53.                 @trigger_error(
  54.                     sprintf(
  55.                         'Passing %s as argument 1 to %s::%s() is deprecated since sonata-project/block-bundle 3.16 and will throw a \TypeError as of 4.0. You must pass an instance of %s instead.',
  56.                         \is_object($templatingOrDeprecatedName) ? 'instance of '.\get_class($templatingOrDeprecatedName) : \gettype($templatingOrDeprecatedName),
  57.                         $class,
  58.                         __FUNCTION__,
  59.                         Environment::class
  60.                     ),
  61.                     E_USER_DEPRECATED
  62.                 );
  63.             }
  64.             if ($templatingOrDeprecatedName instanceof EngineInterface) {
  65.                 $this->name '';
  66.                 $this->templating $templatingOrDeprecatedName;
  67.             } elseif (\is_string($templatingOrDeprecatedName)) {
  68.                 $this->name $templatingOrDeprecatedName;
  69.                 $this->templating $templating;
  70.             } else {
  71.                 $class 'c' === static::class[0] && === strpos(static::class, "class@anonymous\0") ? get_parent_class(static::class).'@anonymous' : static::class;
  72.                 throw new \TypeError(sprintf(
  73.                     'Argument 1 passed to %s::%s() must be a string or an instance of %s or %s, %s given.',
  74.                     $class,
  75.                     __FUNCTION__,
  76.                     Environment::class,
  77.                     EngineInterface::class,
  78.                     \is_object($templatingOrDeprecatedName) ? 'instance of '.\get_class($templatingOrDeprecatedName) : \gettype($templatingOrDeprecatedName)
  79.                 ));
  80.             }
  81.         }
  82.     }
  83.     /**
  84.      * Returns a Response object than can be cacheable.
  85.      *
  86.      * @param string $view
  87.      *
  88.      * @return Response
  89.      */
  90.     public function renderResponse($view, array $parameters = [], Response $response null)
  91.     {
  92.         if (null === $this->twig) {
  93.             return $this->getTemplating()->renderResponse($view$parameters$response);
  94.         }
  95.         // NEXT_MAJOR: Remove the previous condition
  96.         if (null === $response) {
  97.             $response = new Response();
  98.         }
  99.         $response->setContent($this->twig->render($view$parameters));
  100.         return $response;
  101.     }
  102.     /**
  103.      * Returns a Response object that cannot be cacheable, this must be used if the Response is related to the user.
  104.      * A good solution to make the page cacheable is to configure the block to be cached with javascript ...
  105.      *
  106.      * @param string $view
  107.      *
  108.      * @return Response
  109.      */
  110.     public function renderPrivateResponse($view, array $parameters = [], Response $response null)
  111.     {
  112.         return $this->renderResponse($view$parameters$response)
  113.             ->setTtl(0)
  114.             ->setPrivate()
  115.         ;
  116.     }
  117.     public function setDefaultSettings(OptionsResolverInterface $resolver)
  118.     {
  119.         if (!$resolver instanceof OptionsResolver) {
  120.             throw new \BadMethodCallException(
  121.                 sprintf('Calling %s with %s is unsupported'__METHOD__, \get_class($resolver))
  122.             );
  123.         }
  124.         $this->configureSettings($resolver);
  125.     }
  126.     /**
  127.      * Define the default options for the block.
  128.      */
  129.     public function configureSettings(OptionsResolver $resolver)
  130.     {
  131.     }
  132.     public function getCacheKeys(BlockInterface $block)
  133.     {
  134.         return [
  135.             'block_id' => $block->getId(),
  136.             'updated_at' => $block->getUpdatedAt() ? $block->getUpdatedAt()->format('U') : strtotime('now'),
  137.         ];
  138.     }
  139.     public function load(BlockInterface $block)
  140.     {
  141.     }
  142.     public function getJavascripts($media)
  143.     {
  144.         return [];
  145.     }
  146.     public function getStylesheets($media)
  147.     {
  148.         return [];
  149.     }
  150.     public function execute(BlockContextInterface $blockContextResponse $response null)
  151.     {
  152.         return $this->renderResponse($blockContext->getTemplate(), [
  153.             'block_context' => $blockContext,
  154.             'block' => $blockContext->getBlock(),
  155.         ], $response);
  156.     }
  157.     public function getName()
  158.     {
  159.         return $this->name;
  160.     }
  161.     /**
  162.      * NEXT_MAJOR: Remove this method.
  163.      *
  164.      * @deprecated since sonata-project/block-bundle 3.17
  165.      */
  166.     public function getTemplating()
  167.     {
  168.         $class 'c' === static::class[0] && === strpos(static::class, "class@anonymous\0") ? get_parent_class(static::class).'@anonymous' : static::class;
  169.         @trigger_error(
  170.             sprintf(
  171.                 'Method %s::%s() is deprecated since sonata-project/block-bundle 3.17 and will be removed as of version 4.0.',
  172.                 $class,
  173.                 __FUNCTION__
  174.             ),
  175.             E_USER_DEPRECATED
  176.         );
  177.         if (null !== $this->twig) {
  178.             throw new \BadMethodCallException(sprintf(
  179.                 'Calling %1$s::%2$s() is not allowed when an instance of %3$s is passed as argument 1 to %1$s::__construct().',
  180.                 $class,
  181.                 __FUNCTION__,
  182.                 Environment::class
  183.             ));
  184.         }
  185.         return $this->templating;
  186.     }
  187. }