Skip to content

Commit

Permalink
Merge pull request #73 from magento-troll/develop
Browse files Browse the repository at this point in the history
[Troll] S29 - Varnish 4 support, CSS minification and performance toolkit updates
  • Loading branch information
Roman Ganin committed Feb 2, 2015
2 parents 8876c93 + ea36628 commit 2aa22d6
Show file tree
Hide file tree
Showing 75 changed files with 9,603 additions and 3,180 deletions.
4 changes: 4 additions & 0 deletions app/code/Magento/Backend/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@
<label>Merge CSS Files</label>
<source_model>Magento\Backend\Model\Config\Source\Yesno</source_model>
</field>
<field id="minify_files" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Minify CSS Files</label>
<source_model>Magento\Backend\Model\Config\Source\Yesno</source_model>
</field>
</group>
<group id="image" translate="label" type="text" sortOrder="120" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Image Processing Settings</label>
Expand Down
26 changes: 26 additions & 0 deletions app/code/Magento/Catalog/Helper/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class Product extends \Magento\Core\Helper\Url
*/
protected $_catalogSession;

/**
* Invalidate product category indexer params
*
* @var array
*/
protected $_reindexProductCategoryIndexerData;

/**
* Invalidate price indexer params
*
Expand Down Expand Up @@ -109,6 +116,7 @@ class Product extends \Magento\Core\Helper\Url
* @param \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig
* @param string $typeSwitcherLabel
* @param array $reindexPriceIndexerData
* @param array $reindexProductCategoryIndexerData
* @param ProductRepositoryInterface $productRepository
* @param CategoryRepositoryInterface $categoryRepository
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand All @@ -124,6 +132,7 @@ public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
$typeSwitcherLabel,
$reindexPriceIndexerData,
$reindexProductCategoryIndexerData,
ProductRepositoryInterface $productRepository,
CategoryRepositoryInterface $categoryRepository
) {
Expand All @@ -137,6 +146,7 @@ public function __construct(
$this->_reindexPriceIndexerData = $reindexPriceIndexerData;
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->_reindexProductCategoryIndexerData = $reindexProductCategoryIndexerData;
parent::__construct($context, $storeManager);
}

Expand Down Expand Up @@ -169,6 +179,22 @@ public function isDataForPriceIndexerWasChanged($data)
return false;
}

/**
* Retrieve data for product category indexer update
*
* @param \Magento\Catalog\Model\Product $data
* @return boolean
*/
public function isDataForProductCategoryIndexerWasChanged(\Magento\Catalog\Model\Product $data)
{
foreach ($this->_reindexProductCategoryIndexerData['byDataChange'] as $param) {
if ($data->dataHasChangedFor($param)) {
return true;
}
}
return false;
}

/**
* Retrieve product view page url
*
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,9 @@ public function reindex()
$flatIndexer->reindexRow($this->getId());
}
}
$affectedProductIds = $this->getAffectedProductIds();
$productIndexer = $this->indexerRegistry->get(Indexer\Category\Product::INDEXER_ID);
if (!$productIndexer->isScheduled()) {
if (!$productIndexer->isScheduled() && !empty($affectedProductIds)) {
$productIndexer->reindexList($this->getPathIds());
}
}
Expand Down
10 changes: 6 additions & 4 deletions app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,12 @@ public function eavReindexCallback()
*/
public function reindex()
{
$this->_productFlatIndexerProcessor->reindexRow($this->getEntityId());
$categoryIndexer = $this->indexerRegistry->get(Indexer\Product\Category::INDEXER_ID);
if (!$categoryIndexer->isScheduled()) {
$categoryIndexer->reindexRow($this->getId());
if ($this->_catalogProduct->isDataForProductCategoryIndexerWasChanged($this) || $this->isDeleted()) {
$this->_productFlatIndexerProcessor->reindexRow($this->getEntityId());
$categoryIndexer = $this->indexerRegistry->get(Indexer\Product\Category::INDEXER_ID);
if (!$categoryIndexer->isScheduled()) {
$categoryIndexer->reindexRow($this->getId());
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/Catalog/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\Indexer\Category\Product\AbstractAction">
<plugin name="invalidate_pagecache_after_rows_reindex" type="Magento\Catalog\Plugin\Model\Indexer\Category\Product\Execute" />
<type name="Magento\Catalog\Model\Indexer\Category\Product\Action\Full">
<plugin name="invalidate_pagecache_after_full_reindex" type="Magento\Catalog\Plugin\Model\Indexer\Category\Product\Execute" />
</type>
<type name="Magento\Catalog\Model\Resource\Attribute">
<plugin name="invalidate_cache_after_rows_reindex" type="Magento\Catalog\Plugin\Model\Resource\Attribute\Save" />
<plugin name="invalidate_pagecache_after_attribute_save" type="Magento\Catalog\Plugin\Model\Resource\Attribute\Save" />
</type>
</config>
8 changes: 8 additions & 0 deletions app/code/Magento/Catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@
<item name="tax_class_id" xsi:type="string">tax_class_id</item>
</item>
</argument>
<argument name="reindexProductCategoryIndexerData" xsi:type="array">
<item name="byDataChange" xsi:type="array">
<item name="category_ids" xsi:type="string">category_ids</item>
<item name="entity_id" xsi:type="string">entity_id</item>
<item name="store_id" xsi:type="string">store_id</item>
<item name="visibility" xsi:type="string">visibility</item>
</item>
</argument>
<argument name="productRepository" xsi:type="object">Magento\Catalog\Api\ProductRepositoryInterface\Proxy</argument>
</arguments>
</type>
Expand Down
5 changes: 5 additions & 0 deletions app/code/Magento/Core/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<minify_files>0</minify_files>
<minify_adapter>Magento\Framework\Code\Minifier\Adapter\Js\Jsmin</minify_adapter>
</js>
<css>
<minify_files>0</minify_files>
<minify_adapter>Magento\Framework\Code\Minifier\Adapter\Css\CssMinifier</minify_adapter>
</css>
<image>
<default_adapter>GD2</default_adapter>
<adapters>
Expand Down Expand Up @@ -98,6 +102,7 @@
</startup>
<url>
<use_custom>0</use_custom>
<use_custom_path>0</use_custom_path>
<custom />
</url>
<security>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,39 @@ class Export extends \Magento\Backend\Block\System\Config\Form\Field
*
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
* @return string
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
/** @var \Magento\Backend\Block\Widget\Button $buttonBlock */
$buttonBlock = $this->getForm()->getLayout()->createBlock('Magento\Backend\Block\Widget\Button');

$params = ['website' => $buttonBlock->getRequest()->getParam('website')];
$params = [
'website' => $buttonBlock->getRequest()->getParam('website'),
'varnish' => $this->getVarnishVersion()
];

$url = $this->getUrl("*/PageCache/exportVarnishConfig", $params);
$data = [
'id' => 'system_full_page_cache_varnish_export_button',
'label' => __('Export VCL'),
'id' => 'system_full_page_cache_varnish_export_button_version' . $this->getVarnishVersion(),
'label' => __('Export VCL for Varnish ') . $this->getVarnishVersion(),
'onclick' => "setLocation('" . $url . "')",
];

$html = $buttonBlock->setData($data)->toHtml();
return $html;
}

/**
* Return Varnish version to this class
*
* @return int
*/
public function getVarnishVersion()
{
return 0;
}

/**
* Return PageCache TTL value from config
* to avoid saving empty field
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\PageCache\Block\System\Config\Form\Field\Export;

/**
* Class Export
*/
class Varnish3 extends \Magento\PageCache\Block\System\Config\Form\Field\Export
{
/**
* Return Varnish version to this class
*
* @return int
*/
public function getVarnishVersion()
{
return 3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\PageCache\Block\System\Config\Form\Field\Export;

/**
* Class Export
*/
class Varnish4 extends \Magento\PageCache\Block\System\Config\Form\Field\Export
{
/**
* Return Varnish version to this class
*
* @return int
*/
public function getVarnishVersion()
{
return 4;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ public function __construct(
public function execute()
{
$fileName = 'varnish.vcl';
$content = $this->config->getVclFile();
$varnishVersion = $this->getRequest()->getParam('varnish');
switch ($varnishVersion) {
case 3:
$content = $this->config->getVclFile(\Magento\PageCache\Model\Config::VARNISH_3_CONFIGURATION_PATH);
break;
default:
$content = $this->config->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH);
break;
}
return $this->fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
}
}
26 changes: 16 additions & 10 deletions app/code/Magento/PageCache/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ class Config
protected $_scopeConfig;

/**
* XML path to value for saving temporary .vcl configuration
* XML path to Varnish 3 config template path
*/
const VARNISH_CONFIGURATION_PATH = 'system/full_page_cache/varnish/path';
const VARNISH_3_CONFIGURATION_PATH = 'system/full_page_cache/varnish3/path';

/**
* XML path to Varnish 4 config template path
*/
const VARNISH_4_CONFIGURATION_PATH = 'system/full_page_cache/varnish4/path';

/**
* @var \Magento\Framework\App\Cache\StateInterface $_cacheState
Expand Down Expand Up @@ -101,11 +106,12 @@ public function getTtl()
/**
* Return generated varnish.vcl configuration file
*
* @param string $vclTemplatePath
* @return string
*/
public function getVclFile()
public function getVclFile($vclTemplatePath)
{
$data = $this->_modulesDirectory->readFile($this->_scopeConfig->getValue(self::VARNISH_CONFIGURATION_PATH));
$data = $this->_modulesDirectory->readFile($this->_scopeConfig->getValue($vclTemplatePath));
return strtr($data, $this->_getReplacements());
}

Expand All @@ -117,16 +123,16 @@ public function getVclFile()
protected function _getReplacements()
{
return [
'{{ host }}' => $this->_scopeConfig->getValue(
'/* {{ host }} */' => $this->_scopeConfig->getValue(
self::XML_VARNISH_PAGECACHE_BACKEND_HOST,
\Magento\Framework\Store\ScopeInterface::SCOPE_STORE
),
'{{ port }}' => $this->_scopeConfig->getValue(
'/* {{ port }} */' => $this->_scopeConfig->getValue(
self::XML_VARNISH_PAGECACHE_BACKEND_PORT,
\Magento\Framework\Store\ScopeInterface::SCOPE_STORE
),
'{{ ips }}' => $this->_getAccessList(),
'{{ design_exceptions_code }}' => $this->_getDesignExceptions()
'/* {{ ips }} */' => $this->_getAccessList(),
'/* {{ design_exceptions_code }} */' => $this->_getDesignExceptions()
];
}

Expand All @@ -149,9 +155,9 @@ protected function _getAccessList()
\Magento\Framework\Store\ScopeInterface::SCOPE_STORE
);
if (!empty($accessList)) {
$ips = explode(', ', $accessList);
$ips = explode(',', $accessList);
foreach ($ips as $ip) {
$result[] = sprintf($tpl, $ip);
$result[] = sprintf($tpl, trim($ip));
}
return implode("\n", $result);
}
Expand Down
12 changes: 9 additions & 3 deletions app/code/Magento/PageCache/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@
<field id="caching_application">1</field>
</depends>
</field>
<field id="export_button" type="button" sortOrder="35" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Export Varnish Configuration</label>
<frontend_model>Magento\PageCache\Block\System\Config\Form\Field\Export</frontend_model>
<field id="export_button_version3" type="button" sortOrder="35" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Export Configuration</label>
<frontend_model>Magento\PageCache\Block\System\Config\Form\Field\Export\Varnish3</frontend_model>
<depends>
<field id="caching_application">1</field>
</depends>
</field>
<field id="export_button_version4" type="button" sortOrder="36" showInDefault="1" showInWebsite="0" showInStore="0">
<frontend_model>Magento\PageCache\Block\System\Config\Form\Field\Export\Varnish4</frontend_model>
<depends>
<field id="caching_application">1</field>
</depends>
Expand Down
9 changes: 6 additions & 3 deletions app/code/Magento/PageCache/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
<default>
<system>
<full_page_cache>
<varnish>
<path>Magento/PageCache/etc/varnish.vcl</path>
</varnish>
<varnish3>
<path>Magento/PageCache/etc/varnish3.vcl</path>
</varnish3>
<varnish4>
<path>Magento/PageCache/etc/varnish4.vcl</path>
</varnish4>
<ttl>120</ttl>
<caching_application>1</caching_application>
<default>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import std;
# The minimal Varnish version is 3.0.5

backend default {
.host = "{{ host }}";
.port = "{{ port }}";
.host = "/* {{ host }} */";
.port = "/* {{ port }} */";
}

acl purge {
{{ ips }}
/* {{ ips }} */
}

sub vcl_recv {
Expand Down Expand Up @@ -68,7 +68,7 @@ sub vcl_hash {
if (req.http.cookie ~ "X-Magento-Vary=") {
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
}
{{ design_exceptions_code }}
/* {{ design_exceptions_code }} */
}

sub vcl_fetch {
Expand Down
Loading

0 comments on commit 2aa22d6

Please sign in to comment.