/** * Retrieve files * * Filter out theme files that belong to inactive modules or ones explicitly configured to not produce any output * * @param ThemeInterface $theme * @param string $filePath * @return array|\Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $result = array(); foreach ($this->subject->getFiles($theme, $filePath) as $file) { if ($this->moduleManager->isOutputEnabled($file->getModule())) { $result[] = $file; } } return $result; }
/** * Retrieve files * * Aggregate layout files from modules and a theme and its ancestors * * @param ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $list = $this->fileListFactory->create(); $list->add($this->baseFiles->getFiles($theme, $filePath)); foreach ($theme->getInheritedThemes() as $currentTheme) { $list->add($this->themeFiles->getFiles($currentTheme, $filePath)); $list->replace($this->overrideBaseFiles->getFiles($currentTheme, $filePath)); $list->replace($this->overrideThemeFiles->getFiles($currentTheme, $filePath)); } return $list->getAll(); }
/** * Retrieve files * * Aggregate source files from modules and a theme and its ancestors * * @param \Magento\Framework\View\Design\ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] * @throws \LogicException */ public function getFiles(ThemeInterface $theme, $filePath) { $list = $this->fileListFactory->create('Magento\\Framework\\Css\\PreProcessor\\File\\FileList\\Collator'); $list->add($this->libraryFiles->getFiles($theme, $filePath)); $list->add($this->baseFiles->getFiles($theme, $filePath)); foreach ($theme->getInheritedThemes() as $currentTheme) { $files = $this->overriddenBaseFiles->getFiles($currentTheme, $filePath); $list->replace($files); } $result = $list->getAll(); if (empty($result)) { $this->logger->notice('magento_import returns empty result by path ' . $filePath . ' for theme ' . $theme->getCode()); } return $result; }
/** * Collect and merge layout updates from files * * @return \Magento\Framework\View\Layout\Element * @throws \Magento\Framework\Exception */ protected function _loadFileLayoutUpdatesXml() { $layoutStr = ''; $theme = $this->_getPhysicalTheme($this->theme); $updateFiles = $this->fileSource->getFiles($theme, '*.xml'); $updateFiles = array_merge($updateFiles, $this->pageLayoutFileSource->getFiles($theme, '*.xml')); $dir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT); $useErrors = libxml_use_internal_errors(true); foreach ($updateFiles as $file) { $filename = $dir->getRelativePath($file->getFilename()); $fileStr = $dir->readFile($filename); $fileStr = $this->_substitutePlaceholders($fileStr); /** @var $fileXml \Magento\Framework\View\Layout\Element */ $fileXml = $this->_loadXmlString($fileStr); if (!$fileXml instanceof \Magento\Framework\View\Layout\Element) { $this->_logXmlErrors($file->getFilename(), libxml_get_errors()); libxml_clear_errors(); continue; } if (!$file->isBase() && $fileXml->xpath(self::XPATH_HANDLE_DECLARATION)) { throw new \Magento\Framework\Exception(sprintf("Theme layout update file '%s' must not declare page types.", $file->getFileName())); } $handleName = basename($file->getFilename(), '.xml'); $tagName = $fileXml->getName() === 'layout' ? 'layout' : 'handle'; $handleAttributes = ' id="' . $handleName . '"' . $this->_renderXmlAttributes($fileXml); $handleStr = '<' . $tagName . $handleAttributes . '>' . $fileXml->innerXml() . '</' . $tagName . '>'; $layoutStr .= $handleStr; } libxml_use_internal_errors($useErrors); $layoutStr = '<layouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' . $layoutStr . '</layouts>'; $layoutXml = $this->_loadXmlString($layoutStr); return $layoutXml; }
/** * Replace @magento_import to @import less instructions * * @param array $matchedContent * @param LocalInterface $asset * @return string */ protected function replace(array $matchedContent, LocalInterface $asset) { $importsContent = ''; try { $matchedFileId = $matchedContent['path']; $relatedAsset = $this->assetRepo->createRelated($matchedFileId, $asset); $resolvedPath = $relatedAsset->getFilePath(); $importFiles = $this->fileSource->getFiles($this->getTheme($relatedAsset), $resolvedPath); /** @var $importFile \Magento\Framework\View\File */ foreach ($importFiles as $importFile) { $importsContent .= $importFile->getModule() ? "@import '{$importFile->getModule()}::{$resolvedPath}';\n" : "@import '{$matchedFileId}';\n"; } } catch (\LogicException $e) { $this->errorHandler->processException($e); } return $importsContent; }
/** * Get layout files from modules, theme with ancestors and library * * @param ThemeInterface $theme * @param string $filePath * @throws \InvalidArgumentException * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { if (empty($filePath)) { throw new \InvalidArgumentException('File path must be specified'); } $files = []; if ($this->libDirectory->isExist($filePath)) { $filename = $this->libDirectory->getAbsolutePath($filePath); $files[] = $this->fileFactory->create($filename); } $files = array_merge($files, $this->baseFiles->getFiles($theme, $filePath)); foreach ($theme->getInheritedThemes() as $currentTheme) { $files = array_merge($files, $this->themeModularFiles->getFiles($currentTheme, $filePath)); $files = array_merge($files, $this->themeFiles->getFiles($currentTheme, $filePath)); } return $files; }
/** * Collect files * * @param string|null $searchPattern * @return array * @throws \Exception */ public function collectFiles($searchPattern = null) { $result = []; if ($searchPattern === null) { $searchPattern = $this->searchPattern; } if ($searchPattern === null) { throw new \Exception('Search pattern cannot be empty.'); } $files = $this->collectorAggregated->getFiles($this->design->getDesignTheme(), $searchPattern); $fileReader = $this->filesystem->getDirectoryRead(DirectoryList::ROOT); foreach ($files as $file) { $filePath = $fileReader->getRelativePath($file->getFilename()); $result[sprintf('%x', crc32($filePath))] = $fileReader->readFile($filePath); } return $result; }
/** * Collect files * * @param string|null $searchPattern * @return array * @throws \Exception */ public function collectFiles($searchPattern = null) { $result = []; if ($searchPattern === null) { $searchPattern = $this->searchPattern; } if ($searchPattern === null) { throw new \Exception('Search pattern cannot be empty.'); } $files = $this->collectorAggregated->getFiles($this->design->getDesignTheme(), $searchPattern); foreach ($files as $file) { $fullFileName = $file->getFileName(); $fileDir = dirname($fullFileName); $fileName = basename($fullFileName); $dirRead = $this->readFactory->create($fileDir); $result[$fullFileName] = $dirRead->readFile($fileName); } return $result; }
/** * @param string $originalContent * @param string $foundPath * @param string $resolvedPath * @param array $foundFiles * @param string $expectedContent * * @dataProvider processDataProvider */ public function testProcess($originalContent, $foundPath, $resolvedPath, $foundFiles, $expectedContent) { $chain = new \Magento\Framework\View\Asset\PreProcessor\Chain($this->asset, $originalContent, 'css'); $relatedAsset = $this->getMock('\\Magento\\Framework\\View\\Asset\\File', [], [], '', false); $relatedAsset->expects($this->once())->method('getFilePath')->will($this->returnValue($resolvedPath)); $context = $this->getMock('\\Magento\\Framework\\View\\Asset\\File\\FallbackContext', [], [], '', false); $this->assetRepo->expects($this->once())->method('createRelated')->with($foundPath, $this->asset)->will($this->returnValue($relatedAsset)); $relatedAsset->expects($this->once())->method('getContext')->will($this->returnValue($context)); $theme = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Design\\ThemeInterface'); $this->themeList->expects($this->once())->method('getThemeByFullPath')->will($this->returnValue($theme)); $files = []; foreach ($foundFiles as $file) { $fileObject = $this->getMock('Magento\\Framework\\View\\File', [], [], '', false); $fileObject->expects($this->any())->method('getModule')->will($this->returnValue($file['module'])); $fileObject->expects($this->any())->method('getFilename')->will($this->returnValue($file['filename'])); $files[] = $fileObject; } $this->fileSource->expects($this->once())->method('getFiles')->with($theme, $resolvedPath)->will($this->returnValue($files)); $this->object->process($chain); $this->assertEquals($expectedContent, $chain->getContent()); $this->assertEquals('css', $chain->getContentType()); }
/** * * @dataProvider getFilesDataProvider * * @param $libraryFiles array Files in lib directory * @param $baseFiles array Files in base directory * @param $themeFiles array Files in theme * * * @return void */ public function testGetFiles($libraryFiles, $baseFiles, $themeFiles) { $this->fileListMock->expects($this->at(0))->method('add')->with($this->equalTo($libraryFiles)); $this->fileListMock->expects($this->at(1))->method('add')->with($this->equalTo($baseFiles)); $this->fileListMock->expects($this->any())->method('getAll')->will($this->returnValue(['returnedFile'])); $subPath = '*'; $this->libraryFilesMock->expects($this->atLeastOnce())->method('getFiles')->with($this->themeMock, $subPath)->will($this->returnValue($libraryFiles)); $this->baseFilesMock->expects($this->atLeastOnce())->method('getFiles')->with($this->themeMock, $subPath)->will($this->returnValue($baseFiles)); $this->overriddenBaseFilesMock->expects($this->any())->method('getFiles')->will($this->returnValue($themeFiles)); $aggregated = new Aggregated($this->fileListFactoryMock, $this->libraryFilesMock, $this->baseFilesMock, $this->overriddenBaseFilesMock, $this->loggerMock); $inheritedThemeMock = $this->getMockBuilder('\\Magento\\Framework\\View\\Design\\ThemeInterface')->getMock(); $this->themeMock->expects($this->any())->method('getInheritedThemes')->will($this->returnValue([$inheritedThemeMock])); $this->assertEquals(['returnedFile'], $aggregated->getFiles($this->themeMock, $subPath)); }
/** * Retrieve view files, sorted by the priority of modules they belong to * * @param ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $result = $this->subject->getFiles($theme, $filePath); usort($result, [$this, 'compareFiles']); return $result; }
/** * Scan all the themes in the system, for each theme retrieve list of files via $filesRetriever, * and return them as array of pairs [file, theme]. * * @param \Magento\Framework\View\File\CollectorInterface $filesRetriever * @return array */ protected function _retrieveFilesForEveryTheme(\Magento\Framework\View\File\CollectorInterface $filesRetriever) { $result = array(); /** @var $themeCollection \Magento\Core\Model\Theme\Collection */ $themeCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Core\\Model\\Theme\\Collection'); $themeCollection->addDefaultPattern('*'); /** @var $theme \Magento\Framework\View\Design\ThemeInterface */ foreach ($themeCollection as $theme) { foreach ($filesRetriever->getFiles($theme, '*.xml') as $file) { $result['theme: ' . $theme->getFullPath() . ', ' . $file->getFilename()] = array($file, $theme); } } return $result; }