Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Source.php
Go to the documentation of this file.
1 <?php
8 
15 
21 class Source
22 {
26  private $filesystem;
27 
31  protected $rootDir;
32 
36  protected $tmpDir;
37 
41  private $preProcessorPool;
42 
46  protected $fallback;
47 
52  private $themeList;
53 
57  private $chainFactory;
58 
62  private $readFactory;
63 
67  private $themeProvider;
68 
79  public function __construct(
80  \Magento\Framework\Filesystem $filesystem,
81  ReadFactory $readFactory,
82  PreProcessor\Pool $preProcessorPool,
83  \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile $fallback,
84  \Magento\Framework\View\Design\Theme\ListInterface $themeList,
85  ChainFactoryInterface $chainFactory
86  ) {
87  $this->filesystem = $filesystem;
88  $this->readFactory = $readFactory;
89  $this->rootDir = $filesystem->getDirectoryRead(DirectoryList::ROOT);
90  $this->tmpDir = $filesystem->getDirectoryWrite(DirectoryList::TMP_MATERIALIZATION_DIR);
91  $this->preProcessorPool = $preProcessorPool;
92  $this->fallback = $fallback;
93  $this->themeList = $themeList;
94  $this->chainFactory = $chainFactory;
95  }
96 
103  public function getFile(LocalInterface $asset)
104  {
105  $result = $this->preProcess($asset);
106  if (!$result) {
107  return false;
108  }
109  list($dir, $path) = $result;
110  return $this->readFactory->create($dir)->getAbsolutePath($path);
111  }
112 
119  public function getContent(LocalInterface $asset)
120  {
121  $result = $this->preProcess($asset);
122  if (!$result) {
123  return false;
124  }
125  list($dir, $path) = $result;
126  return $this->readFactory->create($dir)->readFile($path);
127  }
128 
141  private function preProcess(LocalInterface $asset)
142  {
143  $sourceFile = $this->findSourceFile($asset);
144  $dir = $this->rootDir->getAbsolutePath();
145  $path = '';
146  if ($sourceFile) {
147  $path = basename($sourceFile);
148  $dir = dirname($sourceFile);
149  }
150 
151  $chain = $this->createChain($asset, $dir, $path);
152  $this->preProcessorPool->process($chain);
153  $chain->assertValid();
154  if ($chain->isChanged()) {
155  $dir = $this->tmpDir->getAbsolutePath();
156  $path = $chain->getTargetAssetPath();
157  $this->tmpDir->writeFile($path, $chain->getContent());
158  }
159  if (empty($path)) {
160  $result = false;
161  } else {
162  $result = [$dir, $path, $chain->getContentType()];
163  }
164  return $result;
165  }
166 
171  public function getSourceContentType(LocalInterface $asset)
172  {
173  list(,,$type) = $this->preProcess($asset);
174  return $type;
175  }
176 
181  public function findSource(LocalInterface $asset)
182  {
183  return $this->findSourceFile($asset);
184  }
185 
192  public function getContentType($path)
193  {
194  return strtolower(pathinfo($path, PATHINFO_EXTENSION));
195  }
196 
204  private function findSourceFile(LocalInterface $asset)
205  {
206  $context = $asset->getContext();
207  if ($context instanceof \Magento\Framework\View\Asset\File\FallbackContext) {
208  $result = $this->findFileThroughFallback($asset, $context);
209  } elseif ($context instanceof \Magento\Framework\View\Asset\File\Context) {
210  $result = $this->findFile($asset, $context);
211  } else {
212  $type = get_class($context);
213  throw new \LogicException("Support for {$type} is not implemented.");
214  }
215  return $result;
216  }
217 
225  private function findFileThroughFallback(
226  LocalInterface $asset,
227  \Magento\Framework\View\Asset\File\FallbackContext $context
228  ) {
229  $themeModel = $this->getThemeProvider()->getThemeByFullPath(
230  $context->getAreaCode() . '/' . $context->getThemePath()
231  );
232  $sourceFile = $this->fallback->getFile(
233  $context->getAreaCode(),
234  $themeModel,
235  $context->getLocale(),
236  $asset->getFilePath(),
237  $asset->getModule()
238  );
239  return $sourceFile;
240  }
241 
245  private function getThemeProvider()
246  {
247  if (null === $this->themeProvider) {
248  $this->themeProvider = ObjectManager::getInstance()->get(ThemeProviderInterface::class);
249  }
250 
251  return $this->themeProvider;
252  }
253 
261  private function findFile(LocalInterface $asset, \Magento\Framework\View\Asset\File\Context $context)
262  {
263  $dir = $this->filesystem->getDirectoryRead($context->getBaseDirType());
264  Simple::assertFilePathFormat($asset->getFilePath());
265  return $dir->getAbsolutePath($asset->getPath());
266  }
267 
276  {
277  $sourceFile = $this->findSourceFile($asset);
278  if (!$sourceFile) {
279  return false;
280  }
281  return $this->rootDir->getRelativePath($sourceFile);
282  }
283 
292  private function createChain(LocalInterface $asset, $dir, $path)
293  {
294  if ($path) {
295  $origContent = $this->readFactory->create($dir)->readFile($path);
296  $origContentType = $this->getContentType($path);
297  } else {
298  $origContent = '';
299  $origContentType = $asset->getContentType();
300  }
301 
302  $chain = $this->chainFactory->create(
303  [
304  'asset' => $asset,
305  'origContent' => $origContent,
306  'origContentType' => $origContentType,
307  'origAssetPath' => $dir . '/' . $path
308  ]
309  );
310  return $chain;
311  }
312 }
getContent(LocalInterface $asset)
Definition: Source.php:119
__construct(\Magento\Framework\Filesystem $filesystem, ReadFactory $readFactory, PreProcessor\Pool $preProcessorPool, \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile $fallback, \Magento\Framework\View\Design\Theme\ListInterface $themeList, ChainFactoryInterface $chainFactory)
Definition: Source.php:79
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13
getFile(LocalInterface $asset)
Definition: Source.php:103
getSourceContentType(LocalInterface $asset)
Definition: Source.php:171
findRelativeSourceFilePath(LocalInterface $asset)
Definition: Source.php:275
$themeList
Definition: config_data.php:15
findSource(LocalInterface $asset)
Definition: Source.php:181
$filesystem