Beispiel #1
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;
 }
Beispiel #2
0
 public function testGetGroups()
 {
     $cssAsset = new \Magento\Framework\View\Asset\Remote('http://127.0.0.1/style.css', 'css');
     $jsAsset = new \Magento\Framework\View\Asset\Remote('http://127.0.0.1/script.js', 'js');
     $jsAssetAllowingMerge = $this->getMockForAbstractClass('Magento\\Framework\\View\\Asset\\MergeableInterface');
     $jsAssetAllowingMerge->expects($this->any())->method('getContentType')->will($this->returnValue('js'));
     // assets with identical properties should be grouped together
     $this->_object->add('css_asset_one', $cssAsset, ['property' => 'test_value']);
     $this->_object->add('css_asset_two', $cssAsset, ['property' => 'test_value']);
     // assets with identical properties but empty properties should be grouped together
     $this->_object->add('css_asset_four', $cssAsset, ['property' => 'test_value2', 'junk1' => null]);
     $this->_object->add('css_asset_five', $cssAsset, ['property' => 'test_value2', 'junk2' => '']);
     // assets with different properties should go to different groups
     $this->_object->add('css_asset_three', $cssAsset, ['property' => 'different_value']);
     $this->_object->add('js_asset_one', $jsAsset, ['property' => 'test_value']);
     // assets with identical properties in a different order should be grouped
     $this->_object->add('js_asset_two', $jsAsset, ['property1' => 'value1', 'property2' => 'value2']);
     $this->_object->add('js_asset_three', $jsAsset, ['property2' => 'value2', 'property1' => 'value1']);
     // assets allowing merge should go to separate group regardless of having identical properties
     $this->_object->add('asset_allowing_merge', $jsAssetAllowingMerge, ['property' => 'test_value']);
     $expectedGroups = [['properties' => ['content_type' => 'unknown', 'can_merge' => false], 'assets' => ['asset' => $this->_asset]], ['properties' => ['property' => 'test_value', 'content_type' => 'css', 'can_merge' => false], 'assets' => ['css_asset_one' => $cssAsset, 'css_asset_two' => $cssAsset]], ['properties' => ['property' => 'test_value2', 'content_type' => 'css', 'can_merge' => false], 'assets' => ['css_asset_four' => $cssAsset, 'css_asset_five' => $cssAsset]], ['properties' => ['property' => 'different_value', 'content_type' => 'css', 'can_merge' => false], 'assets' => ['css_asset_three' => $cssAsset]], ['properties' => ['property' => 'test_value', 'content_type' => 'js', 'can_merge' => false], 'assets' => ['js_asset_one' => $jsAsset]], ['properties' => ['property1' => 'value1', 'property2' => 'value2', 'content_type' => 'js', 'can_merge' => false], 'assets' => ['js_asset_two' => $jsAsset, 'js_asset_three' => $jsAsset]], ['properties' => ['property' => 'test_value', 'content_type' => 'js', 'can_merge' => true], 'assets' => ['asset_allowing_merge' => $jsAssetAllowingMerge]]];
     $this->_assertGroups($expectedGroups, $this->_object->getGroups());
 }