vendor/sylius/sylius/src/Sylius/Bundle/UiBundle/Renderer/HtmlDebugTemplateEventRenderer.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\UiBundle\Renderer;
  12. use Sylius\Bundle\UiBundle\Registry\TemplateBlock;
  13. use Sylius\Bundle\UiBundle\Registry\TemplateBlockRegistryInterface;
  14. /**
  15.  * @experimental
  16.  */
  17. final class HtmlDebugTemplateEventRenderer implements TemplateEventRendererInterface
  18. {
  19.     /** @var TemplateEventRendererInterface */
  20.     private $templateEventRenderer;
  21.     /** @var TemplateBlockRegistryInterface */
  22.     private $templateBlockRegistry;
  23.     public function __construct(TemplateEventRendererInterface $templateEventRendererTemplateBlockRegistryInterface $templateBlockRegistry)
  24.     {
  25.         $this->templateEventRenderer $templateEventRenderer;
  26.         $this->templateBlockRegistry $templateBlockRegistry;
  27.     }
  28.     public function render(array $eventNames, array $context = []): string
  29.     {
  30.         $shouldRenderHtmlDebug $this->shouldRenderHtmlDebug($this->templateBlockRegistry->findEnabledForEvents($eventNames));
  31.         $renderedParts = [];
  32.         if ($shouldRenderHtmlDebug) {
  33.             $renderedParts[] = sprintf(
  34.                 '<!-- BEGIN EVENT | event name: "%s" -->',
  35.                 implode(', '$eventNames)
  36.             );
  37.         }
  38.         $renderedParts[] = $this->templateEventRenderer->render($eventNames$context);
  39.         if ($shouldRenderHtmlDebug) {
  40.             $renderedParts[] = sprintf(
  41.                 '<!-- END EVENT | event name: "%s" -->',
  42.                 implode(', '$eventNames)
  43.             );
  44.         }
  45.         return implode("\n"$renderedParts);
  46.     }
  47.     /**
  48.      * @param TemplateBlock[] $templateBlocks
  49.      */
  50.     private function shouldRenderHtmlDebug(array $templateBlocks): bool
  51.     {
  52.         return count($templateBlocks) === || count(array_filter($templateBlocks, static function (TemplateBlock $templateBlock): bool {
  53.             return strrpos($templateBlock->getTemplate(), '.html.twig') !== false;
  54.         })) >= 1;
  55.     }
  56. }