/** * Propagate parameters necessary for modular rule basing on module_name parameter * * @param array $params * @return array * @throws \InvalidArgumentException */ public function getPatternDirs(array $params) { if (!array_key_exists('module_name', $params)) { throw new \InvalidArgumentException('Required parameter "module_name" is not specified.'); } $params['module_dir'] = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $params['module_name']); return $this->rule->getPatternDirs($params); }
public function testGetPatternDirs() { $expectedResult = ['path1', 'path2']; $module = 'Some_Module'; $modulePath = '/module/path'; $this->componentRegistrar->expects($this->once())->method('getPath')->with(ComponentRegistrar::MODULE, $module)->will($this->returnValue($modulePath)); $this->rule->expects($this->once())->method('getPatternDirs')->with(['module_name' => $module, 'module_dir' => $modulePath])->will($this->returnValue($expectedResult)); $this->assertEquals($expectedResult, $this->model->getPatternDirs(['module_name' => $module])); }
/** * Get path of file after using fallback rules * * @param RuleInterface $fallbackRule * @param string $file * @param array $params * @return string|bool */ protected function resolveFile(RuleInterface $fallbackRule, $file, array $params = []) { foreach ($fallbackRule->getPatternDirs($params) as $dir) { $path = "{$dir}/{$file}"; if ($this->rootDirectory->isExist($this->rootDirectory->getRelativePath($path))) { return $path; } } return false; }
/** * Get path of file after using fallback rules * * @param RuleInterface $fallbackRule * @param string $file * @param array $params * @return string|bool */ protected function resolveFile(RuleInterface $fallbackRule, $file, array $params = []) { foreach ($fallbackRule->getPatternDirs($params) as $dir) { $path = "{$dir}/{$file}"; $dirRead = $this->readFactory->create($dir); if ($dirRead->isExist($file)) { return $path; } } return false; }
public function testResolve() { $requestedFile = 'file.css'; $expected = 'some/dir/file.less'; $theme = $this->getMockForAbstractClass('Magento\\Framework\\View\\Design\\ThemeInterface'); $theme->expects($this->any())->method('getFullPath')->will($this->returnValue('magento_theme')); $this->rule->expects($this->atLeastOnce())->method('getPatternDirs')->will($this->returnValue(['some/dir'])); $fileExistsMap = [['file.css', false], ['file.less', true]]; $this->directory->expects($this->any())->method('isExist')->will($this->returnValueMap($fileExistsMap)); $actual = $this->object->resolve('type', $requestedFile, 'frontend', $theme, 'en_US', 'Magento_Module'); $this->assertSame($expected, $actual); }
public function testGetPatternDirs() { $parentTheme = $this->getMockForAbstractClass('Magento\\Framework\\View\\Design\\ThemeInterface'); $parentTheme->expects($this->any())->method('getFullPath')->will($this->returnValue('package/parent_theme')); $theme = $this->getMockForAbstractClass('Magento\\Framework\\View\\Design\\ThemeInterface'); $theme->expects($this->any())->method('getFullPath')->will($this->returnValue('package/current_theme')); $theme->expects($this->any())->method('getParentTheme')->will($this->returnValue($parentTheme)); $this->componentRegistrar->expects($this->any())->method('getPath')->will($this->returnValueMap([[ComponentRegistrar::THEME, 'package/parent_theme', '/path/to/parent/theme'], [ComponentRegistrar::THEME, 'package/current_theme', '/path/to/current/theme']])); $ruleDirsMap = [[['theme_dir' => '/path/to/current/theme'], ['package/current_theme/path/one', 'package/current_theme/path/two']], [['theme_dir' => '/path/to/parent/theme'], ['package/parent_theme/path/one', 'package/parent_theme/path/two']]]; $this->rule->expects($this->any())->method('getPatternDirs')->will($this->returnValueMap($ruleDirsMap)); $expectedResult = ['package/current_theme/path/one', 'package/current_theme/path/two', 'package/parent_theme/path/one', 'package/parent_theme/path/two']; $this->assertEquals($expectedResult, $this->model->getPatternDirs(['theme' => $theme])); }
public function testResolveNonexistentFile() { $this->readFactoryMock->expects($this->any())->method('create')->willReturn($this->directoryMock); $this->ruleMock->expects($this->once())->method('getPatternDirs')->willReturn(['some/dir']); $this->directoryMock->expects($this->once())->method('isExist')->willReturn(false); $this->assertFalse($this->object->resolve('type', 'file.ext', 'frontend', $this->getMockForTheme('magento_theme'), 'en_US', 'Magento_Module')); }
/** * Propagate an underlying fallback rule to every theme in a hierarchy: parent, grandparent, etc. * * @param array $params * @return array * @throws \InvalidArgumentException */ public function getPatternDirs(array $params) { if (!array_key_exists('theme', $params) || !$params['theme'] instanceof ThemeInterface) { throw new \InvalidArgumentException('Parameter "theme" should be specified and should implement the theme interface.'); } $result = array(); /** @var $theme ThemeInterface */ $theme = $params['theme']; unset($params['theme']); while ($theme) { if ($theme->getThemePath()) { $params['theme_path'] = $theme->getThemePath(); $result = array_merge($result, $this->rule->getPatternDirs($params)); } $theme = $theme->getParentTheme(); } return $result; }
public function testResolveFromCache() { $expectedPath = '/some/dir/file.ext'; $this->cache->expects($this->once())->method('getFromCache')->with('type', 'file.ext', 'frontend', 'magento_theme', 'en_US', 'Magento_Module')->will($this->returnValue($expectedPath)); $this->directory->expects($this->once())->method('getAbsolutePath')->with($expectedPath)->will($this->returnValue($expectedPath)); $this->rule->expects($this->never())->method('getPatternDirs'); $this->cache->expects($this->never())->method('saveToCache'); $actualPath = $this->object->resolve('type', 'file.ext', 'frontend', $this->getMockForTheme('magento_theme'), 'en_US', 'Magento_Module'); $this->assertSame($expectedPath, $actualPath); }
/** * Return pattern for theme locale directories, where <locale_placeholder> is placed to mark a locale's location. * * @param \Magento\Framework\View\Design\ThemeInterface $theme * @return string * @throws \Exception */ protected static function _getLocalePatternDir(\Magento\Framework\View\Design\ThemeInterface $theme) { $localePlaceholder = '<locale_placeholder>'; $params = array('area' => $theme->getArea(), 'theme' => $theme, 'locale' => $localePlaceholder); $patternDirs = self::$_fallbackRule->getPatternDirs($params); $themePath = '/' . $theme->getFullPath() . '/'; foreach ($patternDirs as $patternDir) { $patternPath = $patternDir . '/'; if (strpos($patternPath, $themePath) !== false && strpos($patternPath, $localePlaceholder) !== false) { return $patternDir; } } throw new \Exception('Unable to determine theme locale path'); }