|
4 | 4 |
|
5 | 5 | namespace Netgen\Bundle\SiteBundle\Templating\Twig; |
6 | 6 |
|
| 7 | +use Symfony\Component\Filesystem\Filesystem; |
7 | 8 | use Twig\Source; |
| 9 | +use Twig\Template; |
8 | 10 |
|
9 | | -class DebugTemplate extends BaseDebugTemplate |
| 11 | +use function dirname; |
| 12 | +use function getcwd; |
| 13 | +use function implode; |
| 14 | +use function mb_stripos; |
| 15 | +use function mb_substr; |
| 16 | +use function mb_trim; |
| 17 | +use function preg_replace; |
| 18 | +use function str_contains; |
| 19 | +use function str_ends_with; |
| 20 | + |
| 21 | +/** |
| 22 | + * Meant to be used as a Twig base template class. |
| 23 | + * |
| 24 | + * Wraps the yield method to: |
| 25 | + * - Inject debug info into template to be able to see in the markup which one is used |
| 26 | + */ |
| 27 | +class DebugTemplate extends Template |
10 | 28 | { |
| 29 | + private Filesystem $fileSystem; |
| 30 | + |
| 31 | + public function yield(array $context, array $blocks = []): iterable |
| 32 | + { |
| 33 | + $this->fileSystem = $this->fileSystem ?? new Filesystem(); |
| 34 | + |
| 35 | + // Get parent result to be able to insert template name as HTML comments if applicable. |
| 36 | + // Layout template name will only appear at the end, to avoid potential quirks with old browsers |
| 37 | + // when comments appear before doctype declaration. |
| 38 | + $templateResult = implode('', [...parent::yield($context, $blocks)]); |
| 39 | + |
| 40 | + $templateName = mb_trim($this->fileSystem->makePathRelative($this->getSourceContext()->getPath(), dirname((string) getcwd())), '/'); |
| 41 | + $isHtmlTemplate = str_ends_with($templateName, 'html.twig'); |
| 42 | + $templateName = $isHtmlTemplate ? $templateName . ' (' . $this->getSourceContext()->getName() . ')' : $templateName; |
| 43 | + |
| 44 | + // Display start template comment, if applicable. |
| 45 | + if ($isHtmlTemplate) { |
| 46 | + if (str_contains(mb_trim($templateResult), '<!doctype')) { |
| 47 | + $templateResult = (string) preg_replace( |
| 48 | + '#(<!doctype[^>]+>)#im', |
| 49 | + "$1\n<!-- START " . $templateName . ' -->', |
| 50 | + $templateResult, |
| 51 | + ); |
| 52 | + } else { |
| 53 | + yield "\n<!-- START " . $templateName . " -->\n"; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Display stop template comment after result, if applicable. |
| 58 | + if ($isHtmlTemplate) { |
| 59 | + if (str_contains($templateResult, '</body>')) { |
| 60 | + $bodyPos = (int) mb_stripos($templateResult, '</body>'); |
| 61 | + |
| 62 | + // Add layout template name before </body>, to avoid display quirks in some browsers. |
| 63 | + yield mb_substr($templateResult, 0, $bodyPos) |
| 64 | + . "\n<!-- STOP " . $templateName . " -->\n" |
| 65 | + . mb_substr($templateResult, $bodyPos); |
| 66 | + } else { |
| 67 | + yield $templateResult; |
| 68 | + |
| 69 | + yield "\n<!-- STOP " . $templateName . " -->\n"; |
| 70 | + } |
| 71 | + } else { |
| 72 | + yield $templateResult; |
| 73 | + } |
| 74 | + } |
| 75 | + |
11 | 76 | public function getTemplateName(): string |
12 | 77 | { |
13 | 78 | return ''; |
|
0 commit comments