/
Import.php
156 lines (139 loc) · 4.67 KB
/
Import.php
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
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
namespace Magento\Framework\Css\PreProcessor\Instruction;
use Magento\Framework\View\Asset\LocalInterface;
use Magento\Framework\View\Asset\NotationResolver;
use Magento\Framework\View\Asset\PreProcessorInterface;
use Magento\Framework\Css\PreProcessor\FileGenerator\RelatedGenerator;
/**
* @import instruction preprocessor
*/
class Import implements PreProcessorInterface
{
/**
* Pattern of @import instruction
*/
const REPLACE_PATTERN =
'#@import\s+(\((?P<type>\w+)\)\s+)?[\'\"](?P<path>(?![/\\\]|\w:[/\\\])[^\"\']+)[\'\"]\s*?(?P<media>.*?);#';
/**
* @var \Magento\Framework\View\Asset\NotationResolver\Module
*/
private $notationResolver;
/**
* @var array
*/
protected $relatedFiles = [];
/**
* @var RelatedGenerator
*/
private $relatedFileGenerator;
/**
* Constructor
*
* @param NotationResolver\Module $notationResolver
* @param RelatedGenerator $relatedFileGenerator
*/
public function __construct(
NotationResolver\Module $notationResolver,
RelatedGenerator $relatedFileGenerator
) {
$this->notationResolver = $notationResolver;
$this->relatedFileGenerator = $relatedFileGenerator;
}
/**
* {@inheritdoc}
*/
public function process(\Magento\Framework\View\Asset\PreProcessor\Chain $chain)
{
$asset = $chain->getAsset();
$contentType = $chain->getContentType();
$replaceCallback = function ($matchContent) use ($asset, $contentType) {
return $this->replace($matchContent, $asset, $contentType);
};
$content = $this->removeComments($chain->getContent());
$processedContent = preg_replace_callback(self::REPLACE_PATTERN, $replaceCallback, $content);
$this->relatedFileGenerator->generate($this);
if ($processedContent !== $content) {
$chain->setContent($processedContent);
}
}
/**
* Returns the content without commented lines
*
* @param string $content
* @return string
*/
private function removeComments($content)
{
return preg_replace("#(^\s*//.*$)|((^\s*/\*(?s).*?(\*/)(?!\*/))$)#m", '', $content);
}
/**
* Retrieve information on all related files, processed so far
*
* BUG: this information about related files is not supposed to be in the state of this object.
* This class is meant to be a service (shareable instance) without such a transient state.
* The list of related files needs to be accumulated for the preprocessor,
* because it uses a 3rd-party library, which requires the files to physically reside in the base same directory.
*
* @return array
*/
public function getRelatedFiles()
{
return $this->relatedFiles;
}
/**
* Clear the record of related files, processed so far
* @return void
*/
public function resetRelatedFiles()
{
$this->relatedFiles = [];
}
/**
* Add related file to the record of processed files
*
* @param string $matchedFileId
* @param LocalInterface $asset
* @return void
*/
protected function recordRelatedFile($matchedFileId, LocalInterface $asset)
{
$this->relatedFiles[] = [$matchedFileId, $asset];
}
/**
* Return replacement of an original @import directive
*
* @param array $matchedContent
* @param LocalInterface $asset
* @param string $contentType
* @return string
*/
protected function replace(array $matchedContent, LocalInterface $asset, $contentType)
{
$matchedFileId = $this->fixFileExtension($matchedContent['path'], $contentType);
$this->recordRelatedFile($matchedFileId, $asset);
$resolvedPath = $this->notationResolver->convertModuleNotationToPath($asset, $matchedFileId);
$typeString = empty($matchedContent['type']) ? '' : '(' . $matchedContent['type'] . ') ';
$mediaString = empty($matchedContent['media']) ? '' : ' ' . trim($matchedContent['media']);
return "@import {$typeString}'{$resolvedPath}'{$mediaString};";
}
/**
* Resolve extension of imported asset according to exact format
*
* @param string $fileId
* @param string $contentType
* @return string
* @link http://lesscss.org/features/#import-directives-feature-file-extensions
*/
protected function fixFileExtension($fileId, $contentType)
{
if (!pathinfo($fileId, PATHINFO_EXTENSION)) {
$fileId .= '.' . $contentType;
}
return $fileId;
}
}