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