Exemple #1
0
 /**
  * Perform necessary preprocessing and materialization when the specified asset is requested
  *
  * Returns an array of two elements:
  * - directory code where the file is supposed to be found
  * - relative path to the file
  *
  * Automatically caches the obtained successful results or returns false if source file was not found
  *
  * @param LocalInterface $asset
  * @return array|bool
  */
 private function preProcess(LocalInterface $asset)
 {
     $sourceFile = $this->findSourceFile($asset);
     if (!$sourceFile) {
         return false;
     }
     $dirCode = \Magento\Framework\App\Filesystem::ROOT_DIR;
     $path = $this->rootDir->getRelativePath($sourceFile);
     $cacheId = $path . ':' . $asset->getPath();
     $cached = $this->cache->load($cacheId);
     if ($cached) {
         return unserialize($cached);
     }
     $chain = new \Magento\Framework\View\Asset\PreProcessor\Chain($asset, $this->rootDir->readFile($path), $this->getContentType($path));
     $preProcessors = $this->preProcessorPool->getPreProcessors($chain->getOrigContentType(), $chain->getTargetContentType());
     foreach ($preProcessors as $processor) {
         $processor->process($chain);
     }
     $chain->assertValid();
     if ($chain->isChanged()) {
         $dirCode = \Magento\Framework\App\Filesystem::VAR_DIR;
         $path = self::TMP_MATERIALIZATION_DIR . '/source/' . $asset->getPath();
         $this->varDir->writeFile($path, $chain->getContent());
     }
     $result = array($dirCode, $path);
     $this->cache->save(serialize($result), $cacheId);
     return $result;
 }
Exemple #2
0
    /**
     * Perform necessary preprocessing and materialization when the specified asset is requested
     *
     * Returns an array of two elements:
     * - directory code where the file is supposed to be found
     * - relative path to the file
     *
     * Automatically caches the obtained successful results or returns false if source file was not found
     *
     * @param LocalInterface $asset
     * @return array|bool
     */
    private function preProcess(LocalInterface $asset)
    {
        $sourceFile = $this->findSourceFile($asset);
        if ($sourceFile !== false) {
            $path = $this->rootDir->getRelativePath($sourceFile);
        } else {
            // No original file, the resulting file may be generated by a pre-processor
            $path = false;
        }
        $cacheId = $path . ':' . $asset->getPath();
        $cached = $this->cache->load($cacheId);
        if ($cached) {
            return unserialize($cached);
        }

        $chain = $this->createChain($asset, $path);
        $this->preProcessorPool->process($chain);
        $chain->assertValid();
        $dirCode = DirectoryList::ROOT;
        if ($chain->isChanged()) {
            $dirCode = DirectoryList::VAR_DIR;
            $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath();
            $this->varDir->writeFile($path, $chain->getContent());
        }
        $result = [$dirCode, $path];
        $this->cache->save(serialize($result), $cacheId);
        return $result;
    }
Exemple #3
0
    /**
     * Perform necessary preprocessing and materialization when the specified asset is requested
     *
     * Returns an array of two elements:
     * - directory code where the file is supposed to be found
     * - relative path to the file
     *
     * Automatically caches the obtained successful results or returns false if source file was not found
     *
     * @param LocalInterface $asset
     * @return array|bool
     */
    private function preProcess(LocalInterface $asset)
    {
        $sourceFile = $this->findSourceFile($asset);
        $dirCode = DirectoryList::ROOT;
        $path = $this->rootDir->getRelativePath($sourceFile);
        $cacheId = $path . ':' . $asset->getPath();
        $cached = $this->cache->load($cacheId);
        if ($cached) {
            return unserialize($cached);
        }

        $origContent = $path ? $this->rootDir->readFile($path) : '';
        $origContentType = $this->getContentType($path) ?: $asset->getContentType();

        $chain = $this->chainFactory->create(
            [
                'asset' => $asset,
                'origContent' => $origContent,
                'origContentType' => $origContentType,
                'origAssetPath' => $path
            ]
        );

        $this->preProcessorPool->process($chain);
        $chain->assertValid();
        if ($chain->isChanged()) {
            $dirCode = DirectoryList::VAR_DIR;
            $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath();
            $this->varDir->writeFile($path, $chain->getContent());
        }
        $result = [$dirCode, $path];
        $this->cache->save(serialize($result), $cacheId);
        return $result;
    }