src/Twig/EnvironmentExtension.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use Pimcore;
  4. use Twig\Environment;
  5. use Twig\Loader\LoaderInterface;
  6. use voku\helper\HtmlMin;
  7. class EnvironmentExtension extends Environment
  8. {
  9.     private readonly bool $enabled;
  10.     public function __construct(
  11.         LoaderInterface $loader,
  12.         array           $options = [],
  13.         bool            $enabled false,
  14.     )
  15.     {
  16.         parent::__construct($loader$options);
  17.         $this->enabled $enabled && Pimcore\Tool::isFrontend();
  18.     }
  19.     public function render($name, array $context = []): string
  20.     {
  21.         $content $this->load($name)->render($context);
  22.         if (!$this->enabled) {
  23.             return $content;
  24.         }
  25.         $htmlMin = new HtmlMin();
  26.         $htmlMin->doOptimizeViaHtmlDomParser();               // optimize html via "HtmlDomParser()"
  27.         $htmlMin->doRemoveComments();                         // remove default HTML comments (depends on "doOptimizeViaHtmlDomParser(true)")
  28.         $htmlMin->doSumUpWhitespace();                        // sum-up extra whitespace from the Dom (depends on "doOptimizeViaHtmlDomParser(true)")
  29.         $htmlMin->doRemoveWhitespaceAroundTags();             // remove whitespace around tags (depends on "doOptimizeViaHtmlDomParser(true)")
  30.         $htmlMin->doOptimizeAttributes();                     // optimize html attributes (depends on "doOptimizeViaHtmlDomParser(true)")
  31.         $htmlMin->doRemoveHttpPrefixFromAttributes();         // remove optional "http:"-prefix from attributes (depends on "doOptimizeAttributes(true)")
  32.         $htmlMin->doRemoveHttpsPrefixFromAttributes();        // remove optional "https:"-prefix from attributes (depends on "doOptimizeAttributes(true)")
  33.         $htmlMin->doKeepHttpAndHttpsPrefixOnExternalAttributes(); // keep "http:"- and "https:"-prefix for all external links
  34.         $htmlMin->doRemoveDefaultAttributes();                // remove defaults (depends on "doOptimizeAttributes(true)" | disabled by default)
  35.         $htmlMin->doRemoveDeprecatedAnchorName();             // remove deprecated anchor-jump (depends on "doOptimizeAttributes(true)")
  36.         $htmlMin->doRemoveDeprecatedScriptCharsetAttribute(); // remove deprecated charset-attribute - the browser will use the charset from the HTTP-Header, anyway (depends on "doOptimizeAttributes(true)")
  37.         $htmlMin->doRemoveDeprecatedTypeFromScriptTag();      // remove deprecated script-mime-types (depends on "doOptimizeAttributes(true)")
  38.         $htmlMin->doRemoveDeprecatedTypeFromStylesheetLink(); // remove "type=text/css" for css links (depends on "doOptimizeAttributes(true)")
  39.         $htmlMin->doRemoveDeprecatedTypeFromStyleAndLinkTag(); // remove "type=text/css" from all links and styles
  40.         $htmlMin->doRemoveDefaultMediaTypeFromStyleAndLinkTag(); // remove "media="all" from all links and styles
  41.         $htmlMin->doRemoveDefaultTypeFromButton();            // remove type="submit" from button tags
  42.         $htmlMin->doRemoveEmptyAttributes();                  // remove some empty attributes (depends on "doOptimizeAttributes(true)")
  43.         $htmlMin->doRemoveValueFromEmptyInput();              // remove 'value=""' from empty <input> (depends on "doOptimizeAttributes(true)")
  44.         $htmlMin->doSortCssClassNames();                      // sort css-class-names, for better gzip results (depends on "doOptimizeAttributes(true)")
  45.         $htmlMin->doSortHtmlAttributes();                     // sort html-attributes, for better gzip results (depends on "doOptimizeAttributes(true)")
  46.         $htmlMin->doRemoveSpacesBetweenTags();                // remove more (aggressive) spaces in the dom (disabled by default)
  47.         $content $htmlMin->minify($content);
  48.         $bodyPosition stripos($contentneedle'<body>');
  49.         if ($bodyPosition !== false) {
  50.             $content substr_replace($content'</head><body>'$bodyPosition6);
  51.             $content .= '</body></html>';
  52.         }
  53.         return $content;
  54.     }
  55. }