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

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.     public function __construct(
  20.         private TemplateEventRendererInterface $templateEventRenderer,
  21.         private TemplateBlockRegistryInterface $templateBlockRegistry,
  22.     ) {
  23.     }
  24.     public function render(array $eventNames, array $context = []): string
  25.     {
  26.         $shouldRenderHtmlDebug $this->shouldRenderHtmlDebug($this->templateBlockRegistry->findEnabledForEvents($eventNames));
  27.         $renderedParts = [];
  28.         if ($shouldRenderHtmlDebug) {
  29.             $renderedParts[] = sprintf(
  30.                 '<!-- BEGIN EVENT | event name: "%s" -->',
  31.                 implode(', '$eventNames),
  32.             );
  33.         }
  34.         $renderedParts[] = $this->templateEventRenderer->render($eventNames$context);
  35.         if ($shouldRenderHtmlDebug) {
  36.             $renderedParts[] = sprintf(
  37.                 '<!-- END EVENT | event name: "%s" -->',
  38.                 implode(', '$eventNames),
  39.             );
  40.         }
  41.         return implode("\n"$renderedParts);
  42.     }
  43.     /**
  44.      * @param TemplateBlock[] $templateBlocks
  45.      */
  46.     private function shouldRenderHtmlDebug(array $templateBlocks): bool
  47.     {
  48.         return count($templateBlocks) === || count(array_filter($templateBlocks, static fn (TemplateBlock $templateBlock): bool => strrpos($templateBlock->getTemplate(), '.html.twig') !== false)) >= 1;
  49.     }
  50. }