/** * {@inheritdoc} */ public function merge(array $assetsToMerge, \Magento\Framework\View\Asset\LocalInterface $resultAsset) { $dir = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem::STATIC_VIEW_DIR); if (!$dir->isExist($resultAsset->getPath())) { $this->strategy->merge($assetsToMerge, $resultAsset); } }
/** * Publish the asset * * @param Asset\LocalInterface $asset * @return bool */ private function publishAsset(Asset\LocalInterface $asset) { $dir = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::STATIC_VIEW_DIR); $rootDir = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::ROOT_DIR); $source = $rootDir->getRelativePath($asset->getSourceFile()); $destination = $asset->getPath(); return $rootDir->copyFile($source, $destination, $dir); }
/** * Whether the strategy can be applied * * @param Asset\LocalInterface $asset * @return bool */ public function isSupported(Asset\LocalInterface $asset) { $sourceParts = explode('/', $asset->getSourceFile()); if (in_array(DirectoryList::TMP_MATERIALIZATION_DIR, $sourceParts)) { return false; } return true; }
/** * Publish the asset * * @param Asset\LocalInterface $asset * @return bool */ private function publishAsset(Asset\LocalInterface $asset) { $targetDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW); $rootDir = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT); $source = $rootDir->getRelativePath($asset->getSourceFile()); $destination = $asset->getPath(); $strategy = $this->materializationStrategyFactory->create($asset); return $strategy->publishFile($rootDir, $targetDir, $source, $destination); }
/** * @param \Magento\Framework\View\Asset\LocalInterface $asset * @param string $origContent * @param string $origContentType */ public function __construct(\Magento\Framework\View\Asset\LocalInterface $asset, $origContent, $origContentType) { $this->asset = $asset; $this->origContent = $origContent; $this->content = $origContent; $this->origContentType = $origContentType; $this->contentType = $origContentType; $this->targetContentType = $asset->getContentType(); }
/** * Publish the asset * * @param Asset\LocalInterface $asset * @return bool */ private function publishAsset(Asset\LocalInterface $asset) { $targetDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW); $fullSource = $asset->getSourceFile(); $source = basename($fullSource); $sourceDir = $this->writeFactory->create(dirname($fullSource)); $destination = $asset->getPath(); $strategy = $this->materializationStrategyFactory->create($asset); return $strategy->publishFile($sourceDir, $targetDir, $source, $destination); }
public function testMergeCss() { $this->resultAsset->expects($this->exactly(3))->method('getPath')->will($this->returnValue('foo/result')); $this->resultAsset->expects($this->any())->method('getContentType')->will($this->returnValue('css')); $assets = $this->prepareAssetsToMerge(['one', 'two']); $this->cssUrlResolver->expects($this->exactly(2))->method('relocateRelativeUrls')->will($this->onConsecutiveCalls('1', '2')); $this->cssUrlResolver->expects($this->once())->method('aggregateImportDirectives')->with('12')->will($this->returnValue('1020')); $this->writeDir->expects($this->once())->method('writeFile')->with('foo/result', '1020'); $this->object->merge($assets, $this->resultAsset); }
/** * @param LocalInterface $asset * @param string $origContent * @param string $origContentType */ public function __construct(LocalInterface $asset, $origContent, $origContentType) { $this->asset = $asset; $this->origContent = $origContent; $this->content = $origContent; $this->origContentType = $origContentType; $this->contentType = $origContentType; $this->targetContentType = $asset->getContentType(); $this->targetAssetPath = $asset->getPath(); }
protected function prepareAttemptToMinifyMock($fileExists, $rootDirExpectations = true, $originalExists = true) { $this->asset->expects($this->atLeastOnce())->method('getPath')->will($this->returnValue('test/admin.js')); $this->asset->expects($this->atLeastOnce())->method('getSourceFile')->will($this->returnValue('/foo/bar/test/admin.js')); if ($rootDirExpectations) { $this->rootDir->expects($this->once())->method('getRelativePath')->with('/foo/bar/test/admin.min.js')->will($this->returnValue('test/admin.min.js')); $this->rootDir->expects($this->once())->method('isExist')->with('test/admin.min.js')->will($this->returnValue(false)); } $this->baseUrl->expects($this->once())->method('getBaseUrl')->will($this->returnValue('http://example.com/')); $this->staticViewDir->expects($this->exactly(2 - intval($originalExists)))->method('isExist')->will($this->returnValue($fileExists)); }
/** * Convert module notation to a path relative to the specified asset * * For example, the asset is Foo_Bar/styles/style.css and it refers to Bar_Baz::images/logo.gif * (i.e. url(Bar_Baz::images/logo.gif)) * The result will be ../../Bar_Baz/images/logo.gif * * @param Asset\LocalInterface $thisAsset * @param string $relatedFileId * @return string */ public function convertModuleNotationToPath(Asset\LocalInterface $thisAsset, $relatedFileId) { if (false === strpos($relatedFileId, Asset\Repository::FILE_ID_SEPARATOR)) { return $relatedFileId; } $thisPath = $thisAsset->getPath(); $relatedAsset = $this->assetRepo->createSimilar($relatedFileId, $thisAsset); $relatedPath = $relatedAsset->getPath(); $offset = FileSystem::offsetPath($relatedPath, $thisPath); return FileSystem::normalizePath($offset . '/' . basename($relatedPath)); }
public function testAddAsset() { $dirRead = $this->getMockBuilder('Magento\\Framework\\Filesystem\\Directory\\ReadInterface')->disableOriginalConstructor()->getMock(); $context = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\File\\FallbackContext')->disableOriginalConstructor()->getMock(); $configView = $this->getMockBuilder('Magento\\Framework\\Config\\View')->disableOriginalConstructor()->getMock(); $this->filesystem->expects($this->once())->method('getDirectoryRead')->with(\Magento\Framework\App\Filesystem\DirectoryList::APP)->willReturn($dirRead); $this->asset->expects($this->atLeastOnce())->method('getSourceFile')->willReturn('/path/to/source/file.min.js'); $this->asset->expects($this->atLeastOnce())->method('getContentType')->willReturn('js'); $this->asset->expects($this->once())->method('getPath')->willReturn('/path/to/file.js'); $this->asset->expects($this->atLeastOnce())->method('getContext')->willReturn($context); $this->bundleConfig->expects($this->atLeastOnce())->method('getConfig')->with($context)->willReturn($configView); $configView->expects($this->once())->method('getExcludedFiles')->willReturn([]); $configView->expects($this->once())->method('getExcludedDir')->willReturn([]); $this->bundle->expects($this->once())->method('addAsset')->with($this->asset); $this->assertTrue($this->manager->addAsset($this->asset)); }
/** * {@inheritdoc} */ public function merge(array $assetsToMerge, \Magento\Framework\View\Asset\LocalInterface $resultAsset) { $sourceDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT); $mTime = null; /** @var \Magento\Framework\View\Asset\MergeableInterface $asset */ foreach ($assetsToMerge as $asset) { $mTime .= $sourceDir->stat($sourceDir->getRelativePath($asset->getSourceFile()))['mtime']; } if (null === $mTime) { return; // nothing to merge } $dat = $resultAsset->getPath() . '.dat'; $targetDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW); if (!$targetDir->isExist($dat) || strcmp($mTime, $targetDir->readFile($dat)) !== 0) { $this->strategy->merge($assetsToMerge, $resultAsset); $targetDir->writeFile($dat, $mTime); } }
/** * @param int $mtimeOrig * @param int $mtimeMinified * @param bool $isMinifyExpected * @dataProvider minifyMtimeDataProvider */ public function testMinifyMtime($mtimeOrig, $mtimeMinified, $isMinifyExpected) { $this->prepareAttemptToMinifyMock(true, false); $model = new Minified($this->_asset, $this->_logger, $this->_filesystem, $this->_baseUrl, $this->_adapter, Minified::MTIME); $this->_rootDir->expects($this->any())->method('getRelativePath')->will($this->returnValueMap([['/foo/bar/test/library.min.js', 'test/library.min.js'], ['/foo/bar/test/library.js', 'test/library.js']])); $this->_rootDir->expects($this->once())->method('isExist')->with('test/library.min.js')->will($this->returnValue(false)); $this->_rootDir->expects($this->once())->method('stat')->with('test/library.js')->will($this->returnValue(['mtime' => $mtimeOrig])); $this->_staticViewDir->expects($this->once())->method('stat')->with($this->anything())->will($this->returnValue(['mtime' => $mtimeMinified])); if ($isMinifyExpected) { $this->_asset->expects($this->once())->method('getContent')->will($this->returnValue('content')); $this->_adapter->expects($this->once())->method('minify')->with('content')->will($this->returnValue('mini')); $this->_staticViewDir->expects($this->once())->method('writeFile')->with($this->anything(), 'mini'); } else { $this->_adapter->expects($this->never())->method('minify'); } $this->assertStringMatchesFormat('%s_library.min.js', $model->getFilePath()); }
/** * Perform actual minification * * @return void */ protected function minify() { $isExists = $this->staticViewDir->isExist($this->path); if (!$isExists) { $shouldMinify = true; } elseif ($this->strategy == self::FILE_EXISTS) { $shouldMinify = false; } else { $origlFile = $this->rootDir->getRelativePath($this->originalAsset->getSourceFile()); $origMtime = $this->rootDir->stat($origlFile)['mtime']; $minMtime = $this->staticViewDir->stat($this->path)['mtime']; $shouldMinify = $origMtime != $minMtime; } if ($shouldMinify) { $content = $this->adapter->minify($this->originalAsset->getContent()); $this->staticViewDir->writeFile($this->path, $content); } }
public function testAddAsset() { $context = $this->getMockBuilder('Magento\Framework\View\Asset\File\FallbackContext') ->disableOriginalConstructor() ->getMock(); $configView = $this->getMockBuilder('Magento\Framework\Config\View') ->disableOriginalConstructor() ->getMock(); $this->asset->expects($this->atLeastOnce()) ->method('getContentType') ->willReturn('js'); $this->asset->expects($this->once()) ->method('getSourceFile') ->willReturn('/source/file'); $this->asset->expects($this->atLeastOnce()) ->method('getFilePath') ->willReturn('file/path.js'); $this->assetConfig->expects($this->once()) ->method('isAssetMinification') ->willReturn(false); $this->asset->expects($this->atLeastOnce()) ->method('getContext') ->willReturn($context); $this->bundleConfig ->expects($this->atLeastOnce()) ->method('getConfig') ->with($context) ->willReturn($configView); $configView->expects($this->once()) ->method('getExcludedFiles') ->willReturn([]); $configView->expects($this->once()) ->method('getExcludedDir') ->willReturn([]); $this->bundle->expects($this->once()) ->method('addAsset') ->with($this->asset); $this->assertTrue($this->manager->addAsset($this->asset)); }
/** * Get theme model based on the information from asset * * @param LocalInterface $asset * @return \Magento\Framework\View\Design\ThemeInterface */ protected function getTheme(LocalInterface $asset) { $context = $asset->getContext(); if ($context instanceof FallbackContext) { return $this->themeList->getThemeByFullPath($context->getAreaCode() . '/' . $context->getThemePath()); } return $this->design->getDesignTheme(); }
/** * @param LocalInterface $asset * @return bool */ protected function isValidType(LocalInterface $asset) { $type = $asset->getContentType(); if (!in_array($type, self::$availableTypes)) { return false; } return true; }
/** * Creates a chain for pre-processing * * @param LocalInterface $asset * @param string|bool $dir * @param string|bool $path * @return PreProcessor\Chain */ private function createChain(LocalInterface $asset, $dir, $path) { if ($path) { $origContent = $this->readFactory->create($dir)->readFile($path); $origContentType = $this->getContentType($path); } else { $origContent = ''; $origContentType = $asset->getContentType(); } $chain = $this->chainFactory->create(['asset' => $asset, 'origContent' => $origContent, 'origContentType' => $origContentType, 'origAssetPath' => $dir . '/' . $path]); return $chain; }
/** * @param LocalInterface $asset * @return bool */ protected function isValidType(LocalInterface $asset) { $type = $asset->getContentType(); if (!in_array($type, self::$availableTypes)) { return false; } if ($type == self::ASSET_TYPE_HTML) { return $asset->getModule() !== ''; } return true; }
/** * Create a file asset similar to an existing local asset by using its context * * @param string $fileId * @param LocalInterface $similarTo * @return File */ public function createSimilar($fileId, LocalInterface $similarTo) { list($module, $filePath) = self::extractModule($fileId); if (!$module) { $module = $similarTo->getModule(); } return $this->fileFactory->create(['source' => $this->assetSource, 'context' => $similarTo->getContext(), 'filePath' => $filePath, 'module' => $module, 'contentType' => $this->assetSource->getContentType($filePath)]); }
/** * Build asset key * * @param LocalInterface $asset * @return string */ protected function getAssetKey(LocalInterface $asset) { $result = ($asset->getModule() == '' ? '' : $asset->getModule() . '/') . $asset->getFilePath(); $result = $this->minification->addMinifiedSign($result); return $result; }
protected function setUp() { $this->asset = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\LocalInterface'); $this->asset->expects($this->once())->method('getContentType')->will($this->returnValue('assetType')); $this->object = new Chain($this->asset, 'origContent', 'origType'); }
/** * Find asset file by simply appending its path to the directory in context * * @param LocalInterface $asset * @param \Magento\Framework\View\Asset\File\Context $context * @return string */ private function findFile(LocalInterface $asset, \Magento\Framework\View\Asset\File\Context $context) { $dir = $this->filesystem->getDirectoryRead($context->getBaseDirType()); Simple::assertFilePathFormat($asset->getFilePath()); return $dir->getAbsolutePath($asset->getPath()); }
/** * Get theme model based on the information from asset * * @param LocalInterface $asset * @return \Magento\Framework\View\Design\ThemeInterface */ protected function getTheme(LocalInterface $asset) { $context = $asset->getContext(); if ($context instanceof FallbackContext) { return $this->themeProvider->getThemeModel($context->getThemePath(), $context->getAreaCode()); } return $this->design->getDesignTheme(); }
/** * Create a file asset similar to an existing local asset by using its context * * @param string $fileId * @param LocalInterface $similarTo * @return File */ public function createSimilar($fileId, LocalInterface $similarTo) { list($module, $filePath) = self::extractModule($fileId); if (!$module) { $module = $similarTo->getModule(); } return new File($this->assetSource, $similarTo->getContext(), $filePath, $module, $this->assetSource->getContentType($filePath)); }
/** * Process the resulting asset after merging content is done * * @param Asset\LocalInterface $result * @param string $content * @return string */ private function preProcessMergeResult(Asset\LocalInterface $result, $content) { if ($result->getContentType() == 'css') { $content = $this->cssUrlResolver->aggregateImportDirectives($content); } return $content; }
/** * Build asset key * * @param LocalInterface $asset * @return string */ protected function getAssetKey(LocalInterface $asset) { return $asset->getModule() == '' ? $asset->getFilePath() : $asset->getModule() . '/' . $asset->getFilePath(); }