Пример #1
0
 /**
  * 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;
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 /**
  * 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;
 }
Пример #4
0
 /**
  * Create new config object
  *
  * @param ThemeInterface $theme
  * @return mixed
  * @throws \Magento\Framework\Exception
  */
 public function create(ThemeInterface $theme)
 {
     if (!isset($this->_types[$theme->getType()])) {
         throw new \Magento\Framework\Exception(sprintf('Invalid type of theme domain model "%s"', $theme->getType()));
     }
     $class = $this->_types[$theme->getType()];
     return $this->_objectManager->create($class, ['theme' => $theme]);
 }
Пример #5
0
 protected function setUp()
 {
     parent::setUp();
     $this->_layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\View\\LayoutInterface');
     $this->_theme = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Framework\\View\\Design\\ThemeInterface');
     $this->_theme->setType(\Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL);
     $this->_block = $this->_layout->createBlock('Magento\\Theme\\Block\\Adminhtml\\System\\Design\\Theme\\Edit\\Tab\\General');
 }
Пример #6
0
 public function testToHtmlPreviewImageUrl()
 {
     /** @var $objectManager \Magento\TestFramework\ObjectManager */
     $this->_theme->setType(\Magento\Framework\View\Design\ThemeInterface::TYPE_PHYSICAL);
     $this->_theme->setPreviewImage('preview_image_test.jpg');
     $this->_block->setArea('adminhtml');
     $html = $this->_block->toHtml();
     preg_match_all('/pub\\/static\\/adminhtml\\/_view\\/en_US/', $html, $result);
     $this->assertEmpty($result[0]);
 }
Пример #7
0
 /**
  * @param \Magento\Framework\View\DesignInterface $design
  * @param \Magento\Framework\Filesystem $filesystem
  * @param \Magento\Framework\Event\ManagerInterface $eventDispatcher
  * @param \Magento\Framework\View\ConfigInterface $viewConfig
  * @param \Magento\DesignEditor\Model\Config\Control\AbstractControl $configuration
  * @param \Magento\Framework\View\Design\ThemeInterface $theme
  * @param \Magento\Framework\View\Design\ThemeInterface $parentTheme
  */
 public function __construct(\Magento\Framework\View\DesignInterface $design, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Event\ManagerInterface $eventDispatcher, \Magento\Framework\View\ConfigInterface $viewConfig, \Magento\DesignEditor\Model\Config\Control\AbstractControl $configuration = null, \Magento\Framework\View\Design\ThemeInterface $theme = null, \Magento\Framework\View\Design\ThemeInterface $parentTheme = null)
 {
     $this->_configuration = $configuration;
     $this->_theme = $theme;
     $this->_parentTheme = $parentTheme ?: $theme->getParentTheme();
     $this->_design = $design;
     $this->_filesystem = $filesystem;
     $this->_eventDispatcher = $eventDispatcher;
     $this->_viewConfigLoader = $viewConfig;
     $this->_initViewConfigs()->_loadControlsData();
 }
Пример #8
0
 public function testGetByAreaWithOtherAreaAndNumericThemeId()
 {
     $this->designMock->expects($this->once())->method('getDesignTheme')->will($this->returnValue($this->themeMock));
     $this->designMock->expects($this->once())->method('getArea')->will($this->returnValue('design_area'));
     $this->designMock->expects($this->once())->method('getConfigurationDesignTheme')->will($this->returnValue(12));
     $this->themeMock->expects($this->once())->method('getArea')->will($this->returnValue('theme_area'));
     $this->themeCollectionFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->themeCollectionMock));
     $this->themeCollectionMock->expects($this->once())->method('getItemById')->with(12)->will($this->returnValue($this->themeMock));
     $this->appStateMock->expects($this->once())->method('getAreaCode')->will($this->returnValue('other_area'));
     $this->assertEquals($this->themeMock, $this->model->get());
 }
Пример #9
0
 /**
  * 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;
 }
Пример #10
0
 /**
  * Retrieve layout updates by handle
  *
  * @param string $handle
  * @param \Magento\Framework\View\Design\ThemeInterface $theme
  * @param \Magento\Framework\App\ScopeInterface $store
  * @return string
  */
 public function fetchUpdatesByHandle($handle, \Magento\Framework\View\Design\ThemeInterface $theme, \Magento\Framework\App\ScopeInterface $store)
 {
     $bind = ['layout_update_handle' => $handle, 'theme_id' => $theme->getId(), 'store_id' => $store->getId()];
     $result = '';
     $connection = $this->getConnection();
     if ($connection) {
         $select = $this->_getFetchUpdatesByHandleSelect();
         $result = join('', $connection->fetchCol($select, $bind));
     }
     return $result;
 }
Пример #11
0
 public function setup()
 {
     $this->themeDirectoryMock = $this->getMockBuilder('Magento\\Framework\\Filesystem\\Directory\\ReadInterface')->getMock();
     $this->fileFactoryMock = $this->getMockBuilder('Magento\\Framework\\View\\File\\Factory')->disableOriginalConstructor()->getMock();
     $this->themeMock = $this->getMockBuilder('Magento\\Framework\\View\\Design\\ThemeInterface')->getMock();
     $this->themeMock->expects($this->once())->method('getFullPath')->will($this->returnValue($this->themePath));
     $this->readDirFactory = $this->getMock('Magento\\Framework\\Filesystem\\Directory\\ReadFactory', [], [], '', false);
     $this->readDirFactory->expects($this->any())->method('create')->will($this->returnValue($this->themeDirectoryMock));
     $this->componentRegistrar = $this->getMockForAbstractClass('Magento\\Framework\\Component\\ComponentRegistrarInterface');
     $this->themeFileCollector = new Theme($this->fileFactoryMock, $this->readDirFactory, $this->componentRegistrar);
 }
Пример #12
0
 /**
  * Retrieve layout updates by handle
  *
  * @param string $handle
  * @param \Magento\Framework\View\Design\ThemeInterface $theme
  * @param \Magento\Store\Model\Store $store
  * @return string
  */
 public function fetchUpdatesByHandle($handle, \Magento\Framework\View\Design\ThemeInterface $theme, \Magento\Store\Model\Store $store)
 {
     $bind = ['layout_update_handle' => $handle, 'theme_id' => $theme->getId(), 'store_id' => $store->getId()];
     $result = '';
     $readAdapter = $this->_getReadAdapter();
     if ($readAdapter) {
         $select = $this->_getFetchUpdatesByHandleSelect();
         $result = join('', $readAdapter->fetchCol($select, $bind));
     }
     return $result;
 }
Пример #13
0
 /**
  * @param string $area
  * @param string $themePath
  * @param string $locale
  * @param string $module
  * @param string $expectedId
  * @param string $savedValue
  *
  * @dataProvider cacheDataProvider
  */
 public function testSaveToCache($area, $themePath, $locale, $module, $expectedId, $savedValue)
 {
     if (isset($params['theme'])) {
         $this->theme->expects($this->any())->method('getThemePath')->will($this->returnValue($params['theme']));
         $params['theme'] = $this->theme;
     } else {
         $this->theme->expects($this->never())->method('getThemePath');
     }
     $this->cache->expects($this->once())->method('save')->with($savedValue, $expectedId)->will($this->returnValue(true));
     $actual = $this->object->saveToCache($savedValue, 'file', 'file.ext', $area, $themePath, $locale, $module);
     $this->assertTrue($actual);
 }
Пример #14
0
 /**
  * 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;
 }
Пример #15
0
 /**
  * Retrieve layout updates by handle
  *
  * @param string $handle
  * @param \Magento\Framework\View\Design\ThemeInterface $theme
  * @param \Magento\Framework\App\ScopeInterface $store
  * @return string
  */
 public function fetchUpdatesByHandle($handle, \Magento\Framework\View\Design\ThemeInterface $theme, \Magento\Framework\App\ScopeInterface $store)
 {
     $bind = ['theme_id' => $theme->getId(), 'store_id' => $store->getId()];
     $cacheKey = implode('-', $bind);
     if (!isset($this->layoutUpdateCache[$cacheKey])) {
         $this->layoutUpdateCache[$cacheKey] = [];
         foreach ($this->getConnection()->fetchAll($this->_getFetchUpdatesByHandleSelect(), $bind) as $layout) {
             if (!isset($this->layoutUpdateCache[$cacheKey][$layout['handle']])) {
                 $this->layoutUpdateCache[$cacheKey][$layout['handle']] = '';
             }
             $this->layoutUpdateCache[$cacheKey][$layout['handle']] .= $layout['xml'];
         }
     }
     return isset($this->layoutUpdateCache[$cacheKey][$handle]) ? $this->layoutUpdateCache[$cacheKey][$handle] : '';
 }
Пример #16
0
 /**
  * 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;
 }
Пример #17
0
 /**
  * Create 'staging' theme associated with current 'virtual' theme
  *
  * @return \Magento\Framework\View\Design\ThemeInterface
  */
 protected function _createStagingTheme()
 {
     $stagingTheme = $this->_themeFactory->create();
     $stagingTheme->setData(['parent_id' => $this->_theme->getId(), 'theme_path' => null, 'theme_title' => sprintf('%s - Staging', $this->_theme->getThemeTitle()), 'preview_image' => $this->_theme->getPreviewImage(), 'is_featured' => $this->_theme->getIsFeatured(), 'type' => \Magento\Framework\View\Design\ThemeInterface::TYPE_STAGING]);
     $stagingTheme->save();
     return $stagingTheme;
 }
Пример #18
0
 /**
  * Creates or updates custom single file which belong to a selected theme
  *
  * @param \Magento\Framework\View\Design\ThemeInterface $themeModel
  * @param string $fileContent
  * @return \Magento\Framework\View\Design\Theme\FileInterface
  */
 public function update(\Magento\Framework\View\Design\ThemeInterface $themeModel, $fileContent)
 {
     $customFiles = $themeModel->getCustomization()->getFilesByType($this->_fileService->getType());
     $customCss = reset($customFiles);
     if (empty($fileContent) && $customCss) {
         $customCss->delete();
         return $customCss;
     }
     if (!$customCss) {
         $customCss = $this->_fileService->create();
     }
     $customCss->setData('content', $fileContent);
     $customCss->setTheme($themeModel);
     $customCss->save();
     return $customCss;
 }
Пример #19
0
 /**
  * Add theme fieldset
  *
  * @param \Magento\Framework\Data\Form $form
  * @param array $formData
  * @param ThemeInterface $theme
  * @return $this
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _addThemeFieldset($form, $formData, ThemeInterface $theme)
 {
     $themeFieldset = $form->addFieldset('theme', ['legend' => __('Theme Settings')]);
     $this->_addElementTypes($themeFieldset);
     if (isset($formData['theme_id'])) {
         $themeFieldset->addField('theme_id', 'hidden', ['name' => 'theme_id']);
     }
     /** @var \Magento\Theme\Model\Theme\Collection $themesCollections */
     $themesCollections = $this->_objectManager->create('Magento\\Theme\\Model\\Theme\\Collection');
     /** @var \Magento\Framework\Json\Helper\Data $helper */
     $helper = $this->_objectManager->get('Magento\\Framework\\Json\\Helper\\Data');
     $onChangeScript = sprintf('parentThemeOnChange(this.value, %s)', str_replace('"', '\'', $helper->jsonEncode($this->_getDefaultsInherited($themesCollections->addDefaultPattern()))));
     /** @var ThemeInterface $parentTheme */
     $parentTheme = $this->_objectManager->create('Magento\\Framework\\View\\Design\\ThemeInterface');
     if (!empty($formData['parent_id'])) {
         $parentTheme->load($formData['parent_id']);
     }
     if ($this->_getCurrentTheme()->isObjectNew()) {
         $themeFieldset->addField('parent_id', 'select', ['label' => __('Parent Theme'), 'title' => __('Parent Theme'), 'name' => 'parent_id', 'values' => $themesCollections->toOptionArray(!$parentTheme->getId()), 'required' => true, 'class' => 'no-changes', 'onchange' => $onChangeScript]);
     } elseif (!empty($formData['parent_id'])) {
         $themeFieldset->addField('parent_title', 'note', ['label' => __('Parent Theme'), 'title' => __('Parent Theme'), 'name' => 'parent_title', 'text' => $parentTheme->getId() ? $parentTheme->getThemeTitle() : '']);
     }
     if (!empty($formData['theme_path'])) {
         $themeFieldset->addField('theme_path', 'label', ['label' => __('Theme Path'), 'title' => __('Theme Path'), 'name' => 'theme_code']);
     }
     $themeFieldset->addField('theme_title', $this->_getFieldTextType(), ['label' => __('Theme Title'), 'title' => __('Theme Title'), 'name' => 'theme_title', 'required' => $this->_isFieldAttrRequired()]);
     if ($this->_isThemeEditable) {
         $themeFieldset->addField('preview_image', 'image', ['label' => __('Theme Preview Image'), 'title' => __('Theme Preview Image'), 'name' => 'preview', 'required' => false, 'note' => $this->_getPreviewImageNote(), 'theme' => $theme]);
     } elseif ($theme->hasPreviewImage()) {
         $themeFieldset->addField('preview_image', 'note', ['label' => __('Theme Preview Image'), 'title' => __('Theme Preview Image'), 'name' => 'preview', 'after_element_html' => '<a href="' . $theme->getThemeImage()->getPreviewImageUrl() . '" onclick="imagePreview(\'theme_preview_image\'); return false;">' . '<img width="50" src="' . $theme->getThemeImage()->getPreviewImageUrl() . '" id="theme_preview_image" /></a>']);
     }
     return $this;
 }
Пример #20
0
 /**
  * Assign theme to the stores
  *
  * @param \Magento\Framework\View\Design\ThemeInterface $theme
  * @param array $stores
  * @param string $scope
  * @return $this
  */
 public function assignToStore($theme, array $stores = array(), $scope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES)
 {
     $isReassigned = false;
     $this->_unassignThemeFromStores($theme->getId(), $stores, $scope, $isReassigned);
     if ($this->_storeManager->isSingleStoreMode()) {
         $this->_assignThemeToDefaultScope($theme->getId(), $isReassigned);
     } else {
         $this->_assignThemeToStores($theme->getId(), $stores, $scope, $isReassigned);
     }
     if ($isReassigned) {
         $this->_configCache->clean();
         $this->_layoutCache->clean();
     }
     $this->_eventManager->dispatch('assign_theme_to_stores_after', array('stores' => $stores, 'scope' => $scope, 'theme' => $theme));
     return $this;
 }
Пример #21
0
 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'));
 }
Пример #22
0
 /**
  * Pass data to the Toolbar panel blocks that is needed for rendering
  *
  * @param ThemeInterface $theme
  * @param ThemeInterface $editableTheme
  * @param string $mode
  * @return $this
  */
 protected function _configureToolbarBlocks($theme, $editableTheme, $mode)
 {
     /** @var $toolbarBlock \Magento\DesignEditor\Block\Adminhtml\Editor\Toolbar\Buttons */
     $toolbarBlock = $this->_view->getLayout()->getBlock('design_editor_toolbar_buttons');
     $toolbarBlock->setThemeId($editableTheme->getId())->setVirtualThemeId($theme->getId())->setMode($mode);
     /** @var $saveButtonBlock \Magento\DesignEditor\Block\Adminhtml\Editor\Toolbar\Buttons\Save */
     $saveButtonBlock = $this->_view->getLayout()->getBlock('design_editor_toolbar_buttons_save');
     if ($saveButtonBlock) {
         $saveButtonBlock->setTheme($theme)->setMode($mode)->setHasThemeAssigned($this->_customizationConfig->hasThemeAssigned());
     }
     /** @var $saveButtonBlock \Magento\DesignEditor\Block\Adminhtml\Editor\Toolbar\Buttons\Edit */
     $editButtonBlock = $this->_view->getLayout()->getBlock('design_editor_toolbar_buttons_edit');
     if ($editButtonBlock) {
         $editButtonBlock->setTheme($editableTheme);
     }
     return $this;
 }
Пример #23
0
 /**
  * Retrieve files
  *
  * @param \Magento\Framework\View\Design\ThemeInterface $theme
  * @param string $filePath
  * @return \Magento\Framework\View\File[]
  */
 public function getFiles(ThemeInterface $theme, $filePath)
 {
     $namespace = $module = '*';
     $themePath = $theme->getFullPath();
     $files = $this->directory->search("{$themePath}/{$namespace}_{$module}/{$this->subDir}{$filePath}");
     $result = [];
     $pattern = "#/(?<moduleName>[^/]+)/{$this->subDir}"
         . $this->pathPatternHelper->translatePatternFromGlob($filePath) . "$#i";
     foreach ($files as $file) {
         $filename = $this->directory->getAbsolutePath($file);
         if (!preg_match($pattern, $filename, $matches)) {
             continue;
         }
         $result[] = $this->fileFactory->create($filename, $matches['moduleName'], $theme);
     }
     return $result;
 }
Пример #24
0
 /**
  * Calculate unique identifier for a view file
  *
  * @return string
  */
 public function getFileIdentifier()
 {
     if (null === $this->identifier) {
         $theme = $this->getTheme() ? '|theme:' . $this->theme->getFullPath() : '';
         $this->identifier = ($this->isBase ? 'base' : '') . $theme . '|module:' . $this->getModule() . '|file:' . $this->getName();
     }
     return $this->identifier;
 }
Пример #25
0
 /**
  * Retrieve files
  *
  * @param ThemeInterface $theme
  * @param string $filePath
  * @return \Magento\Framework\View\File[]
  * @throws \UnexpectedValueException
  */
 public function getFiles(ThemeInterface $theme, $filePath)
 {
     $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($this->subDir . $filePath);
     $result = [];
     foreach ($files as $file) {
         $filename = $themeDir->getAbsolutePath($file);
         $result[] = $this->fileFactory->create($filename, null, $theme);
     }
     return $result;
 }
Пример #26
0
 /**
  * @param string $area
  * @param string $themePath
  * @param string $locale
  * @param string $module
  * @param array $files
  *
  * @dataProvider getFromCacheDataProvider
  */
 public function testGetFromCache($area, $themePath, $locale, $module, array $files)
 {
     if (isset($params['theme'])) {
         $this->theme->expects($this->any())->method('getThemePath')->will($this->returnValue($params['theme']));
         $params['theme'] = $this->theme;
     } else {
         $this->theme->expects($this->never())->method('getThemePath');
     }
     $cachedSections = ['type:file|area:frontend|theme:magento_theme|locale:en_US' => ['module:Magento_Module|file:file.ext' => 'one/file.ext', 'module:Magento_Module|file:other_file.ext' => 'one/other_file.ext', 'module:|file:file.ext' => 'two/file.ext', 'module:|file:other_file.ext' => 'two/other_file.ext'], 'type:file|area:frontend|theme:magento_theme|locale:' => ['module:Magento_Module|file:file.ext' => 'three/file.ext', 'module:Magento_Module|file:other_file.ext' => 'four/other_file.ext'], 'type:file|area:frontend|theme:|locale:en_US' => ['module:Magento_Module|file:file.ext' => 'five/file.ext', 'module:Magento_Module|file:other_file.ext' => 'five/other_file.ext'], 'type:file|area:|theme:magento_theme|locale:en_US' => ['module:Magento_Module|file:file.ext' => 'seven/file.ext', 'module:Magento_Module|file:other_file.ext' => 'other_file.ext']];
     $this->cache->expects($this->once())->method('load')->will($this->returnCallback(function ($sectionId) use($cachedSections) {
         if (!isset($cachedSections[$sectionId])) {
             return false;
         }
         return json_encode($cachedSections[$sectionId]);
     }));
     foreach ($files as $requested => $expected) {
         $actual = $this->object->getFromCache('file', $requested, $area, $themePath, $locale, $module);
         $this->assertSame($expected, $actual);
     }
 }
Пример #27
0
 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]);
 }
Пример #28
0
    /**
     * Retrieve files
     *
     * @param \Magento\Framework\View\Design\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();
        $searchPattern = "{$themePath}/{$namespace}_{$module}/{$this->subDir}*/*/{$filePath}";
        $files = $this->directory->search($searchPattern);

        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 = $this->directory->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;
    }
Пример #29
0
 /**
  *
  * @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));
 }
Пример #30
0
 /**
  * Get CSS files of a given theme
  *
  * Returns an associative array of local assets with FileId used as keys:
  * array('Magento_Catalog::widgets.css' => \Magento\Framework\View\Asset\LocalInterface)
  * The array will be sorted by keys
  *
  * @param \Magento\Framework\View\Design\ThemeInterface $theme
  * @return \Magento\Framework\View\Asset\LocalInterface[]
  */
 public function getCssAssets($theme)
 {
     /** @var $layoutProcessor \Magento\Framework\View\Layout\ProcessorInterface */
     $layoutProcessor = $this->_layoutProcessorFactory->create(['theme' => $theme]);
     $layoutElement = $layoutProcessor->getFileLayoutUpdatesXml();
     /**
      * XPath selector to get CSS files from layout added for HEAD block directly
      */
     $xpathSelectorBlocks = '//block[@class="Magento\\Theme\\Block\\Html\\Head"]' . '/block[@class="Magento\\Theme\\Block\\Html\\Head\\Css"]/arguments/argument[@name="file"]';
     /**
      * XPath selector to get CSS files from layout added for HEAD block using reference
      */
     $xpathSelectorRefs = '//referenceBlock[@name="head"]' . '/block[@class="Magento\\Theme\\Block\\Html\\Head\\Css"]/arguments/argument[@name="file"]';
     $elements = array_merge($layoutElement->xpath($xpathSelectorBlocks) ?: [], $layoutElement->xpath($xpathSelectorRefs) ?: []);
     $params = ['area' => $theme->getArea(), 'themeModel' => $theme];
     $result = [];
     foreach ($elements as $fileId) {
         $fileId = (string) $fileId;
         $result[$fileId] = $this->_assetRepo->createAsset($fileId, $params);
     }
     ksort($result);
     return $result;
 }