Skip to content

Commit 2f245a0

Browse files
author
Viktor Paladiychuk
committed
MAGETWO-62660: [GitHub] Overly aggressive performance optimizations of the static content deployment #7862
1 parent d923e49 commit 2f245a0

File tree

5 files changed

+252
-89
lines changed

5 files changed

+252
-89
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Deploy\Model\Deploy;
8+
9+
use Magento\Framework\App\View\Asset\Publisher;
10+
use Magento\Framework\View\Asset\Repository;
11+
use Magento\Framework\Translate\Js\Config as TranslationJsConfig;
12+
use Magento\Framework\TranslateInterface;
13+
use Magento\Framework\Console\Cli;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
16+
class JsDictionaryDeploy implements DeployInterface
17+
{
18+
/**
19+
* @var Repository
20+
*/
21+
private $assetRepo;
22+
23+
/**
24+
* @var Publisher
25+
*/
26+
private $assetPublisher;
27+
28+
/**
29+
* @var TranslationJsConfig
30+
*/
31+
private $translationJsConfig;
32+
33+
/**
34+
* @var TranslateInterface
35+
*/
36+
private $translator;
37+
38+
/**
39+
* @var OutputInterface
40+
*/
41+
private $output;
42+
43+
/**
44+
* @param Repository $assetRepo
45+
* @param Publisher $assetPublisher
46+
* @param TranslationJsConfig $translationJsConfig
47+
* @param TranslateInterface $translator
48+
* @param OutputInterface $output
49+
*/
50+
public function __construct(
51+
Repository $assetRepo,
52+
Publisher $assetPublisher,
53+
TranslationJsConfig $translationJsConfig,
54+
TranslateInterface $translator,
55+
OutputInterface $output
56+
) {
57+
$this->assetRepo = $assetRepo;
58+
$this->assetPublisher = $assetPublisher;
59+
$this->translationJsConfig = $translationJsConfig;
60+
$this->translator = $translator;
61+
$this->output = $output;
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function deploy($area, $themePath, $locale)
68+
{
69+
$this->translator->setLocale($locale);
70+
$this->translator->loadData($area, true);
71+
72+
$asset = $this->assetRepo->createAsset(
73+
$this->translationJsConfig->getDictionaryFileName(),
74+
['area' => $area, 'theme' => $themePath, 'locale' => $locale]
75+
);
76+
if ($this->output->isVeryVerbose()) {
77+
$this->output->writeln("\tDeploying the file to '{$asset->getPath()}'");
78+
} else {
79+
$this->output->write('.');
80+
}
81+
82+
$this->assetPublisher->publish($asset);
83+
84+
return Cli::RETURN_SUCCESS;
85+
}
86+
}

app/code/Magento/Deploy/Model/Deploy/LocaleQuickDeploy.php

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
use Magento\Framework\Console\Cli;
1414
use Magento\Deploy\Console\Command\DeployStaticOptionsInterface as Options;
1515
use Magento\Framework\RequireJs\Config as RequireJsConfig;
16-
use Magento\Framework\App\View\Asset\Publisher;
17-
use Magento\Framework\View\Asset\Repository;
16+
use Magento\Framework\Translate\Js\Config as TranslationJsConfig;
1817
use Magento\Framework\App\ObjectManager;
19-
use Magento\Translation\Model\Js\Config as TranslationJsConfig;
20-
use Magento\Framework\TranslateInterface;
18+
use Magento\Deploy\Model\DeployStrategyFactory;
2119

2220
class LocaleQuickDeploy implements DeployInterface
2321
{
@@ -42,51 +40,41 @@ class LocaleQuickDeploy implements DeployInterface
4240
private $options = [];
4341

4442
/**
45-
* @var Repository
46-
*/
47-
private $assetRepo;
48-
49-
/**
50-
* @var Publisher
43+
* @var TranslationJsConfig
5144
*/
52-
private $assetPublisher;
45+
private $translationJsConfig;
5346

5447
/**
55-
* @var TranslationJsConfig
48+
* @var DeployStrategyFactory
5649
*/
57-
private $translationJsConfig;
50+
private $deployStrategyFactory;
5851

5952
/**
60-
* @var TranslateInterface
53+
* @var DeployInterface[]
6154
*/
62-
private $translator;
55+
private $deploys;
6356

6457
/**
6558
* @param Filesystem $filesystem
6659
* @param OutputInterface $output
6760
* @param array $options
68-
* @param Repository $assetRepo
69-
* @param Publisher $assetPublisher
7061
* @param TranslationJsConfig $translationJsConfig
71-
* @param TranslateInterface $translator
62+
* @param DeployStrategyFactory $deployStrategyFactory
7263
*/
7364
public function __construct(
7465
Filesystem $filesystem,
7566
OutputInterface $output,
7667
$options = [],
77-
Repository $assetRepo = null,
78-
Publisher $assetPublisher = null,
7968
TranslationJsConfig $translationJsConfig = null,
80-
TranslateInterface $translator = null
69+
DeployStrategyFactory $deployStrategyFactory = null
8170
) {
8271
$this->filesystem = $filesystem;
8372
$this->output = $output;
8473
$this->options = $options;
85-
$this->assetRepo = $assetRepo ?: ObjectManager::getInstance()->get(Repository::class);
86-
$this->assetPublisher = $assetPublisher ?: ObjectManager::getInstance()->get(Publisher::class);
8774
$this->translationJsConfig = $translationJsConfig
8875
?: ObjectManager::getInstance()->get(TranslationJsConfig::class);
89-
$this->translator = $translator ?: ObjectManager::getInstance()->get(TranslateInterface::class);
76+
$this->deployStrategyFactory = $deployStrategyFactory
77+
?: ObjectManager::getInstance()->get(DeployStrategyFactory::class);
9078
}
9179

9280
/**
@@ -140,16 +128,23 @@ public function deploy($area, $themePath, $locale)
140128
$jsDictionaryEnabled = $this->translationJsConfig->dictionaryEnabled();
141129
foreach ($localeFiles as $path) {
142130
if ($this->getStaticDirectory()->isFile($path)) {
143-
$destination = $this->replaceLocaleInPath($path, $baseLocale, $locale);
144131
if (!$jsDictionaryEnabled || !$this->isJsDictionary($path)) {
132+
$destination = $this->replaceLocaleInPath($path, $baseLocale, $locale);
145133
$this->getStaticDirectory()->copyFile($path, $destination);
134+
$processedFiles++;
146135
}
147-
$processedFiles++;
148136
}
149137
}
150138

151139
if ($jsDictionaryEnabled) {
152-
$this->deployJsDictionary($area, $themePath, $locale);
140+
$this->getDeploy(
141+
DeployStrategyFactory::DEPLOY_STRATEGY_JS_DICTIONARY,
142+
[
143+
'output' => $this->output,
144+
'translationJsConfig' => $this->translationJsConfig
145+
]
146+
)
147+
->deploy($area, $themePath, $locale);
153148
$processedFiles++;
154149
}
155150

@@ -160,40 +155,29 @@ public function deploy($area, $themePath, $locale)
160155
}
161156

162157
/**
163-
* Define if provided path is js dictionary
158+
* Get deploy strategy according to required strategy
164159
*
165-
* @param string $path
166-
* @return bool
160+
* @param string $strategy
161+
* @param array $params
162+
* @return DeployInterface
167163
*/
168-
private function isJsDictionary($path)
164+
private function getDeploy($strategy, $params)
169165
{
170-
return strpos($path, $this->translationJsConfig->getDictionaryFileName()) !== false;
166+
if (empty($this->deploys[$strategy])) {
167+
$this->deploys[$strategy] = $this->deployStrategyFactory->create($strategy, $params);
168+
}
169+
return $this->deploys[$strategy];
171170
}
172171

173172
/**
174-
* Deploy js-dictionary for specific locale, theme and area
173+
* Define if provided path is js dictionary
175174
*
176-
* @param string $area
177-
* @param string $themePath
178-
* @param string $locale
179-
* @return void
175+
* @param string $path
176+
* @return bool
180177
*/
181-
private function deployJsDictionary($area, $themePath, $locale)
178+
private function isJsDictionary($path)
182179
{
183-
$this->translator->setLocale($locale);
184-
$this->translator->loadData($area, true);
185-
186-
$asset = $this->assetRepo->createAsset(
187-
$this->translationJsConfig->getDictionaryFileName(),
188-
['area' => $area, 'theme' => $themePath, 'locale' => $locale]
189-
);
190-
if ($this->output->isVeryVerbose()) {
191-
$this->output->writeln("\tDeploying the file to '{$asset->getPath()}'");
192-
} else {
193-
$this->output->write('.');
194-
}
195-
196-
$this->assetPublisher->publish($asset);
180+
return strpos($path, $this->translationJsConfig->getDictionaryFileName()) !== false;
197181
}
198182

199183
/**

app/code/Magento/Deploy/Model/DeployStrategyFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class DeployStrategyFactory
2222
*/
2323
const DEPLOY_STRATEGY_QUICK = 'quick';
2424

25+
/**
26+
* Strategy for deploying js dictionary
27+
*/
28+
const DEPLOY_STRATEGY_JS_DICTIONARY = 'js-dictionary';
29+
2530
/**
2631
* @param ObjectManagerInterface $objectManager
2732
*/
@@ -41,6 +46,7 @@ public function create($type, array $arguments = [])
4146
$strategyMap = [
4247
self::DEPLOY_STRATEGY_STANDARD => Deploy\LocaleDeploy::class,
4348
self::DEPLOY_STRATEGY_QUICK => Deploy\LocaleQuickDeploy::class,
49+
self::DEPLOY_STRATEGY_JS_DICTIONARY => Deploy\JsDictionaryDeploy::class
4450
];
4551

4652
if (!isset($strategyMap[$type])) {
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Deploy\Test\Unit\Model\Deploy;
8+
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Magento\Framework\Translate\Js\Config as TranslationJsConfig;
11+
use Magento\Framework\TranslateInterface;
12+
use Magento\Framework\View\Asset\Repository;
13+
use Magento\Framework\View\Asset\LocalInterface as Asset;
14+
use Magento\Framework\App\View\Asset\Publisher;
15+
use Magento\Deploy\Model\Deploy\JsDictionaryDeploy;
16+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
17+
18+
class JsDictionaryDeployTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var TranslationJsConfig|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $translationJsConfig;
24+
25+
/**
26+
* @var TranslateInterface|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $translator;
29+
30+
/**
31+
* @var Repository|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $assetRepo;
34+
35+
/**
36+
* @var Asset|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $asset;
39+
40+
/**
41+
* @var Publisher|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $assetPublisher;
44+
45+
/**
46+
* @var JsDictionaryDeploy
47+
*/
48+
private $model;
49+
50+
/**
51+
* @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
private $output;
54+
55+
protected function setUp()
56+
{
57+
$this->output = $this->getMockBuilder(OutputInterface::class)
58+
->setMethods(['writeln', 'isVeryVerbose'])
59+
->getMockForAbstractClass();
60+
61+
$this->translationJsConfig = $this->getMock(TranslationJsConfig::class, [], [], '', false);
62+
$this->translator = $this->getMockForAbstractClass(TranslateInterface::class, [], '', false, false, true);
63+
$this->assetRepo = $this->getMock(Repository::class, [], [], '', false);
64+
$this->asset = $this->getMockForAbstractClass(Asset::class, [], '', false, false, true);
65+
$this->assetPublisher = $this->getMock(Publisher::class, [], [], '', false);
66+
67+
$this->model = (new ObjectManager($this))->getObject(
68+
JsDictionaryDeploy::class,
69+
[
70+
'translationJsConfig' => $this->translationJsConfig,
71+
'translator' => $this->translator,
72+
'assetRepo' => $this->assetRepo,
73+
'assetPublisher' => $this->assetPublisher,
74+
'output' => $this->output
75+
]
76+
);
77+
}
78+
79+
public function testDeploy()
80+
{
81+
$area = 'adminhtml';
82+
$themePath = 'Magento/backend';
83+
$locale = 'uk_UA';
84+
85+
$dictionary = 'js-translation.json';
86+
87+
$this->translationJsConfig->expects(self::exactly(1))->method('getDictionaryFileName')
88+
->willReturn($dictionary);
89+
90+
$this->translator->expects($this->once())->method('setLocale')->with($locale);
91+
$this->translator->expects($this->once())->method('loadData')->with($area, true);
92+
93+
$this->assetRepo->expects($this->once())->method('createAsset')
94+
->with(
95+
$dictionary,
96+
['area' => $area, 'theme' => $themePath, 'locale' => $locale]
97+
)
98+
->willReturn($this->asset);
99+
100+
$this->assetPublisher->expects($this->once())->method('publish');
101+
102+
$this->model->deploy($area, $themePath, $locale);
103+
}
104+
105+
}

0 commit comments

Comments
 (0)