Theme.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Design\Fallback\Rule;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. use Magento\Framework\Component\ComponentRegistrarInterface;
  9. use Magento\Framework\View\Design\ThemeInterface;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. use Magento\Framework\App\ObjectManager;
  12. /**
  13. * Fallback Rule Theme
  14. *
  15. * An aggregate of a fallback rule that propagates it to every theme according to a hierarchy
  16. */
  17. class Theme implements RuleInterface
  18. {
  19. /**
  20. * Rule
  21. *
  22. * @var RuleInterface
  23. */
  24. protected $rule;
  25. /**
  26. * Component registrar
  27. *
  28. * @var ComponentRegistrarInterface
  29. */
  30. private $componentRegistrar;
  31. /**
  32. * @var DirectoryList
  33. */
  34. private $directoryList;
  35. /**
  36. * Constructors
  37. *
  38. * @param RuleInterface $rule
  39. * @param ComponentRegistrarInterface $componentRegistrar
  40. */
  41. public function __construct(RuleInterface $rule, ComponentRegistrarInterface $componentRegistrar)
  42. {
  43. $this->rule = $rule;
  44. $this->componentRegistrar = $componentRegistrar;
  45. }
  46. /**
  47. * Propagate an underlying fallback rule to every theme in a hierarchy: parent, grandparent, etc.
  48. *
  49. * @param array $params
  50. * @return array
  51. * @throws \InvalidArgumentException
  52. */
  53. public function getPatternDirs(array $params)
  54. {
  55. if (!array_key_exists('theme', $params) || !$params['theme'] instanceof ThemeInterface) {
  56. throw new \InvalidArgumentException(
  57. 'Parameter "theme" should be specified and should implement the theme interface.'
  58. );
  59. }
  60. $result = [];
  61. /** @var $theme ThemeInterface */
  62. $theme = $params['theme'];
  63. unset($params['theme']);
  64. while ($theme) {
  65. if ($theme->getFullPath()) {
  66. $params['theme_dir'] = $this->componentRegistrar->getPath(
  67. ComponentRegistrar::THEME,
  68. $theme->getFullPath()
  69. );
  70. $params = $this->getThemePubStaticDir($theme, $params);
  71. $result = array_merge($result, $this->rule->getPatternDirs($params));
  72. }
  73. $theme = $theme->getParentTheme();
  74. }
  75. return $result;
  76. }
  77. /**
  78. * Get dir of Theme that contains published static view files
  79. *
  80. * @param ThemeInterface $theme
  81. * @param array $params
  82. * @return array
  83. */
  84. private function getThemePubStaticDir(ThemeInterface $theme, $params = [])
  85. {
  86. if (empty($params['theme_pubstatic_dir'])
  87. && isset($params['file'])
  88. && pathinfo($params['file'], PATHINFO_EXTENSION) === 'css'
  89. ) {
  90. $params['theme_pubstatic_dir'] = $this->getDirectoryList()
  91. ->getPath(DirectoryList::STATIC_VIEW)
  92. . '/' . $theme->getArea() . '/' . $theme->getCode()
  93. . (isset($params['locale']) ? '/' . $params['locale'] : '');
  94. }
  95. return $params;
  96. }
  97. /**
  98. * Get DirectoryList instance
  99. * @return DirectoryList
  100. *
  101. * @deprecated 101.0.0
  102. */
  103. private function getDirectoryList()
  104. {
  105. if (null === $this->directoryList) {
  106. $this->directoryList = ObjectManager::getInstance()->get(DirectoryList::class);
  107. }
  108. return $this->directoryList;
  109. }
  110. }