Skip to content

Commit 106118e

Browse files
committed
Merge branch 'develop' of github.com:magento/magento2ce into MAGETWO-63252
2 parents ddc51e0 + 24cd0c3 commit 106118e

File tree

5 files changed

+344
-28
lines changed

5 files changed

+344
-28
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
/**
17+
* Deploy class for js dictionary
18+
*/
19+
class JsDictionaryDeploy implements DeployInterface
20+
{
21+
/**
22+
* @var Repository
23+
*/
24+
private $assetRepo;
25+
26+
/**
27+
* @var Publisher
28+
*/
29+
private $assetPublisher;
30+
31+
/**
32+
* @var TranslationJsConfig
33+
*/
34+
private $translationJsConfig;
35+
36+
/**
37+
* @var TranslateInterface
38+
*/
39+
private $translator;
40+
41+
/**
42+
* @var OutputInterface
43+
*/
44+
private $output;
45+
46+
/**
47+
* @param Repository $assetRepo
48+
* @param Publisher $assetPublisher
49+
* @param TranslationJsConfig $translationJsConfig
50+
* @param TranslateInterface $translator
51+
* @param OutputInterface $output
52+
*/
53+
public function __construct(
54+
Repository $assetRepo,
55+
Publisher $assetPublisher,
56+
TranslationJsConfig $translationJsConfig,
57+
TranslateInterface $translator,
58+
OutputInterface $output
59+
) {
60+
$this->assetRepo = $assetRepo;
61+
$this->assetPublisher = $assetPublisher;
62+
$this->translationJsConfig = $translationJsConfig;
63+
$this->translator = $translator;
64+
$this->output = $output;
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function deploy($area, $themePath, $locale)
71+
{
72+
$this->translator->setLocale($locale);
73+
$this->translator->loadData($area, true);
74+
75+
$asset = $this->assetRepo->createAsset(
76+
$this->translationJsConfig->getDictionaryFileName(),
77+
['area' => $area, 'theme' => $themePath, 'locale' => $locale]
78+
);
79+
if ($this->output->isVeryVerbose()) {
80+
$this->output->writeln("\tDeploying the file to '{$asset->getPath()}'");
81+
} else {
82+
$this->output->write('.');
83+
}
84+
85+
$this->assetPublisher->publish($asset);
86+
87+
return Cli::RETURN_SUCCESS;
88+
}
89+
}

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

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66

77
namespace Magento\Deploy\Model\Deploy;
88

9-
use Magento\Deploy\Model\DeployManager;
109
use Magento\Framework\App\Filesystem\DirectoryList;
11-
use Magento\Framework\App\Utility\Files;
1210
use Magento\Framework\Filesystem;
1311
use Magento\Framework\Filesystem\Directory\WriteInterface;
1412
use Symfony\Component\Console\Output\OutputInterface;
1513
use Magento\Framework\Console\Cli;
1614
use Magento\Deploy\Console\Command\DeployStaticOptionsInterface as Options;
17-
use \Magento\Framework\RequireJs\Config as RequireJsConfig;
15+
use Magento\Framework\RequireJs\Config as RequireJsConfig;
16+
use Magento\Framework\Translate\Js\Config as TranslationJsConfig;
17+
use Magento\Framework\App\ObjectManager;
18+
use Magento\Deploy\Model\DeployStrategyFactory;
1819

20+
/**
21+
* To avoid duplication of deploying of all static content per each theme/local, this class uses copying/symlinking
22+
* of default static files to other locales, separately calls deploy for js dictionary per each locale
23+
*/
1924
class LocaleQuickDeploy implements DeployInterface
2025
{
2126
/**
@@ -38,16 +43,42 @@ class LocaleQuickDeploy implements DeployInterface
3843
*/
3944
private $options = [];
4045

46+
/**
47+
* @var TranslationJsConfig
48+
*/
49+
private $translationJsConfig;
50+
51+
/**
52+
* @var DeployStrategyFactory
53+
*/
54+
private $deployStrategyFactory;
55+
56+
/**
57+
* @var DeployInterface[]
58+
*/
59+
private $deploys;
60+
4161
/**
4262
* @param Filesystem $filesystem
4363
* @param OutputInterface $output
4464
* @param array $options
65+
* @param TranslationJsConfig $translationJsConfig
66+
* @param DeployStrategyFactory $deployStrategyFactory
4567
*/
46-
public function __construct(\Magento\Framework\Filesystem $filesystem, OutputInterface $output, $options = [])
47-
{
68+
public function __construct(
69+
Filesystem $filesystem,
70+
OutputInterface $output,
71+
$options = [],
72+
TranslationJsConfig $translationJsConfig = null,
73+
DeployStrategyFactory $deployStrategyFactory = null
74+
) {
4875
$this->filesystem = $filesystem;
4976
$this->output = $output;
5077
$this->options = $options;
78+
$this->translationJsConfig = $translationJsConfig
79+
?: ObjectManager::getInstance()->get(TranslationJsConfig::class);
80+
$this->deployStrategyFactory = $deployStrategyFactory
81+
?: ObjectManager::getInstance()->get(DeployStrategyFactory::class);
5182
}
5283

5384
/**
@@ -67,13 +98,13 @@ private function getStaticDirectory()
6798
*/
6899
public function deploy($area, $themePath, $locale)
69100
{
70-
if (isset($this->options[Options::DRY_RUN]) && $this->options[Options::DRY_RUN]) {
101+
if (!empty($this->options[Options::DRY_RUN])) {
71102
return Cli::RETURN_SUCCESS;
72103
}
73104

74105
$this->output->writeln("=== {$area} -> {$themePath} -> {$locale} ===");
75106

76-
if (!isset($this->options[self::DEPLOY_BASE_LOCALE])) {
107+
if (empty($this->options[self::DEPLOY_BASE_LOCALE])) {
77108
throw new \InvalidArgumentException('Deploy base locale must be set for Quick Deploy');
78109
}
79110
$processedFiles = 0;
@@ -88,7 +119,7 @@ public function deploy($area, $themePath, $locale)
88119
$this->deleteLocaleResource($newLocalePath);
89120
$this->deleteLocaleResource($newRequireJsPath);
90121

91-
if (isset($this->options[Options::SYMLINK_LOCALE]) && $this->options[Options::SYMLINK_LOCALE]) {
122+
if (!empty($this->options[Options::SYMLINK_LOCALE])) {
92123
$this->getStaticDirectory()->createSymlink($baseLocalePath, $newLocalePath);
93124
$this->getStaticDirectory()->createSymlink($baseRequireJsPath, $newRequireJsPath);
94125

@@ -98,20 +129,61 @@ public function deploy($area, $themePath, $locale)
98129
$this->getStaticDirectory()->readRecursively($baseLocalePath),
99130
$this->getStaticDirectory()->readRecursively($baseRequireJsPath)
100131
);
132+
$jsDictionaryEnabled = $this->translationJsConfig->dictionaryEnabled();
101133
foreach ($localeFiles as $path) {
102134
if ($this->getStaticDirectory()->isFile($path)) {
103-
$destination = $this->replaceLocaleInPath($path, $baseLocale, $locale);
104-
$this->getStaticDirectory()->copyFile($path, $destination);
105-
$processedFiles++;
135+
if (!$jsDictionaryEnabled || !$this->isJsDictionary($path)) {
136+
$destination = $this->replaceLocaleInPath($path, $baseLocale, $locale);
137+
$this->getStaticDirectory()->copyFile($path, $destination);
138+
$processedFiles++;
139+
}
106140
}
107141
}
108142

143+
if ($jsDictionaryEnabled) {
144+
$this->getDeploy(
145+
DeployStrategyFactory::DEPLOY_STRATEGY_JS_DICTIONARY,
146+
[
147+
'output' => $this->output,
148+
'translationJsConfig' => $this->translationJsConfig
149+
]
150+
)
151+
->deploy($area, $themePath, $locale);
152+
$processedFiles++;
153+
}
154+
109155
$this->output->writeln("\nSuccessful copied: {$processedFiles} files; errors: {$errorAmount}\n---\n");
110156
}
111157

112158
return Cli::RETURN_SUCCESS;
113159
}
114160

161+
/**
162+
* Get deploy strategy according to required strategy
163+
*
164+
* @param string $strategy
165+
* @param array $params
166+
* @return DeployInterface
167+
*/
168+
private function getDeploy($strategy, $params)
169+
{
170+
if (empty($this->deploys[$strategy])) {
171+
$this->deploys[$strategy] = $this->deployStrategyFactory->create($strategy, $params);
172+
}
173+
return $this->deploys[$strategy];
174+
}
175+
176+
/**
177+
* Define if provided path is js dictionary
178+
*
179+
* @param string $path
180+
* @return bool
181+
*/
182+
private function isJsDictionary($path)
183+
{
184+
return strpos($path, $this->translationJsConfig->getDictionaryFileName()) !== false;
185+
}
186+
115187
/**
116188
* @param string $path
117189
* @return void

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

0 commit comments

Comments
 (0)