/** * Retrieve files * * @param ThemeInterface $theme * @param string $filePath * @return array|\Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $result = []; $namespace = $module = '*'; $sharedFiles = $this->modulesDirectory->search("{$namespace}/{$module}/view/base/{$this->subDir}{$filePath}"); $filePathPtn = strtr(preg_quote($filePath), ['\\*' => '[^/]+']); $pattern = "#(?<namespace>[^/]+)/(?<module>[^/]+)/view/base/{$this->subDir}" . $filePathPtn . "\$#i"; foreach ($sharedFiles as $file) { $filename = $this->modulesDirectory->getAbsolutePath($file); if (!preg_match($pattern, $filename, $matches)) { continue; } $moduleFull = "{$matches['namespace']}_{$matches['module']}"; $result[] = $this->fileFactory->create($filename, $moduleFull, null, true); } $area = $theme->getData('area'); $themeFiles = $this->modulesDirectory->search("{$namespace}/{$module}/view/{$area}/{$this->subDir}{$filePath}"); $pattern = "#(?<namespace>[^/]+)/(?<module>[^/]+)/view/{$area}/{$this->subDir}" . $filePathPtn . "\$#i"; foreach ($themeFiles as $file) { $filename = $this->modulesDirectory->getAbsolutePath($file); if (!preg_match($pattern, $filename, $matches)) { continue; } $moduleFull = "{$matches['namespace']}_{$matches['module']}"; $result[] = $this->fileFactory->create($filename, $moduleFull); } return $result; }
/** * Retrieve files * * @param ThemeInterface $theme * @param string $filePath * @return array|\Magento\Framework\View\File[] * @throws \Magento\Framework\Exception */ public function getFiles(ThemeInterface $theme, $filePath) { $namespace = $module = '*'; $themePath = $theme->getFullPath(); $searchPattern = "{$themePath}/{$namespace}_{$module}/{$this->subDir}*/*/{$filePath}"; $files = $this->themesDirectory->search($searchPattern); if (empty($files)) { return []; } $themes = []; $currentTheme = $theme; while ($currentTheme = $currentTheme->getParentTheme()) { $themes[$currentTheme->getCode()] = $currentTheme; } $result = []; $pattern = "#/(?<module>[^/]+)/{$this->subDir}(?<themeVendor>[^/]+)/(?<themeName>[^/]+)/" . strtr(preg_quote($filePath), ['\\*' => '[^/]+']) . "\$#i"; foreach ($files as $file) { $filename = $this->themesDirectory->getAbsolutePath($file); if (!preg_match($pattern, $filename, $matches)) { continue; } $moduleFull = $matches['module']; $ancestorThemeCode = $matches['themeVendor'] . '/' . $matches['themeName']; if (!isset($themes[$ancestorThemeCode])) { throw new Exception(sprintf("Trying to override modular view file '%s' for theme '%s', which is not ancestor of theme '%s'", $filename, $ancestorThemeCode, $theme->getCode())); } $result[] = $this->fileFactory->create($filename, $moduleFull, $themes[$ancestorThemeCode]); } return $result; }
public function testCreate() { $theme = $this->getMockForAbstractClass('Magento\\Framework\\View\\Design\\ThemeInterface'); $file = new \Magento\Framework\View\File(__FILE__, 'Fixture_Module', $theme); $isBase = true; $this->_objectManager->expects($this->once())->method('create')->with('Magento\\Framework\\View\\File', $this->identicalTo(['filename' => __FILE__, 'module' => 'Fixture_Module', 'theme' => $theme, 'isBase' => $isBase]))->will($this->returnValue($file)); $this->assertSame($file, $this->_model->create(__FILE__, 'Fixture_Module', $theme, $isBase)); }
/** * @param ReadInterface $reader * @param ThemeInterface $theme * @param array $files * @return array */ protected function createFiles(ReadInterface $reader, ThemeInterface $theme, $files) { $result = []; foreach ($files as $file) { $filename = $reader->getAbsolutePath($file); $result[] = $this->fileFactory->create($filename, false, $theme); } return $result; }
/** * Retrieve files * * @param ThemeInterface $theme * @param string $filePath * @return array|\Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $themePath = $theme->getFullPath(); $files = $this->themesDirectory->search("{$themePath}/{$this->subDir}{$filePath}"); $result = []; foreach ($files as $file) { $filename = $this->themesDirectory->getAbsolutePath($file); $result[] = $this->fileFactory->create($filename, null, $theme); } return $result; }
/** * Retrieve files * * @param \Magento\Framework\View\Design\ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $result = []; $sharedFiles = $this->componentDirSearch->collectFilesWithContext(ComponentRegistrar::MODULE, "view/base/{$this->subDir}{$filePath}"); foreach ($sharedFiles as $file) { $result[] = $this->fileFactory->create($file->getFullPath(), $file->getComponentName(), null, true); } $area = $theme->getData('area'); $themeFiles = $this->componentDirSearch->collectFilesWithContext(ComponentRegistrar::MODULE, "view/{$area}/{$this->subDir}{$filePath}"); foreach ($themeFiles as $file) { $result[] = $this->fileFactory->create($file->getFullPath(), $file->getComponentName()); } return $result; }
public function testGetFilesMultiple() { $dirPath = '/Magento_Customer/css/'; $themePath = '/opt/magento2/app/design/frontend/Magento/blank'; $searchPath = 'css/*.test'; $this->themeMock->expects($this->any())->method('getFullPath')->will($this->returnValue($themePath)); $this->themesDirectoryMock->expects($this->any())->method('getAbsolutePath')->will($this->returnValueMap([['fileA.test', $dirPath . 'fileA.test'], ['fileB.tst', $dirPath . 'fileB.tst'], ['fileC.test', $dirPath . 'fileC.test']])); $fileMock = $this->getMockBuilder('Magento\\Framework\\View\\File')->disableOriginalConstructor()->getMock(); // Verifies correct files are searched for $this->themesDirectoryMock->expects($this->once())->method('search')->with($themePath . '/' . $searchPath)->will($this->returnValue(['fileA.test', 'fileC.test'])); // Verifies Magento_Customer was correctly produced from directory path $this->fileFactoryMock->expects($this->any())->method('create')->with($this->isType('string'), null, $this->equalTo($this->themeMock))->will($this->returnValue($fileMock)); $theme = new Theme($this->filesystemMock, $this->fileFactoryMock); // Only two files should be in array, which were returned from search $this->assertEquals([$fileMock, $fileMock], $theme->getFiles($this->themeMock, 'css/*.test')); }
public function testGetFilesWithPreset() { $inputPath = 'preset/3.xml'; $grandparentTheme = $this->getMockForAbstractClass('Magento\Framework\View\Design\ThemeInterface'); $grandparentTheme->expects($this->once())->method('getCode')->willReturn('vendor/grand_parent_theme'); $parentTheme = $this->getMockForAbstractClass('Magento\Framework\View\Design\ThemeInterface'); $parentTheme->expects($this->once())->method('getCode')->willReturn('vendor/parent_theme'); $parentTheme->expects($this->once())->method('getParentTheme')->willReturn($grandparentTheme); $theme = $this->getMockForAbstractClass('Magento\Framework\View\Design\ThemeInterface'); $theme->expects($this->once())->method('getFullPath')->willReturn('area/theme_path'); $theme->expects($this->once())->method('getParentTheme')->willReturn($parentTheme); $filePathOne = 'design/area/theme_path/Module_Two/override/theme/vendor/grand_parent_theme/preset/3.xml'; $this->directory->expects($this->once()) ->method('search') ->with('area/theme_path/*_*/override/theme/*/*/preset/3.xml') ->willReturn([$filePathOne]); $fileOne = new \Magento\Framework\View\File('3.xml', 'Module_Two', $grandparentTheme); $this->fileFactory ->expects($this->once()) ->method('create') ->with($filePathOne, 'Module_Two', $grandparentTheme) ->willReturn($fileOne); $this->pathPatternHelperMock->expects($this->any()) ->method('translatePatternFromGlob') ->with($inputPath) ->willReturn('preset/3.xml'); $this->assertSame([$fileOne], $this->model->getFiles($theme, $inputPath)); }
/** * Retrieve files * * @param ThemeInterface $theme * @param string $filePath * @return array|\Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $namespace = $module = '*'; $themePath = $theme->getFullPath(); $files = $this->themesDirectory->search("{$themePath}/{$namespace}_{$module}/{$this->subDir}{$filePath}"); $result = array(); $pattern = "#/(?<moduleName>[^/]+)/{$this->subDir}" . strtr(preg_quote($filePath), array('\\*' => '[^/]+')) . "\$#i"; foreach ($files as $file) { $filename = $this->themesDirectory->getAbsolutePath($file); if (!preg_match($pattern, $filename, $matches)) { continue; } $result[] = $this->fileFactory->create($filename, $matches['moduleName'], $theme); } return $result; }
/** * Retrieve files * * @param ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $namespace = $module = '*'; $themePath = $theme->getFullPath(); if (empty($themePath)) { return []; } $themeAbsolutePath = $this->componentRegistrar->getPath(ComponentRegistrar::THEME, $themePath); if (!$themeAbsolutePath) { return []; } $themeDir = $this->readDirFactory->create($themeAbsolutePath); $searchPattern = "{$namespace}_{$module}/{$this->subDir}{$filePath}"; $files = $themeDir->search($searchPattern); $result = []; $pattern = "#(?<moduleName>[^/]+)/{$this->subDir}" . $this->pathPatternHelper->translatePatternFromGlob($filePath) . "\$#i"; foreach ($files as $file) { $filename = $themeDir->getAbsolutePath($file); if (!preg_match($pattern, $filename, $matches)) { continue; } $result[] = $this->fileFactory->create($filename, $matches['moduleName']); } return $result; }
/** * 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; }
public function testGetFiles() { $files = []; foreach (['shared', 'theme'] as $fileType) { for ($i = 0; $i < 2; $i++) { $file = $this->getMock('\\Magento\\Framework\\Component\\ComponentFile', [], [], '', false); $file->expects($this->once())->method('getFullPath')->will($this->returnValue("{$fileType}/module/{$i}/path")); $file->expects($this->once())->method('getComponentName')->will($this->returnValue('Module_' . $i)); $files[$fileType][] = $file; } } $this->dirSearch->expects($this->any())->method('collectFilesWithContext')->willReturnMap([[ComponentRegistrar::MODULE, 'view/base/layout/*.xml', $files['shared']], [ComponentRegistrar::MODULE, 'view/frontend/layout/*.xml', $files['theme']]]); $this->fileFactoryMock->expects($this->atLeastOnce())->method('create')->willReturn($this->createFileMock()); $this->themeMock->expects($this->once())->method('getData')->with('area')->willReturn('frontend'); $result = $this->fileCollector->getFiles($this->themeMock, '*.xml'); $this->assertCount(4, $result); $this->assertInstanceOf('Magento\\Framework\\View\\File', $result[0]); $this->assertInstanceOf('Magento\\Framework\\View\\File', $result[1]); $this->assertInstanceOf('Magento\\Framework\\View\\File', $result[2]); $this->assertInstanceOf('Magento\\Framework\\View\\File', $result[3]); }
public function testGetFilesMultiple() { $dirPath = '/Magento_Customer/css/'; $searchPath = 'css/*.test'; $this->componentRegistrar->expects($this->once())->method('getPath')->with(ComponentRegistrar::THEME, $this->themePath)->will($this->returnValue(self::FULL_THEME_PATH)); $fileMock = $this->getMockBuilder('Magento\\Framework\\View\\File')->disableOriginalConstructor()->getMock(); $this->themeDirectoryMock->expects($this->any())->method('getAbsolutePath')->willReturnMap([['fileA.test', $dirPath . 'fileA.test'], ['fileB.tst', $dirPath . 'fileB.tst'], ['fileC.test', $dirPath . 'fileC.test']]); // Verifies correct files are searched for $this->themeDirectoryMock->expects($this->once())->method('search')->with($searchPath)->willReturn(['fileA.test', 'fileC.test']); // Verifies Magento_Customer was correctly produced from directory path $this->fileFactoryMock->expects($this->any())->method('create')->with($this->isType('string'), null, $this->themeMock)->willReturn($fileMock); // Only two files should be in array, which were returned from search $this->assertEquals([$fileMock, $fileMock], $this->themeFileCollector->getFiles($this->themeMock, $searchPath)); }
public function testGetFilesWithPreset() { $themePath = 'area/theme_path'; $inputPath = 'preset/3.xml'; $grandparentTheme = $this->getMockForAbstractClass('Magento\\Framework\\View\\Design\\ThemeInterface'); $grandparentTheme->expects($this->once())->method('getCode')->willReturn('vendor/grand_parent_theme'); $parentTheme = $this->getMockForAbstractClass('Magento\\Framework\\View\\Design\\ThemeInterface'); $parentTheme->expects($this->once())->method('getCode')->willReturn('vendor/parent_theme'); $parentTheme->expects($this->once())->method('getParentTheme')->willReturn($grandparentTheme); $theme = $this->getMockForAbstractClass('Magento\\Framework\\View\\Design\\ThemeInterface'); $theme->expects($this->once())->method('getFullPath')->willReturn($themePath); $theme->expects($this->once())->method('getParentTheme')->willReturn($parentTheme); $filePathOne = 'design/area/theme_path/Module_Two/override/theme/vendor/grand_parent_theme/preset/3.xml'; $this->themeDirectory->expects($this->once())->method('search')->with('*_*/override/theme/*/*/preset/3.xml')->willReturn([$filePathOne]); $fileOne = $this->getMock('Magento\\Framework\\View\\File', [], [], '', false); $this->fileFactory->expects($this->once())->method('create')->with($filePathOne, 'Module_Two', $grandparentTheme)->willReturn($fileOne); $this->pathPatternHelperMock->expects($this->any())->method('translatePatternFromGlob')->with($inputPath)->willReturn('preset/3.xml'); $this->componentRegistrar->expects($this->once())->method('getPath')->with(ComponentRegistrar::THEME, $themePath)->will($this->returnValue('/full/theme/path')); $this->assertSame([$fileOne], $this->model->getFiles($theme, $inputPath)); }
public function testGetFiles() { $sharedFiles = [ 'Namespace/One/view/base/layout/one.xml', 'Namespace/Two/view/base/layout/two.xml' ]; $themeFiles = [ 'Namespace/Two/view/frontend/layout/four.txt', 'Namespace/Two/view/frontend/layout/three.xml' ]; $this->directoryMock->expects($this->any()) ->method('search') ->willReturnMap( [ ['*/*/view/base/layout/*.xml', null, $sharedFiles], ['*/*/view/frontend/layout/*.xml', null, $themeFiles] ] ); $this->pathPatternHelperMock->expects($this->once()) ->method('translatePatternFromGlob') ->with('*.xml') ->willReturn('[^/]*\\.xml'); $this->directoryMock->expects($this->atLeastOnce()) ->method('getAbsolutePath') ->willReturnArgument(0); $this->fileFactoryMock->expects($this->atLeastOnce()) ->method('create') ->willReturn($this->createFileMock()); $this->themeMock->expects($this->once()) ->method('getData') ->with('area') ->willReturn('frontend'); $result = $this->fileCollector->getFiles($this->themeMock, '*.xml'); $this->assertCount(3, $result); $this->assertInstanceOf('Magento\Framework\View\File', $result[0]); $this->assertInstanceOf('Magento\Framework\View\File', $result[1]); $this->assertInstanceOf('Magento\Framework\View\File', $result[2]); }
/** * Retrieve files * * @param ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] * @throws \Magento\Framework\Exception\LocalizedException */ public function getFiles(ThemeInterface $theme, $filePath) { $namespace = $module = '*'; $themePath = $theme->getFullPath(); if (empty($themePath)) { return []; } $themeAbsolutePath = $this->componentRegistrar->getPath(ComponentRegistrar::THEME, $themePath); if (!$themeAbsolutePath) { return []; } $themeDir = $this->readDirFactory->create($themeAbsolutePath); $files = $themeDir->search("{$namespace}_{$module}/{$this->subDir}*/*/{$filePath}"); if (empty($files)) { return []; } $themes = []; $currentTheme = $theme; while ($currentTheme = $currentTheme->getParentTheme()) { $themes[$currentTheme->getCode()] = $currentTheme; } $result = []; $pattern = "#/(?<module>[^/]+)/{$this->subDir}(?<themeVendor>[^/]+)/(?<themeName>[^/]+)/" . $this->pathPatternHelper->translatePatternFromGlob($filePath) . "\$#i"; foreach ($files as $file) { $filename = $themeDir->getAbsolutePath($file); if (!preg_match($pattern, $filename, $matches)) { continue; } $moduleFull = $matches['module']; $ancestorThemeCode = $matches['themeVendor'] . '/' . $matches['themeName']; if (!isset($themes[$ancestorThemeCode])) { throw new LocalizedException(new \Magento\Framework\Phrase("Trying to override modular view file '%1' for theme '%2', which is not ancestor of theme '%3'", [$filename, $ancestorThemeCode, $theme->getCode()])); } $result[] = $this->fileFactory->create($filename, $moduleFull, $themes[$ancestorThemeCode]); } return $result; }