Minifier.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Template\Html;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. class Minifier implements MinifierInterface
  10. {
  11. /**
  12. * @var Filesystem
  13. */
  14. protected $filesystem;
  15. /**
  16. * All inline HTML tags
  17. *
  18. * @var array
  19. */
  20. protected $inlineHtmlTags = [
  21. 'b',
  22. 'big',
  23. 'i',
  24. 'small',
  25. 'tt',
  26. 'abbr',
  27. 'acronym',
  28. 'cite',
  29. 'code',
  30. 'dfn',
  31. 'em',
  32. 'kbd',
  33. 'strong',
  34. 'samp',
  35. 'var',
  36. 'a',
  37. 'bdo',
  38. 'br',
  39. 'img',
  40. 'map',
  41. 'object',
  42. 'q',
  43. 'span',
  44. 'sub',
  45. 'sup',
  46. 'button',
  47. 'input',
  48. 'label',
  49. 'select',
  50. 'textarea',
  51. '\?',
  52. ];
  53. /**
  54. * @var Filesystem\Directory\WriteInterface
  55. */
  56. protected $htmlDirectory;
  57. /**
  58. * @var Filesystem\Directory\ReadFactory
  59. */
  60. protected $readFactory;
  61. /**
  62. * @param Filesystem $filesystem
  63. * @param Filesystem\Directory\ReadFactory $readFactory
  64. */
  65. public function __construct(
  66. Filesystem $filesystem,
  67. Filesystem\Directory\ReadFactory $readFactory
  68. ) {
  69. $this->filesystem = $filesystem;
  70. $this->htmlDirectory = $filesystem->getDirectoryWrite(DirectoryList::TMP_MATERIALIZATION_DIR);
  71. $this->readFactory = $readFactory;
  72. }
  73. /**
  74. * Return path to minified template file, or minify if file not exist
  75. *
  76. * @param string $file
  77. * @return string
  78. */
  79. public function getMinified($file)
  80. {
  81. $file = $this->htmlDirectory->getDriver()->getRealPathSafety($file);
  82. if (!$this->htmlDirectory->isExist($this->getRelativeGeneratedPath($file))) {
  83. $this->minify($file);
  84. }
  85. return $this->getPathToMinified($file);
  86. }
  87. /**
  88. * Return path to minified template file
  89. *
  90. * @param string $file
  91. * @return string
  92. */
  93. public function getPathToMinified($file)
  94. {
  95. return $this->htmlDirectory->getAbsolutePath($this->getRelativeGeneratedPath($file));
  96. }
  97. /**
  98. * Minify template file
  99. *
  100. * @param string $file
  101. * @return void
  102. */
  103. public function minify($file)
  104. {
  105. $dir = dirname($file);
  106. $fileName = basename($file);
  107. $content = preg_replace(
  108. '#(?<!]]>)\s+</#',
  109. '</',
  110. preg_replace(
  111. '#((?:<\?php\s+(?!echo|print|if|elseif|else)[^\?]*)\?>)\s+#',
  112. '$1 ',
  113. preg_replace(
  114. '#(?<!' . implode('|', $this->inlineHtmlTags) . ')\> \<#',
  115. '><',
  116. preg_replace(
  117. '#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\b))*+)'
  118. . '(?:<(?>textarea|pre|script)\b|\z))#',
  119. ' ',
  120. preg_replace(
  121. '#(?<!:|\\\\|\'|")//(?!\s*\<\!\[)(?!\s*]]\>)[^\n\r]*#',
  122. '',
  123. preg_replace(
  124. '#(?<!:|\'|")//[^\n\r]*(\?\>)#',
  125. ' $1',
  126. preg_replace(
  127. '#(?<!:)//[^\n\r]*(\<\?php)[^\n\r]*(\s\?\>)[^\n\r]*#',
  128. '',
  129. $this->readFactory->create($dir)->readFile($fileName)
  130. )
  131. )
  132. )
  133. )
  134. )
  135. )
  136. );
  137. if (!$this->htmlDirectory->isExist()) {
  138. $this->htmlDirectory->create();
  139. }
  140. $this->htmlDirectory->writeFile($this->getRelativeGeneratedPath($file), rtrim($content));
  141. }
  142. /**
  143. * Gets the relative path of minified file to generation directory
  144. *
  145. * @param string $sourcePath
  146. * @return string
  147. */
  148. private function getRelativeGeneratedPath($sourcePath)
  149. {
  150. return $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getRelativePath($sourcePath);
  151. }
  152. }