RulePool.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\View\Design\Fallback\Rule\Composite;
  9. use Magento\Framework\View\Design\Fallback\Rule\RuleInterface;
  10. /**
  11. * Fallback Factory
  12. *
  13. * Factory that produces all sorts of fallback rules
  14. */
  15. class RulePool
  16. {
  17. /**#@+
  18. * Supported types of fallback rules
  19. */
  20. const TYPE_FILE = 'file';
  21. const TYPE_LOCALE_FILE = 'locale';
  22. const TYPE_TEMPLATE_FILE = 'template';
  23. const TYPE_STATIC_FILE = 'static';
  24. const TYPE_EMAIL_TEMPLATE = 'email';
  25. /**#@-*/
  26. /**#@-*/
  27. protected $filesystem;
  28. /**
  29. * Rules
  30. *
  31. * @var array
  32. */
  33. private $rules = [];
  34. /**
  35. * Factory for simple rule
  36. *
  37. * @var \Magento\Framework\View\Design\Fallback\Rule\SimpleFactory
  38. */
  39. private $simpleFactory;
  40. /**
  41. * Factory for theme rule
  42. *
  43. * @var Rule\ThemeFactory
  44. */
  45. private $themeFactory;
  46. /**
  47. * Factory for modular switcher
  48. *
  49. * @var Rule\ModularSwitchFactory
  50. */
  51. private $modularSwitchFactory;
  52. /**
  53. * Factory for module rule
  54. *
  55. * @var Rule\ModuleFactory
  56. */
  57. private $moduleFactory;
  58. /**
  59. * Constructor
  60. *
  61. * @param \Magento\Framework\Filesystem $filesystem
  62. * @param Rule\SimpleFactory $simpleFactory
  63. * @param Rule\ThemeFactory $themeFactory
  64. * @param Rule\ModuleFactory $moduleFactory
  65. * @param Rule\ModularSwitchFactory $modularSwitchFactory
  66. */
  67. public function __construct(
  68. \Magento\Framework\Filesystem $filesystem,
  69. Rule\SimpleFactory $simpleFactory,
  70. Rule\ThemeFactory $themeFactory,
  71. Rule\ModuleFactory $moduleFactory,
  72. Rule\ModularSwitchFactory $modularSwitchFactory
  73. ) {
  74. $this->filesystem = $filesystem;
  75. $this->simpleFactory = $simpleFactory;
  76. $this->themeFactory = $themeFactory;
  77. $this->moduleFactory = $moduleFactory;
  78. $this->modularSwitchFactory = $modularSwitchFactory;
  79. }
  80. /**
  81. * Retrieve newly created fallback rule for locale files, such as CSV translation maps
  82. *
  83. * @return RuleInterface
  84. */
  85. protected function createLocaleFileRule()
  86. {
  87. return $this->themeFactory->create(
  88. ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>"])]
  89. );
  90. }
  91. /**
  92. * Retrieve newly created fallback rule for template files
  93. *
  94. * @return RuleInterface
  95. */
  96. protected function createTemplateFileRule()
  97. {
  98. return $this->modularSwitchFactory->create(
  99. ['ruleNonModular' =>
  100. $this->themeFactory->create(
  101. ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>/templates"])]
  102. ),
  103. 'ruleModular' => new Composite(
  104. [
  105. $this->themeFactory->create(
  106. ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>/<module_name>/templates"])]
  107. ),
  108. $this->moduleFactory->create(
  109. ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/<area>/templates"])]
  110. ),
  111. $this->moduleFactory->create(
  112. ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/base/templates"])]
  113. ),
  114. ]
  115. )]
  116. );
  117. }
  118. /**
  119. * Retrieve newly created fallback rule for dynamic view files
  120. *
  121. * @return RuleInterface
  122. */
  123. protected function createFileRule()
  124. {
  125. return $this->modularSwitchFactory->create(
  126. ['ruleNonModular' => $this->themeFactory->create(
  127. ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>"])]
  128. ),
  129. 'ruleModular' => new Composite(
  130. [
  131. $this->themeFactory->create(
  132. ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>/<module_name>"])]
  133. ),
  134. $this->moduleFactory->create(
  135. ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/<area>"])]
  136. ),
  137. $this->moduleFactory->create(
  138. ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/base"])]
  139. ),
  140. ]
  141. )]
  142. );
  143. }
  144. /**
  145. * Retrieve newly created fallback rule for static view files, such as CSS, JavaScript, images, etc.
  146. *
  147. * @return RuleInterface
  148. */
  149. protected function createViewFileRule()
  150. {
  151. $libDir = rtrim($this->filesystem->getDirectoryRead(DirectoryList::LIB_WEB)->getAbsolutePath(), '/');
  152. return $this->modularSwitchFactory->create(
  153. ['ruleNonModular' => new Composite(
  154. [
  155. $this->themeFactory->create(
  156. ['rule' =>
  157. new Composite(
  158. [
  159. $this->simpleFactory
  160. ->create([
  161. 'pattern' => "<theme_dir>/web/i18n/<locale>",
  162. 'optionalParams' => ['locale']
  163. ]),
  164. $this->simpleFactory
  165. ->create(['pattern' => "<theme_dir>/web"]),
  166. $this->simpleFactory
  167. ->create([
  168. 'pattern' => "<theme_pubstatic_dir>",
  169. 'optionalParams' => ['theme_pubstatic_dir']
  170. ]),
  171. ]
  172. )]
  173. ),
  174. $this->simpleFactory->create(['pattern' => $libDir]),
  175. ]
  176. ),
  177. 'ruleModular' => new Composite(
  178. [
  179. $this->themeFactory->create(
  180. ['rule' =>
  181. new Composite(
  182. [
  183. $this->simpleFactory->create(
  184. [
  185. 'pattern' => "<theme_dir>/<module_name>/web/i18n/<locale>",
  186. 'optionalParams' => ['locale'],
  187. ]
  188. ),
  189. $this->simpleFactory->create(
  190. ['pattern' => "<theme_dir>/<module_name>/web"]
  191. ),
  192. ]
  193. )]
  194. ),
  195. $this->moduleFactory->create(
  196. ['rule' => $this->simpleFactory->create(
  197. [
  198. 'pattern' => "<module_dir>/view/<area>/web/i18n/<locale>",
  199. 'optionalParams' => ['locale']
  200. ]
  201. )]
  202. ),
  203. $this->moduleFactory->create(
  204. ['rule' => $this->simpleFactory->create(
  205. [
  206. 'pattern' => "<module_dir>/view/base/web/i18n/<locale>",
  207. 'optionalParams' => ['locale']
  208. ]
  209. )]
  210. ),
  211. $this->moduleFactory->create(
  212. ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/<area>/web"])]
  213. ),
  214. $this->moduleFactory->create(
  215. ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/base/web"])]
  216. ),
  217. ]
  218. )]
  219. );
  220. }
  221. /**
  222. * Retrieve newly created fallback rule for email templates.
  223. *
  224. * Emails are only loaded in a modular context, so a non-modular rule is not specified.
  225. *
  226. * @return RuleInterface
  227. */
  228. protected function createEmailTemplateFileRule()
  229. {
  230. return new Composite(
  231. [
  232. $this->themeFactory->create(
  233. ['rule' =>
  234. $this->simpleFactory->create(
  235. ['pattern' => "<theme_dir>/<module_name>/email"]
  236. )]
  237. ),
  238. $this->moduleFactory->create(
  239. ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/<area>/email"])]
  240. ),
  241. ]
  242. );
  243. }
  244. /**
  245. * Get rule by type
  246. *
  247. * @param string $type
  248. * @return RuleInterface
  249. * @throws \InvalidArgumentException
  250. */
  251. public function getRule($type)
  252. {
  253. if (isset($this->rules[$type])) {
  254. return $this->rules[$type];
  255. }
  256. switch ($type) {
  257. case self::TYPE_FILE:
  258. $rule = $this->createFileRule();
  259. break;
  260. case self::TYPE_LOCALE_FILE:
  261. $rule = $this->createLocaleFileRule();
  262. break;
  263. case self::TYPE_TEMPLATE_FILE:
  264. $rule = $this->createTemplateFileRule();
  265. break;
  266. case self::TYPE_STATIC_FILE:
  267. $rule = $this->createViewFileRule();
  268. break;
  269. case self::TYPE_EMAIL_TEMPLATE:
  270. $rule = $this->createEmailTemplateFileRule();
  271. break;
  272. default:
  273. throw new \InvalidArgumentException("Fallback rule '$type' is not supported");
  274. }
  275. $this->rules[$type] = $rule;
  276. return $this->rules[$type];
  277. }
  278. }