Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ThemeModular.php
Go to the documentation of this file.
1 <?php
7 
15 use Magento\Framework\View\File\Factory as FileFactory;
16 
21 {
27  private $pathPatternHelper;
28 
34  private $fileFactory;
35 
41  private $readDirFactory;
42 
48  private $componentRegistrar;
49 
55  private $subDir;
56 
66  public function __construct(
67  FileFactory $fileFactory,
68  ReadFactory $readDirFactory,
69  ComponentRegistrarInterface $componentRegistrar,
70  PathPattern $pathPatternHelper,
71  $subDir = ''
72  ) {
73  $this->pathPatternHelper = $pathPatternHelper;
74  $this->fileFactory = $fileFactory;
75  $this->readDirFactory = $readDirFactory;
76  $this->componentRegistrar = $componentRegistrar;
77  $this->subDir = $subDir ? $subDir . '/' : '';
78  }
79 
88  public function getFiles(ThemeInterface $theme, $filePath)
89  {
90  $namespace = $module = '*';
91  $themePath = $theme->getFullPath();
92  if (empty($themePath)) {
93  return [];
94  }
95  $themeAbsolutePath = $this->componentRegistrar->getPath(ComponentRegistrar::THEME, $themePath);
96  if (!$themeAbsolutePath) {
97  return [];
98  }
99  $themeDir = $this->readDirFactory->create($themeAbsolutePath);
100  $files = $themeDir->search("{$namespace}_{$module}/{$this->subDir}*/*/{$filePath}");
101  if (empty($files)) {
102  return [];
103  }
104 
105  $themes = [];
106  $currentTheme = $theme;
107  while ($currentTheme = $currentTheme->getParentTheme()) {
108  $themes[$currentTheme->getCode()] = $currentTheme;
109  }
110  $result = [];
111  $pattern = "#/(?<module>[^/]+)/{$this->subDir}(?<themeVendor>[^/]+)/(?<themeName>[^/]+)/"
112  . $this->pathPatternHelper->translatePatternFromGlob($filePath) . "$#i";
113  foreach ($files as $file) {
114  $filename = $themeDir->getAbsolutePath($file);
115  if (!preg_match($pattern, $filename, $matches)) {
116  continue;
117  }
118  $moduleFull = $matches['module'];
119  $ancestorThemeCode = $matches['themeVendor'] . '/' . $matches['themeName'];
120  if (!isset($themes[$ancestorThemeCode])) {
121  throw new LocalizedException(
122  new \Magento\Framework\Phrase(
123  "Trying to override modular view file '%1' for theme '%2', which is not ancestor of theme '%3'",
124  [$filename, $ancestorThemeCode, $theme->getCode()]
125  )
126  );
127  }
128  $result[] = $this->fileFactory->create($filename, $moduleFull, $themes[$ancestorThemeCode]);
129  }
130  return $result;
131  }
132 }
$componentRegistrar
Definition: bootstrap.php:23
$pattern
Definition: website.php:22
__construct(FileFactory $fileFactory, ReadFactory $readDirFactory, ComponentRegistrarInterface $componentRegistrar, PathPattern $pathPatternHelper, $subDir='')
foreach($appDirs as $dir) $files