Skip to content

Commit f05e6ab

Browse files
author
Pavlo Cherniavskyi
committed
MAGETWO-33622: Junior+ Developer Workflow
- draft commit
1 parent a9e59f8 commit f05e6ab

File tree

5 files changed

+127
-11
lines changed

5 files changed

+127
-11
lines changed

app/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@
181181
<argument name="mode" xsi:type="init_parameter">Magento\Framework\App\State::PARAM_MODE</argument>
182182
</arguments>
183183
</type>
184+
<type name="\Magento\Framework\View\Asset\Source">
185+
<arguments>
186+
<argument name="appmode" xsi:type="init_parameter">Magento\Framework\App\State::PARAM_MODE</argument>
187+
</arguments>
188+
</type>
184189
<type name="Magento\Framework\App\Arguments\ValidationState">
185190
<arguments>
186191
<argument name="appMode" xsi:type="init_parameter">Magento\Framework\App\State::PARAM_MODE</argument>

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public function getUrl()
6767
return $this->context->getBaseUrl() . $this->getPath();
6868
}
6969

70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function getSourceUrl()
74+
{
75+
return $this->context->getBaseUrl() . $this->getRelativeSourceFilePath();
76+
}
77+
7078
/**
7179
* {@inheritdoc}
7280
*/
@@ -87,6 +95,22 @@ public function getPath()
8795
return $result;
8896
}
8997

98+
/**
99+
* {@inheritdoc}
100+
*/
101+
public function getRelativeSourceFilePath()
102+
{
103+
$path = $this->filePath;
104+
if (strpos($this->source->findRelativeSourceFile($this), 'less')) {
105+
$path = str_replace('.css', '.less', $this->filePath);
106+
}
107+
$result = '';
108+
$result = $this->join($result, $this->context->getPath());
109+
$result = $this->join($result, $this->module);
110+
$result = $this->join($result, $path);
111+
return $result;
112+
}
113+
90114
/**
91115
* Subroutine for building path
92116
*
@@ -106,7 +130,9 @@ private function join($path, $item)
106130
public function getSourceFile()
107131
{
108132
if (null === $this->resolvedFile) {
109-
$this->resolvedFile = $this->source->getFile($this);
133+
$result = $this->source->getFile($this);
134+
$this->resolvedFile = $result['abs'];
135+
$this->filePath = $result['relativePath'];
110136
if (false === $this->resolvedFile) {
111137
throw new File\NotFoundException("Unable to resolve the source file for '{$this->getPath()}'");
112138
}

lib/internal/Magento/Framework/View/Asset/PreProcessor/Chain.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,39 @@ class Chain
4242
*/
4343
private $targetContentType;
4444

45+
/**
46+
* @var string
47+
*/
48+
private $appMode;
49+
50+
private $targetAssetPath;
51+
4552
/**
4653
* @param \Magento\Framework\View\Asset\LocalInterface $asset
4754
* @param string $origContent
4855
* @param string $origContentType
4956
*/
50-
public function __construct(\Magento\Framework\View\Asset\LocalInterface $asset, $origContent, $origContentType)
51-
{
57+
public function __construct(
58+
\Magento\Framework\View\Asset\LocalInterface $asset,
59+
$origContent,
60+
$origContentType,
61+
$origPath,
62+
$appMode = \Magento\Framework\App\State::MODE_DEFAULT
63+
) {
5264
$this->asset = $asset;
5365
$this->origContent = $origContent;
5466
$this->content = $origContent;
5567
$this->origContentType = $origContentType;
5668
$this->contentType = $origContentType;
57-
$this->targetContentType = $asset->getContentType();
69+
$this->appMode = $appMode;
70+
71+
if ($appMode == \Magento\Framework\App\State::MODE_DEVELOPER) {
72+
$this->targetContentType = $this->origContentType;
73+
$this->targetAssetPath = $origPath;
74+
} else {
75+
$this->targetContentType = $asset->getContentType();
76+
$this->targetAssetPath = $asset->getPath();
77+
}
5878
}
5979

6080
/**
@@ -139,6 +159,16 @@ public function getTargetContentType()
139159
return $this->targetContentType;
140160
}
141161

162+
/**
163+
* Get the target asset path
164+
*
165+
* @return string
166+
*/
167+
public function getTargetAssetPath()
168+
{
169+
return $this->targetAssetPath;
170+
}
171+
142172
/**
143173
* Assert invariants
144174
*

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class Source
5151
*/
5252
private $themeList;
5353

54+
/**
55+
* @var string
56+
*/
57+
private $appMode;
58+
5459
/**
5560
* @param PreProcessor\Cache $cache
5661
* @param \Magento\Framework\Filesystem $filesystem
@@ -63,7 +68,8 @@ public function __construct(
6368
\Magento\Framework\Filesystem $filesystem,
6469
PreProcessor\Pool $preProcessorPool,
6570
\Magento\Framework\View\Design\FileResolution\Fallback\StaticFile $fallback,
66-
\Magento\Framework\View\Design\Theme\ListInterface $themeList
71+
\Magento\Framework\View\Design\Theme\ListInterface $themeList,
72+
$appMode = \Magento\Framework\App\State::MODE_DEFAULT
6773
) {
6874
$this->cache = $cache;
6975
$this->filesystem = $filesystem;
@@ -72,6 +78,7 @@ public function __construct(
7278
$this->preProcessorPool = $preProcessorPool;
7379
$this->fallback = $fallback;
7480
$this->themeList = $themeList;
81+
$this->appMode = $appMode;
7582
}
7683

7784
/**
@@ -87,7 +94,10 @@ public function getFile(LocalInterface $asset)
8794
return false;
8895
}
8996
list($dirCode, $path) = $result;
90-
return $this->filesystem->getDirectoryRead($dirCode)->getAbsolutePath($path);
97+
return [
98+
'abs' => $this->filesystem->getDirectoryRead($dirCode)->getAbsolutePath($path),
99+
'relativePath' => $path
100+
];
91101
}
92102

93103
/**
@@ -134,7 +144,9 @@ private function preProcess(LocalInterface $asset)
134144
$chain = new \Magento\Framework\View\Asset\PreProcessor\Chain(
135145
$asset,
136146
$this->rootDir->readFile($path),
137-
$this->getContentType($path)
147+
$this->getContentType($path),
148+
$path,
149+
'developer'
138150
);
139151
$preProcessors = $this->preProcessorPool
140152
->getPreProcessors($chain->getOrigContentType(), $chain->getTargetContentType());
@@ -144,7 +156,7 @@ private function preProcess(LocalInterface $asset)
144156
$chain->assertValid();
145157
if ($chain->isChanged()) {
146158
$dirCode = DirectoryList::VAR_DIR;
147-
$path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $asset->getPath();
159+
$path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath();
148160
$this->varDir->writeFile($path, $chain->getContent());
149161
}
150162
$result = [$dirCode, $path];
@@ -219,4 +231,13 @@ private function findFile(LocalInterface $asset, \Magento\Framework\View\Asset\F
219231
Simple::assertFilePathFormat($asset->getFilePath());
220232
return $dir->getAbsolutePath($asset->getPath());
221233
}
234+
235+
public function findRelativeSourceFile(LocalInterface $asset)
236+
{
237+
$sourceFile = $this->findSourceFile($asset);
238+
if (!$sourceFile) {
239+
return false;
240+
}
241+
return $this->rootDir->getRelativePath($sourceFile);
242+
}
222243
}

lib/internal/Magento/Framework/View/Page/Config/Renderer.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Renderer
1717
/**
1818
* @var array
1919
*/
20-
protected $assetTypeOrder = ['css', 'ico', 'js'];
20+
protected $assetTypeOrder = ['css', 'less', 'ico', 'js'];
2121

2222
/**
2323
* @var \Magento\Framework\View\Page\Config
@@ -222,6 +222,9 @@ public function renderAssets()
222222
/** @var $group \Magento\Framework\View\Asset\PropertyGroup */
223223
foreach ($this->pageConfig->getAssetCollection()->getGroups() as $group) {
224224
$type = $group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE);
225+
if ($type == 'js') {
226+
$resultGroups[$type] .= $this->renderLessJsInclude();
227+
}
225228
if (!isset($resultGroups[$type])) {
226229
$resultGroups[$type] = '';
227230
}
@@ -308,6 +311,10 @@ protected function addDefaultAttributes($contentType, $attributes)
308311
case 'css':
309312
$attributes = ' rel="stylesheet" type="text/css" ' . ($attributes ?: ' media="all"');
310313
break;
314+
315+
case 'less':
316+
$attributes = ' rel="stylesheet/less" type="text/css" ' . ($attributes ?: ' media="all"');
317+
break;
311318
}
312319
return $attributes;
313320
}
@@ -325,6 +332,7 @@ protected function getAssetTemplate($contentType, $attributes)
325332
break;
326333

327334
case 'css':
335+
case 'less':
328336
default:
329337
$groupTemplate = '<link ' . $attributes . ' href="%s" />' . "\n";
330338
break;
@@ -357,14 +365,40 @@ protected function renderAssetHtml($template, $assets)
357365
{
358366
$result = '';
359367
try {
360-
/** @var $asset \Magento\Framework\View\Asset\AssetInterface */
368+
/** @var $asset \Magento\Framework\View\Asset\File */
361369
foreach ($assets as $asset) {
362-
$result .= sprintf($template, $asset->getUrl());
370+
371+
if (true) {
372+
if ($asset->getSourceUrl() != $asset->getUrl()) {
373+
$attributes = $this->addDefaultAttributes('less', []);
374+
$groupTemplate = $this->getAssetTemplate('less', $attributes);
375+
$result .= sprintf($groupTemplate, $asset->getSourceUrl());
376+
} else {
377+
$result .= sprintf($template, $asset->getSourceUrl());
378+
}
379+
380+
} else {
381+
$result .= sprintf($template, $asset->getUrl());
382+
}
363383
}
364384
} catch (\Magento\Framework\Exception $e) {
365385
$this->logger->critical($e);
366386
$result .= sprintf($template, $this->urlBuilder->getUrl('', ['_direct' => 'core/index/notFound']));
367387
}
368388
return $result;
369389
}
390+
391+
private function renderLessJsInclude()
392+
{
393+
$result = '';
394+
$result .= '<script>
395+
less = {
396+
env: "production",
397+
async: false,
398+
fileAsync: false
399+
};
400+
</script>' ;
401+
$result .= sprintf('<script src="%s"></script>' . "\n", '//cdnjs.cloudflare.com/ajax/libs/less.js/2.3.1/less.min.js') ;
402+
return $result;
403+
}
370404
}

0 commit comments

Comments
 (0)