Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Less.php
Go to the documentation of this file.
1 <?php
7 
18 
25 class Less implements ProcessorInterface
26 {
32  private $fileNameResolver;
33 
37  private $notationResolver;
38 
42  private $deployStaticFile;
43 
47  private $minification;
48 
54  private $options = [];
55 
59  private $map = [];
60 
69  public function __construct(
70  FileNameResolver $fileNameResolver,
71  NotationResolver\Module $notationResolver,
72  DeployStaticFile $deployStaticFile,
73  Minification $minification
74  ) {
75  $this->fileNameResolver = $fileNameResolver;
76  $this->notationResolver = $notationResolver;
77  $this->deployStaticFile = $deployStaticFile;
78  $this->minification = $minification;
79  }
80 
84  public function process(Package $package, array $options)
85  {
86  $this->options = $options;
87  if ($this->options[DeployStaticOptions::NO_CSS] === true) {
88  return false;
89  }
90  if ($package->getArea() !== Package::BASE_AREA && $package->getTheme() !== Package::BASE_THEME) {
91  $files = $package->getParentFiles('less');
92  foreach ($files as $file) {
93  $packageFile = $package->getFile($file->getFileId());
94  if ($packageFile && $packageFile->getOrigPackage() === $package) {
95  continue;
96  }
97  $deployFileName = $this->fileNameResolver->resolve($file->getFileName());
98  if ($deployFileName !== $file->getFileName()) {
99  if ($this->hasOverrides($file, $package)) {
100  $file = clone $file;
101  $file->setArea($package->getArea());
102  $file->setTheme($package->getTheme());
103  $file->setLocale($package->getLocale());
104 
105  $file->setPackage($package);
106  $package->addFileToMap($file);
107  }
108  }
109  }
110  }
111  return true;
112  }
113 
123  private function hasOverrides(PackageFile $parentFile, Package $package)
124  {
125  $map = $this->buildMap(
126  $parentFile->getFilePath(),
127  $parentFile->getPackage()->getPath(),
128  $parentFile->getExtension()
129  );
131  $currentPackageFiles = array_merge($package->getFilesByType('less'), $package->getFilesByType('css'));
132 
133  foreach ($currentPackageFiles as $file) {
134  if ($this->inParentFiles($file->getDeployedFileName(), $parentFile->getFileName(), $map)) {
135  return true;
136  }
137  }
138  return false;
139  }
140 
147  private function inParentFiles($fileName, $parentFile, $map)
148  {
149  if (isset($map[$parentFile])) {
150  if (in_array($fileName, $map[$parentFile])) {
151  return true;
152  } else {
153  foreach ($map[$parentFile] as $pFile) {
154  return $this->inParentFiles($fileName, $pFile, $map);
155  }
156  }
157  }
158  return false;
159  }
160 
169  private function buildMap($filePath, $packagePath, $contentType)
170  {
171  $content = $this->deployStaticFile->readTmpFile($filePath, $packagePath);
172  $replaceCallback = function ($matchedContent) use ($filePath, $packagePath, $contentType) {
173  $matchedFileId = $matchedContent['path'];
174  if (!pathinfo($matchedContent['path'], PATHINFO_EXTENSION)) {
175  $matchedFileId .= '.' . $contentType;
176  }
177  if (strpos($matchedFileId, Repository::FILE_ID_SEPARATOR) !== false) {
178  $basePath = $packagePath;
179  } else {
180  $basePath = pathinfo($filePath, PATHINFO_DIRNAME);
181  }
182  $resolvedPath = str_replace(Repository::FILE_ID_SEPARATOR, '/', $matchedFileId);
183  if (strpos($resolvedPath, '@{baseUrl}') === 0) {
184  $resolvedMapPath = str_replace('@{baseUrl}', '', $resolvedPath);
185  } else {
186  $resolvedMapPath = $this->normalizePath($basePath . '/' . $resolvedPath);
187  }
188  if (!isset($this->map[$filePath])) {
189  $this->map[$filePath] = [];
190  }
191  $this->map[$filePath][] = $resolvedMapPath;
192  $this->buildMap($resolvedMapPath, $packagePath, $contentType);
193  };
194  if ($content) {
195  preg_replace_callback(Import::REPLACE_PATTERN, $replaceCallback, $content);
196  }
197  return $this->map;
198  }
199 
206  private function normalizePath($path)
207  {
208  if (strpos($path, '/../') === false) {
209  return $path;
210  }
211  $pathParts = explode('/', $path);
212  $realPath = [];
213  foreach ($pathParts as $pathPart) {
214  if ($pathPart == '.') {
215  continue;
216  }
217  if ($pathPart == '..') {
218  array_pop($realPath);
219  continue;
220  }
221  $realPath[] = $pathPart;
222  }
223  return implode('/', $realPath);
224  }
225 }
__construct(FileNameResolver $fileNameResolver, NotationResolver\Module $notationResolver, DeployStaticFile $deployStaticFile, Minification $minification)
Definition: Less.php:69
$fileName
Definition: translate.phtml:15
process(Package $package, array $options)
Definition: Less.php:84
foreach($appDirs as $dir) $files
addFileToMap(PackageFile $file)
Definition: Package.php:274