TemplateFile.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Design\FileResolution\Fallback;
  7. use Magento\Framework\App\State;
  8. use Magento\Framework\View\Asset\ConfigInterface;
  9. use Magento\Framework\View\Design\ThemeInterface;
  10. use Magento\Framework\View\Template\Html\MinifierInterface;
  11. use Magento\Framework\App\DeploymentConfig;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Framework\Config\ConfigOptionsListConstants as Constants;
  14. /**
  15. * Provider of template view files
  16. */
  17. class TemplateFile extends File
  18. {
  19. /**
  20. * @var State
  21. */
  22. protected $appState;
  23. /**
  24. * @var MinifierInterface
  25. */
  26. protected $templateMinifier;
  27. /**
  28. * @var ConfigInterface
  29. */
  30. protected $assetConfig;
  31. /**
  32. * @var DeploymentConfig
  33. */
  34. private $deploymentConfig;
  35. /**
  36. * @param ResolverInterface $resolver
  37. * @param MinifierInterface $templateMinifier
  38. * @param State $appState
  39. * @param ConfigInterface $assetConfig
  40. * @param DeploymentConfig $deploymentConfig
  41. */
  42. public function __construct(
  43. ResolverInterface $resolver,
  44. MinifierInterface $templateMinifier,
  45. State $appState,
  46. ConfigInterface $assetConfig,
  47. DeploymentConfig $deploymentConfig = null
  48. ) {
  49. $this->appState = $appState;
  50. $this->templateMinifier = $templateMinifier;
  51. $this->assetConfig = $assetConfig;
  52. $this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
  53. parent::__construct($resolver);
  54. }
  55. /**
  56. * @return string
  57. */
  58. protected function getFallbackType()
  59. {
  60. return \Magento\Framework\View\Design\Fallback\RulePool::TYPE_TEMPLATE_FILE;
  61. }
  62. /**
  63. * Get existing file name, using fallback mechanism
  64. *
  65. * @param string $area
  66. * @param ThemeInterface $themeModel
  67. * @param string $file
  68. * @param string|null $module
  69. * @return string|bool
  70. */
  71. public function getFile($area, ThemeInterface $themeModel, $file, $module = null)
  72. {
  73. $template = parent::getFile($area, $themeModel, $file, $module);
  74. if ($template && $this->assetConfig->isMinifyHtml()) {
  75. switch ($this->appState->getMode()) {
  76. case State::MODE_PRODUCTION:
  77. return $this->getMinifiedTemplateInProduction($template);
  78. case State::MODE_DEFAULT:
  79. return $this->templateMinifier->getMinified($template);
  80. case State::MODE_DEVELOPER:
  81. default:
  82. return $template;
  83. }
  84. }
  85. return $template;
  86. }
  87. /**
  88. * Returns path to minified template file
  89. *
  90. * If SCD on demand in production is disabled - returns the path to minified template file.
  91. * Otherwise returns the path to minified template file,
  92. * or minify if file not exist and returns path.
  93. *
  94. * @param string $template
  95. * @return string
  96. */
  97. private function getMinifiedTemplateInProduction($template)
  98. {
  99. $forceMinification = $this->deploymentConfig->getConfigData(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
  100. || $this->deploymentConfig->getConfigData(Constants::CONFIG_PATH_FORCE_HTML_MINIFICATION);
  101. return $forceMinification ?
  102. $this->templateMinifier->getMinified($template)
  103. : $this->templateMinifier->getPathToMinified($template);
  104. }
  105. }