Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RulePool.php
Go to the documentation of this file.
1 <?php
8 
12 
18 class RulePool
19 {
23  const TYPE_FILE = 'file';
24  const TYPE_LOCALE_FILE = 'locale';
25  const TYPE_TEMPLATE_FILE = 'template';
26  const TYPE_STATIC_FILE = 'static';
27  const TYPE_EMAIL_TEMPLATE = 'email';
31  protected $filesystem;
32 
38  private $rules = [];
39 
45  private $simpleFactory;
46 
52  private $themeFactory;
53 
59  private $modularSwitchFactory;
60 
66  private $moduleFactory;
67 
77  public function __construct(
78  \Magento\Framework\Filesystem $filesystem,
79  Rule\SimpleFactory $simpleFactory,
80  Rule\ThemeFactory $themeFactory,
81  Rule\ModuleFactory $moduleFactory,
82  Rule\ModularSwitchFactory $modularSwitchFactory
83  ) {
84  $this->filesystem = $filesystem;
85  $this->simpleFactory = $simpleFactory;
86  $this->themeFactory = $themeFactory;
87  $this->moduleFactory = $moduleFactory;
88  $this->modularSwitchFactory = $modularSwitchFactory;
89  }
90 
96  protected function createLocaleFileRule()
97  {
98  return $this->themeFactory->create(
99  ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>"])]
100  );
101  }
102 
108  protected function createTemplateFileRule()
109  {
110  return $this->modularSwitchFactory->create(
111  ['ruleNonModular' =>
112  $this->themeFactory->create(
113  ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>/templates"])]
114  ),
115  'ruleModular' => new Composite(
116  [
117  $this->themeFactory->create(
118  ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>/<module_name>/templates"])]
119  ),
120  $this->moduleFactory->create(
121  ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/<area>/templates"])]
122  ),
123  $this->moduleFactory->create(
124  ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/base/templates"])]
125  ),
126  ]
127  )]
128  );
129  }
130 
136  protected function createFileRule()
137  {
138  return $this->modularSwitchFactory->create(
139  ['ruleNonModular' => $this->themeFactory->create(
140  ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>"])]
141  ),
142  'ruleModular' => new Composite(
143  [
144  $this->themeFactory->create(
145  ['rule' => $this->simpleFactory->create(['pattern' => "<theme_dir>/<module_name>"])]
146  ),
147  $this->moduleFactory->create(
148  ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/<area>"])]
149  ),
150  $this->moduleFactory->create(
151  ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/base"])]
152  ),
153  ]
154  )]
155  );
156  }
157 
163  protected function createViewFileRule()
164  {
165  $libDir = rtrim($this->filesystem->getDirectoryRead(DirectoryList::LIB_WEB)->getAbsolutePath(), '/');
166  return $this->modularSwitchFactory->create(
167  ['ruleNonModular' => new Composite(
168  [
169  $this->themeFactory->create(
170  ['rule' =>
171  new Composite(
172  [
173  $this->simpleFactory
174  ->create([
175  'pattern' => "<theme_dir>/web/i18n/<locale>",
176  'optionalParams' => ['locale']
177  ]),
178  $this->simpleFactory
179  ->create(['pattern' => "<theme_dir>/web"]),
180  $this->simpleFactory
181  ->create([
182  'pattern' => "<theme_pubstatic_dir>",
183  'optionalParams' => ['theme_pubstatic_dir']
184  ]),
185  ]
186  )]
187  ),
188  $this->simpleFactory->create(['pattern' => $libDir]),
189  ]
190  ),
191  'ruleModular' => new Composite(
192  [
193  $this->themeFactory->create(
194  ['rule' =>
195  new Composite(
196  [
197  $this->simpleFactory->create(
198  [
199  'pattern' => "<theme_dir>/<module_name>/web/i18n/<locale>",
200  'optionalParams' => ['locale'],
201  ]
202  ),
203  $this->simpleFactory->create(
204  ['pattern' => "<theme_dir>/<module_name>/web"]
205  ),
206  ]
207  )]
208  ),
209  $this->moduleFactory->create(
210  ['rule' => $this->simpleFactory->create(
211  [
212  'pattern' => "<module_dir>/view/<area>/web/i18n/<locale>",
213  'optionalParams' => ['locale']
214  ]
215  )]
216  ),
217  $this->moduleFactory->create(
218  ['rule' => $this->simpleFactory->create(
219  [
220  'pattern' => "<module_dir>/view/base/web/i18n/<locale>",
221  'optionalParams' => ['locale']
222  ]
223  )]
224  ),
225  $this->moduleFactory->create(
226  ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/<area>/web"])]
227  ),
228  $this->moduleFactory->create(
229  ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/base/web"])]
230  ),
231  ]
232  )]
233  );
234  }
235 
243  protected function createEmailTemplateFileRule()
244  {
245  return new Composite(
246  [
247  $this->themeFactory->create(
248  ['rule' =>
249  $this->simpleFactory->create(
250  ['pattern' => "<theme_dir>/<module_name>/email"]
251  )]
252  ),
253  $this->moduleFactory->create(
254  ['rule' => $this->simpleFactory->create(['pattern' => "<module_dir>/view/<area>/email"])]
255  ),
256  ]
257  );
258  }
259 
267  public function getRule($type)
268  {
269  if (isset($this->rules[$type])) {
270  return $this->rules[$type];
271  }
272  switch ($type) {
273  case self::TYPE_FILE:
274  $rule = $this->createFileRule();
275  break;
277  $rule = $this->createLocaleFileRule();
278  break;
280  $rule = $this->createTemplateFileRule();
281  break;
283  $rule = $this->createViewFileRule();
284  break;
287  break;
288  default:
289  throw new \InvalidArgumentException("Fallback rule '$type' is not supported");
290  }
291  $this->rules[$type] = $rule;
292  return $this->rules[$type];
293  }
294 }
$type
Definition: item.phtml:13
__construct(\Magento\Framework\Filesystem $filesystem, Rule\SimpleFactory $simpleFactory, Rule\ThemeFactory $themeFactory, Rule\ModuleFactory $moduleFactory, Rule\ModularSwitchFactory $modularSwitchFactory)
Definition: RulePool.php:77