Skip to content

Commit 058ac23

Browse files
committed
MAGETWO-52660: Improve performance of static assets deployment
- MAGETWO-57185: Porting to 2.1 -- use theme provider
1 parent 66db224 commit 058ac23

File tree

9 files changed

+186
-52
lines changed

9 files changed

+186
-52
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\Config\Theme;
1717
use Magento\Framework\ObjectManagerInterface;
1818
use Magento\Framework\Translate\Js\Config as JsTranslationConfig;
19+
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
1920
use Symfony\Component\Console\Output\OutputInterface;
2021
use Psr\Log\LoggerInterface;
2122
use Magento\Framework\View\Asset\Minification;
@@ -77,6 +78,11 @@ class Deployer
7778
*/
7879
private $alternativeSources;
7980

81+
/**
82+
* @var ThemeProviderInterface
83+
*/
84+
private $themeProvider;
85+
8086
/**
8187
* @var array
8288
*/
@@ -465,9 +471,7 @@ private function deployFile($filePath, $area, $themePath, $locale, $module, $ful
465471
*/
466472
private function findAncestors($themeFullPath)
467473
{
468-
/** @var \Magento\Framework\View\Design\Theme\ListInterface $themeCollection */
469-
$themeCollection = $this->objectManager->get(\Magento\Framework\View\Design\Theme\ListInterface::class);
470-
$theme = $themeCollection->getThemeByFullPath($themeFullPath);
474+
$theme = $this->getThemeProvider()->getThemeByFullPath($themeFullPath);
471475
$ancestors = $theme->getInheritedThemes();
472476
$ancestorThemeFullPath = [];
473477
foreach ($ancestors as $ancestor) {
@@ -476,6 +480,19 @@ private function findAncestors($themeFullPath)
476480
return $ancestorThemeFullPath;
477481
}
478482

483+
484+
/**
485+
* @return ThemeProviderInterface
486+
*/
487+
private function getThemeProvider()
488+
{
489+
if (null === $this->themeProvider) {
490+
$this->themeProvider = ObjectManager::getInstance()->get(ThemeProviderInterface::class);
491+
}
492+
493+
return $this->themeProvider;
494+
}
495+
479496
/**
480497
* @return \Magento\Framework\View\Asset\ConfigInterface
481498
* @deprecated

app/code/Magento/Theme/Model/Theme/ThemeProvider.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class ThemeProvider implements \Magento\Framework\View\Design\Theme\ThemeProvide
2222
*/
2323
protected $cache;
2424

25+
/**
26+
* @var \Magento\Framework\View\Design\ThemeInterface[]
27+
*/
28+
private $themes;
29+
2530
/**
2631
* ThemeProvider constructor.
2732
*
@@ -44,18 +49,24 @@ public function __construct(
4449
*/
4550
public function getThemeByFullPath($fullPath)
4651
{
52+
if (isset($this->themes[$fullPath])) {
53+
return $this->themes[$fullPath];
54+
}
4755
/** @var $themeCollection \Magento\Theme\Model\ResourceModel\Theme\Collection */
4856
$theme = $this->cache->load('theme'. $fullPath);
4957
if ($theme) {
50-
return unserialize($theme);
58+
$this->themes[$fullPath] = unserialize($theme);
59+
return $this->themes[$fullPath];
5160
}
5261
$themeCollection = $this->collectionFactory->create();
5362
$item = $themeCollection->getThemeByFullPath($fullPath);
5463
if ($item->getId()) {
5564
$themeData = serialize($item);
5665
$this->cache->save($themeData, 'theme' . $fullPath);
5766
$this->cache->save($themeData, 'theme-by-id-' . $item->getId());
67+
$this->themes[$fullPath] = $item;
5868
}
69+
5970
return $item;
6071
}
6172

@@ -77,15 +88,20 @@ public function getThemeCustomizations(
7788
*/
7889
public function getThemeById($themeId)
7990
{
91+
if (isset($this->themes[$themeId])) {
92+
return $this->themes[$themeId];
93+
}
8094
$theme = $this->cache->load('theme-by-id-' . $themeId);
8195
if ($theme) {
82-
return unserialize($theme);
96+
$this->themes[$themeId] = unserialize($theme);
97+
return $this->themes[$themeId];
8398
}
8499
/** @var $themeModel \Magento\Framework\View\Design\ThemeInterface */
85100
$themeModel = $this->themeFactory->create();
86101
$themeModel->load($themeId);
87102
if ($themeModel->getId()) {
88103
$this->cache->save(serialize($themeModel), 'theme-by-id-' . $themeId);
104+
$this->themes[$themeId] = $themeModel;
89105
}
90106
return $themeModel;
91107
}

lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66
namespace Magento\Framework\Css\PreProcessor\Instruction;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\Css\PreProcessor\ErrorHandlerInterface;
910
use Magento\Framework\View\Asset\File\FallbackContext;
1011
use Magento\Framework\View\Asset\LocalInterface;
1112
use Magento\Framework\View\Asset\PreProcessorInterface;
13+
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
1214
use Magento\Framework\View\DesignInterface;
1315
use Magento\Framework\View\File\CollectorInterface;
1416

@@ -45,9 +47,15 @@ class MagentoImport implements PreProcessorInterface
4547

4648
/**
4749
* @var \Magento\Framework\View\Design\Theme\ListInterface
50+
* @deprecated
4851
*/
4952
protected $themeList;
5053

54+
/**
55+
* @var ThemeProviderInterface
56+
*/
57+
private $themeProvider;
58+
5159
/**
5260
* @param DesignInterface $design
5361
* @param CollectorInterface $fileSource
@@ -120,8 +128,22 @@ protected function getTheme(LocalInterface $asset)
120128
{
121129
$context = $asset->getContext();
122130
if ($context instanceof FallbackContext) {
123-
return $this->themeList->getThemeByFullPath($context->getAreaCode() . '/' . $context->getThemePath());
131+
return $this->getThemeProvider()->getThemeByFullPath(
132+
$context->getAreaCode() . '/' . $context->getThemePath()
133+
);
124134
}
125135
return $this->design->getDesignTheme();
126136
}
137+
138+
/**
139+
* @return ThemeProviderInterface
140+
*/
141+
private function getThemeProvider()
142+
{
143+
if (null === $this->themeProvider) {
144+
$this->themeProvider = ObjectManager::getInstance()->get(ThemeProviderInterface::class);
145+
}
146+
147+
return $this->themeProvider;
148+
}
127149
}

lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
namespace Magento\Framework\Css\Test\Unit\PreProcessor\Instruction;
1010

11+
use Magento\Framework\Css\PreProcessor\Instruction\MagentoImport;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
14+
1115
class MagentoImportTest extends \PHPUnit_Framework_TestCase
1216
{
1317
/**
@@ -38,7 +42,7 @@ class MagentoImportTest extends \PHPUnit_Framework_TestCase
3842
/**
3943
* @var \Magento\Framework\View\Design\Theme\ListInterface|\PHPUnit_Framework_MockObject_MockObject
4044
*/
41-
private $themeList;
45+
private $themeProvider;
4246

4347
/**
4448
* @var \Magento\Framework\Css\PreProcessor\Instruction\Import
@@ -55,14 +59,14 @@ protected function setUp()
5559
$this->asset = $this->getMock('\Magento\Framework\View\Asset\File', [], [], '', false);
5660
$this->asset->expects($this->any())->method('getContentType')->will($this->returnValue('css'));
5761
$this->assetRepo = $this->getMock('\Magento\Framework\View\Asset\Repository', [], [], '', false);
58-
$this->themeList = $this->getMockForAbstractClass('\Magento\Framework\View\Design\Theme\ListInterface');
59-
$this->object = new \Magento\Framework\Css\PreProcessor\Instruction\MagentoImport(
60-
$this->design,
61-
$this->fileSource,
62-
$this->errorHandler,
63-
$this->assetRepo,
64-
$this->themeList
65-
);
62+
$this->themeProvider = $this->getMock(ThemeProviderInterface::class);
63+
$this->object = (new ObjectManager($this))->getObject(MagentoImport::class, [
64+
'design' => $this->design,
65+
'fileSource' => $this->fileSource,
66+
'errorHandler' => $this->errorHandler,
67+
'assetRepo' => $this->assetRepo,
68+
'themeProvider' => $this->themeProvider
69+
]);
6670
}
6771

6872
/**
@@ -88,7 +92,7 @@ public function testProcess($originalContent, $foundPath, $resolvedPath, $foundF
8892
->will($this->returnValue($relatedAsset));
8993
$relatedAsset->expects($this->once())->method('getContext')->will($this->returnValue($context));
9094
$theme = $this->getMockForAbstractClass('\Magento\Framework\View\Design\ThemeInterface');
91-
$this->themeList->expects($this->once())->method('getThemeByFullPath')->will($this->returnValue($theme));
95+
$this->themeProvider->expects($this->once())->method('getThemeByFullPath')->will($this->returnValue($theme));
9296
$files = [];
9397
foreach ($foundFiles as $file) {
9498
$fileObject = $this->getMock('Magento\Framework\View\File', [], [], '', false);

lib/internal/Magento/Framework/View/Asset/Bundle/Config.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
namespace Magento\Framework\View\Asset\Bundle;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\View;
1011
use Magento\Framework\View\Asset\Bundle;
1112
use Magento\Framework\View\Design\Theme\ListInterface;
1213
use Magento\Framework\View\Asset\File\FallbackContext;
14+
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
1315

1416
class Config implements Bundle\ConfigInterface
1517
{
@@ -30,6 +32,16 @@ class Config implements Bundle\ConfigInterface
3032
*/
3133
protected $viewConfig;
3234

35+
/**
36+
* @var ThemeProviderInterface
37+
*/
38+
private $themeProvider;
39+
40+
/**
41+
* @var \Magento\Framework\Config\View[]
42+
*/
43+
private $config = [];
44+
3345
/**
3446
* @param View\ConfigInterface $viewConfig
3547
* @param ListInterface $themeList
@@ -57,12 +69,17 @@ public function isSplit(FallbackContext $assetContext)
5769
*/
5870
public function getConfig(FallbackContext $assetContext)
5971
{
60-
return $this->viewConfig->getViewConfig([
61-
'area' => $assetContext->getAreaCode(),
62-
'themeModel' => $this->themeList->getThemeByFullPath(
63-
$assetContext->getAreaCode() . '/' . $assetContext->getThemePath()
64-
)
65-
]);
72+
$themePath = $assetContext->getAreaCode() . '/' . $assetContext->getThemePath();
73+
if (!isset($this->config[$themePath])) {
74+
$this->config[$themePath] = $this->viewConfig->getViewConfig([
75+
'area' => $assetContext->getAreaCode(),
76+
'themeModel' => $this->getThemeProvider()->getThemeByFullPath(
77+
$themePath
78+
)
79+
]);
80+
}
81+
82+
return $this->config[$themePath];
6683
}
6784

6885
/**
@@ -86,4 +103,16 @@ public function getPartSize(FallbackContext $assetContext)
86103
return (int)$size / 1024;
87104
}
88105
}
106+
107+
/**
108+
* @return ThemeProviderInterface
109+
*/
110+
private function getThemeProvider()
111+
{
112+
if (null === $this->themeProvider) {
113+
$this->themeProvider = ObjectManager::getInstance()->get(ThemeProviderInterface::class);
114+
}
115+
116+
return $this->themeProvider;
117+
}
89118
}

lib/internal/Magento/Framework/View/Asset/Repository.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
use Magento\Framework\UrlInterface;
1010
use Magento\Framework\App\Filesystem\DirectoryList;
11-
use Magento\Framework\Filesystem;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
1213

1314
/**
1415
* A repository service for view assets
@@ -35,6 +36,7 @@ class Repository
3536

3637
/**
3738
* @var \Magento\Framework\View\Design\Theme\ListInterface
39+
* @deprecated
3840
*/
3941
private $themeList;
4042

@@ -72,11 +74,17 @@ class Repository
7274
* @var File\ContextFactory
7375
*/
7476
private $contextFactory;
77+
7578
/**
7679
* @var RemoteFactory
7780
*/
7881
private $remoteFactory;
7982

83+
/**
84+
* @var ThemeProviderInterface
85+
*/
86+
private $themeProvider;
87+
8088
/**
8189
* @param \Magento\Framework\UrlInterface $baseUrl
8290
* @param \Magento\Framework\View\DesignInterface $design
@@ -138,7 +146,7 @@ public function updateDesignParams(array &$params)
138146
}
139147

140148
if ($theme) {
141-
$params['themeModel'] = $this->themeList->getThemeByFullPath($area . '/' . $theme);
149+
$params['themeModel'] = $this->getThemeProvider()->getThemeByFullPath($area . '/' . $theme);
142150
if (!$params['themeModel']) {
143151
throw new \UnexpectedValueException("Could not find theme '$theme' for area '$area'");
144152
}
@@ -158,6 +166,18 @@ public function updateDesignParams(array &$params)
158166
return $this;
159167
}
160168

169+
/**
170+
* @return ThemeProviderInterface
171+
*/
172+
private function getThemeProvider()
173+
{
174+
if (null === $this->themeProvider) {
175+
$this->themeProvider = ObjectManager::getInstance()->get(ThemeProviderInterface::class);
176+
}
177+
178+
return $this->themeProvider;
179+
}
180+
161181
/**
162182
* Get default design parameter
163183
*

0 commit comments

Comments
 (0)