vendor/ezsystems/ezpublish-kernel/eZ/Bundle/EzPublishDebugBundle/Twig/DebugTemplate.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Bundle\EzPublishDebugBundle\Twig;
  7. use Symfony\Component\Filesystem\Filesystem;
  8. use Twig_Template;
  9. /**
  10.  * Meant to be used as a Twig base template class.
  11.  *
  12.  * Wraps the display method to:
  13.  * - Inject debug info into template to be able to see in the markup which one is used
  14.  */
  15. class DebugTemplate extends Twig_Template
  16. {
  17.     private $fileSystem;
  18.     public function display(array $context, array $blocks = [])
  19.     {
  20.         $this->fileSystem $this->fileSystem ?: new Filesystem();
  21.         // Bufferize to be able to insert template name as HTML comments if applicable.
  22.         // Layout template name will only appear at the end, to avoid potential quirks with old browsers
  23.         // when comments appear before doctype declaration.
  24.         ob_start();
  25.         parent::display($context$blocks);
  26.         $templateResult ob_get_clean();
  27.         $templateName trim($this->fileSystem->makePathRelative($this->getSourceContext()->getPath(), dirname(getcwd())), '/');
  28.         // Check if template name ends with "html.twig", indicating this is an HTML template.
  29.         $isHtmlTemplate substr($templateName, -strlen('html.twig')) === 'html.twig';
  30.         $templateName $isHtmlTemplate $templateName ' (' $this->getSourceContext()->getName() . ')' $templateName;
  31.         // Display start template comment, if applicable.
  32.         if ($isHtmlTemplate) {
  33.             if (stripos(trim($templateResult), '<!doctype') !== false) {
  34.                 $templateResult preg_replace(
  35.                     '#(<!doctype[^>]+>)#im',
  36.                     "$1\n<!-- START " $templateName ' -->',
  37.                     $templateResult
  38.                 );
  39.             } else {
  40.                 echo "\n<!-- START $templateName -->\n";
  41.             }
  42.         }
  43.         // Display stop template comment after result, if applicable.
  44.         if ($isHtmlTemplate) {
  45.             $bodyPos stripos($templateResult'</body>');
  46.             if ($bodyPos !== false) {
  47.                 // Add layout template name before </body>, to avoid display quirks in some browsers.
  48.                 echo substr($templateResult0$bodyPos)
  49.                      . "\n<!-- STOP $templateName -->\n"
  50.                      substr($templateResult$bodyPos);
  51.             } else {
  52.                 echo $templateResult;
  53.                 echo "\n<!-- STOP $templateName -->\n";
  54.             }
  55.         } else {
  56.             echo $templateResult;
  57.         }
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function getTemplateName()
  63.     {
  64.         return '';
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     public function getSource()
  70.     {
  71.         return '';
  72.     }
  73.     /**
  74.      * {@inheritdoc}
  75.      */
  76.     protected function doDisplay(array $context, array $blocks = [])
  77.     {
  78.         return '';
  79.     }
  80.     /**
  81.      * {@inheritdoc}
  82.      */
  83.     public function getDebugInfo()
  84.     {
  85.         return [];
  86.     }
  87. }