1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\Package\Processor\PreProcessor;
use Magento\Deploy\Console\DeployStaticOptions;
use Magento\Deploy\Package\Package;
use Magento\Deploy\Package\PackageFile;
use Magento\Deploy\Package\Processor\ProcessorInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\ReadInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\View\Url\CssResolver;
use Magento\Framework\View\Asset\Minification;
/**
* Pre-processor for speeding up deployment of CSS files
*
* If there is a CSS file overridden in target theme and there are files in ancestor theme where this file was imported,
* then all such parent files must be copied into target theme.
*/
class Css implements ProcessorInterface
{
/**
* @var ReadInterface
*/
private $staticDir;
/**
* @var Minification
*/
private $minification;
/**
* CssUrls constructor
*
* @param Filesystem $filesystem
* @param Minification $minification
*/
public function __construct(Filesystem $filesystem, Minification $minification)
{
$this->staticDir = $filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
$this->minification = $minification;
}
/**
* CSS files map
*
* Collect information about CSS files which have been imported in other CSS files
*
* @var []
*/
private $map = [];
/**
* Deployment procedure options
*
* @var array
*/
private $options = [];
/**
* @inheritdoc
*/
public function process(Package $package, array $options)
{
$this->options = $options;
if ($this->options[DeployStaticOptions::NO_CSS] === true) {
return false;
}
if ($package->getArea() !== Package::BASE_AREA && $package->getTheme() !== Package::BASE_THEME) {
$files = $package->getParentFiles('css');
foreach ($files as $file) {
$packageFile = $package->getFile($file->getFileId());
if ($packageFile && $packageFile->getPackage() === $package) {
continue;
}
if ($this->hasOverrides($file, $package)) {
$file = clone $file;
$file->setArea($package->getArea());
$file->setTheme($package->getTheme());
$file->setLocale($package->getLocale());
$file->setPackage($package);
$package->addFileToMap($file);
}
}
}
return true;
}
/**
* Checks if there are imports of CSS files or images within the given CSS file
* which exists in the current package
*
* @param PackageFile $parentFile
* @param Package $package
* @return bool
*/
private function hasOverrides(PackageFile $parentFile, Package $package)
{
$this->buildMap(
$parentFile->getPackage()->getPath(),
$parentFile->getDeployedFileName(),
$parentFile->getDeployedFilePath()
);
$parentFiles = $this->collectFileMap($parentFile->getDeployedFilePath());
$currentPackageFiles = $package->getFiles();
$intersections = array_intersect_key($parentFiles, $currentPackageFiles);
if ($intersections) {
return true;
}
return false;
}
/**
* Build map file
*
* @param string $packagePath
* @param string $filePath
* @param string $fullPath
* @return void
*/
private function buildMap($packagePath, $filePath, $fullPath)
{
if (!isset($this->map[$fullPath])) {
$imports = [];
$this->map[$fullPath] = [];
$content = $this->staticDir->readFile($this->minification->addMinifiedSign($fullPath));
$callback = function ($matchContent) use ($packagePath, $filePath, & $imports) {
$importRelPath = $this->normalize(pathinfo($filePath, PATHINFO_DIRNAME) . '/' . $matchContent['path']);
$imports[$importRelPath] = $this->normalize(
$packagePath . '/' . pathinfo($filePath, PATHINFO_DIRNAME) . '/' . $matchContent['path']
);
};
preg_replace_callback(
\Magento\Framework\Css\PreProcessor\Instruction\Import::REPLACE_PATTERN,
$callback,
$content
);
preg_match_all(CssResolver::REGEX_CSS_RELATIVE_URLS, $content, $matches);
if (!empty($matches[0]) && !empty($matches[1])) {
$urls = array_combine($matches[0], $matches[1]);
foreach ($urls as $url) {
$importRelPath = $this->normalize(pathinfo($filePath, PATHINFO_DIRNAME) . '/' . $url);
$imports[$importRelPath] = $this->normalize(
$packagePath . '/' . pathinfo($filePath, PATHINFO_DIRNAME) . '/' . $url
);
}
}
$this->map[$fullPath] = $imports;
foreach ($imports as $importRelPath => $importFullPath) {
// only inner CSS files are concerned
if (strtolower(pathinfo($importFullPath, PATHINFO_EXTENSION)) === 'css') {
$this->buildMap($packagePath, $importRelPath, $importFullPath);
}
}
}
}
/**
* Flatten map tree into simple array
*
* Original map file information structure in form of tree,
* and to have checking of overridden files simpler we need to flatten that tree
*
* @param string $fileName
* @return array
*/
private function collectFileMap($fileName)
{
$result = isset($this->map[$fileName]) ? $this->map[$fileName] : [];
foreach ($result as $path) {
$result = array_merge($result, $this->collectFileMap($path));
}
return array_unique($result);
}
/**
* Return normalized path
*
* @param string $path
* @return string
*/
private function normalize($path)
{
if (strpos($path, '/../') === false) {
return $path;
}
$pathParts = explode('/', $path);
$realPath = [];
foreach ($pathParts as $pathPart) {
if ($pathPart == '.') {
continue;
}
if ($pathPart == '..') {
array_pop($realPath);
continue;
}
$realPath[] = $pathPart;
}
return implode('/', $realPath);
}
}