vendor/sonata-project/block-bundle/src/Block/BlockRenderer.php line 46

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;
  12. use Psr\Log\LoggerInterface;
  13. use Sonata\BlockBundle\Exception\Strategy\StrategyManagerInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. /**
  16.  * Handles the execution and rendering of a block.
  17.  */
  18. final class BlockRenderer implements BlockRendererInterface
  19. {
  20.     public function __construct(
  21.         private BlockServiceManagerInterface $blockServiceManager,
  22.         private StrategyManagerInterface $exceptionStrategyManager,
  23.         private ?LoggerInterface $logger null
  24.     ) {
  25.     }
  26.     public function render(BlockContextInterface $blockContext, ?Response $response null): Response
  27.     {
  28.         $block $blockContext->getBlock();
  29.         if (null !== $this->logger) {
  30.             $this->logger->info(
  31.                 sprintf('[cms::renderBlock] block.id=%d, block.type=%s'$block->getId() ?? ''$block->getType() ?? '')
  32.             );
  33.         }
  34.         try {
  35.             $service $this->blockServiceManager->get($block);
  36.             $service->load($block);
  37.             $response $service->execute($blockContext$response ?? new Response());
  38.         } catch (\Throwable $exception) {
  39.             if (null !== $this->logger) {
  40.                 $this->logger->error(sprintf(
  41.                     '[cms::renderBlock] block.id=%d - error while rendering block - %s',
  42.                     $block->getId() ?? '',
  43.                     $exception->getMessage()
  44.                 ), compact('exception'));
  45.             }
  46.             $response $this->exceptionStrategyManager->handleException($exception$blockContext->getBlock(), $response);
  47.         }
  48.         return $response;
  49.     }
  50. }