Ejemplo n.º 1
0
 /**
  * Deploy a static view file
  *
  * @param string $filePath
  * @param string $area
  * @param string $themePath
  * @param string $locale
  * @param string $module
  * @return void
  */
 private function deployFile($filePath, $area, $themePath, $locale, $module)
 {
     $requestedPath = $filePath;
     if (substr($filePath, -5) == '.less') {
         $requestedPath = preg_replace('/.less$/', '.css', $filePath);
     }
     $logMessage = "Processing file '{$filePath}' for area '{$area}', theme '{$themePath}', locale '{$locale}'";
     if ($module) {
         $logMessage .= ", module '{$module}'";
     }
     $this->logger->logDebug($logMessage);
     try {
         $asset = $this->assetRepo->createAsset($requestedPath, ['area' => $area, 'theme' => $themePath, 'locale' => $locale, 'module' => $module]);
         $asset = $this->minifyService->getAssets([$asset], true)[0];
         $this->logger->logDebug("\tDeploying the file to '{$asset->getPath()}'", '.');
         if ($this->isDryRun) {
             $asset->getContent();
         } else {
             $this->assetPublisher->publish($asset);
             $this->bundleManager->addAsset($asset);
         }
         $this->count++;
     } catch (\Magento\Framework\View\Asset\File\NotFoundException $e) {
         // File was not found by Fallback (possibly because it's wrong context for it) - there is nothing to publish
         $this->logger->logDebug("\tNotice: Could not find file '{$filePath}'. This file may not be relevant for the theme or area.");
     } catch (\Less_Exception_Compiler $e) {
         $this->logger->logDebug("\tNotice: Could not parse LESS file '{$filePath}'. " . "This may indicate that the file is incomplete, but this is acceptable. " . "The file '{$filePath}' will be combined with another LESS file.");
     } catch (\Exception $e) {
         $this->logger->logError($e->getMessage() . " ({$logMessage})");
         $this->logger->logDebug((string) $e);
         $this->errorCount++;
     }
 }
Ejemplo n.º 2
0
 public function testGetAssetsInvalidAdapter()
 {
     $this->setExpectedException('\\Magento\\Framework\\Exception', 'Invalid adapter: \'stdClass\'. Expected: \\Magento\\Framework\\Code\\Minifier\\AdapterInterface');
     $asset = $this->getMockForAbstractClass('Magento\\Framework\\View\\Asset\\LocalInterface');
     $asset->expects($this->once())->method('getContentType')->will($this->returnValue('js'));
     $this->_config->expects($this->once())->method('isAssetMinification')->with('js')->will($this->returnValue(true));
     $this->_config->expects($this->once())->method('getAssetMinificationAdapter')->with('js')->will($this->returnValue('StdClass'));
     $obj = new \StdClass();
     $this->_objectManager->expects($this->once())->method('get')->with('StdClass')->will($this->returnValue($obj));
     $this->_model->getAssets([$asset]);
 }
Ejemplo n.º 3
0
 /**
  * Returns rendered HTML for an Asset Group
  *
  * @param \Magento\Framework\View\Asset\PropertyGroup $group
  * @return string
  */
 protected function renderAssetGroup(\Magento\Framework\View\Asset\PropertyGroup $group)
 {
     $groupAssets = $this->assetMinifyService->getAssets($group->getAll());
     $groupAssets = $this->processMerge($groupAssets, $group);
     $attributes = $this->getGroupAttributes($group);
     $attributes = $this->addDefaultAttributes($group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE), $attributes);
     $groupTemplate = $this->getAssetTemplate($group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE), $attributes);
     $groupHtml = $this->renderAssetHtml($groupTemplate, $groupAssets);
     $groupHtml = $this->processIeCondition($groupHtml, $group);
     return $groupHtml;
 }
Ejemplo n.º 4
0
 /**
  * Render HTML for the added head items
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  *
  * @return string
  */
 public function getCssJsHtml()
 {
     foreach ($this->getLayout()->getChildBlocks($this->getNameInLayout()) as $block) {
         /** @var $block \Magento\Framework\View\Element\AbstractBlock */
         if ($block instanceof \Magento\Theme\Block\Html\Head\AssetBlockInterface) {
             /** @var \Magento\Framework\View\Asset\AssetInterface $asset */
             $asset = $block->getAsset();
             $this->_pageAssets->add($block->getNameInLayout(), $asset, (array) $block->getProperties());
         }
     }
     $result = '';
     /** @var $group \Magento\Framework\View\Asset\PropertyGroup */
     foreach ($this->_pageAssets->getGroups() as $group) {
         $contentType = $group->getProperty(\Magento\Framework\View\Asset\GroupedCollection::PROPERTY_CONTENT_TYPE);
         $canMerge = $group->getProperty(\Magento\Framework\View\Asset\GroupedCollection::PROPERTY_CAN_MERGE);
         $attributes = $group->getProperty('attributes');
         $ieCondition = $group->getProperty('ie_condition');
         $flagName = $group->getProperty('flag_name');
         if ($flagName && !$this->getData($flagName)) {
             continue;
         }
         $groupAssets = $group->getAll();
         $groupAssets = $this->_assetMinifyService->getAssets($groupAssets);
         if ($canMerge && count($groupAssets) > 1) {
             $groupAssets = $this->_assetMergeService->getMergedAssets($groupAssets, $contentType);
         }
         if (!empty($attributes)) {
             if (is_array($attributes)) {
                 $attributesString = '';
                 foreach ($attributes as $name => $value) {
                     $attributesString .= ' ' . $name . '="' . $this->escapeHtml($value) . '"';
                 }
                 $attributes = $attributesString;
             } else {
                 $attributes = ' ' . $attributes;
             }
         }
         if ($contentType == 'js') {
             $groupTemplate = '<script' . $attributes . ' type="text/javascript" src="%s"></script>' . "\n";
         } else {
             if ($contentType == 'css') {
                 $attributes = ' rel="stylesheet" type="text/css"' . ($attributes ?: ' media="all"');
             }
             $groupTemplate = '<link' . $attributes . ' href="%s" />' . "\n";
         }
         $groupHtml = $this->_renderHtml($groupTemplate, $groupAssets);
         if (!empty($ieCondition)) {
             $groupHtml = '<!--[if ' . $ieCondition . ']>' . "\n" . $groupHtml . '<![endif]-->' . "\n";
         }
         $result .= $groupHtml;
     }
     return $result;
 }
Ejemplo n.º 5
0
    /**
     * Deploy a static view file
     *
     * @param string $filePath
     * @param string $area
     * @param string $themePath
     * @param string $locale
     * @param string $module
     * @return void
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    private function deployFile($filePath, $area, $themePath, $locale, $module)
    {
        $requestedPath = $filePath;
        if (substr($filePath, -5) == '.less') {
            $requestedPath = preg_replace('/.less$/', '.css', $filePath);
        }
        $logMessage = "Processing file '$filePath' for area '$area', theme '$themePath', locale '$locale'";
        if ($module) {
            $logMessage .= ", module '$module'";
        }

        if ($this->output->isVeryVerbose()) {
            $this->output->writeln($logMessage);
        }

        try {
            $asset = $this->assetRepo->createAsset(
                $requestedPath,
                ['area' => $area, 'theme' => $themePath, 'locale' => $locale, 'module' => $module]
            );
            $asset = $this->minifyService->getAssets([$asset], true)[0];
            if ($this->output->isVeryVerbose()) {
                $this->output->writeln("\tDeploying the file to '{$asset->getPath()}'");
            } else {
                $this->output->write('.');
            }
            if ($this->isDryRun) {
                $asset->getContent();
            } else {
                $this->assetPublisher->publish($asset);
                $this->bundleManager->addAsset($asset);
            }
            $this->count++;
        } catch (\Less_Exception_Compiler $e) {
            $this->verboseLog(
                "\tNotice: Could not parse LESS file '$filePath'. "
                . "This may indicate that the file is incomplete, but this is acceptable. "
                . "The file '$filePath' will be combined with another LESS file."
            );
            $this->verboseLog("\tCompiler error: " . $e->getMessage());
        } catch (\Exception $e) {
            $this->output->writeln($e->getMessage() . " ($logMessage)");
            $this->verboseLog($e->getTraceAsString());
            $this->errorCount++;
        }
    }
Ejemplo n.º 6
0
 /**
  * Finds requested resource and provides it to the client
  *
  * @return \Magento\Framework\App\ResponseInterface
  * @throws \Exception
  */
 public function launch()
 {
     // disabling profiling when retrieving static resource
     \Magento\Framework\Profiler::reset();
     $appMode = $this->state->getMode();
     if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) {
         $this->response->setHttpResponseCode(404);
     } else {
         $path = $this->request->get('resource');
         $params = $this->parsePath($path);
         $this->state->setAreaCode($params['area']);
         $this->objectManager->configure($this->configLoader->load($params['area']));
         $file = $params['file'];
         unset($params['file']);
         $asset = $this->assetRepo->createAsset($file, $params);
         $asset = $this->minifyService->getAssets([$asset], true)[0];
         $this->response->setFilePath($asset->getSourceFile());
         $this->publisher->publish($asset);
     }
     return $this->response;
 }