38 private $minification;
45 private $options = [];
56 $this->minification = $minification;
70 foreach (array_keys($package->
getMap()) as $fileId) {
72 if (strtolower(pathinfo($fileId, PATHINFO_EXTENSION)) ==
'css') {
73 $urlMap = $this->parseCss(
77 $this->staticDir->readFile(
78 $this->minification->addMinifiedSign($package->
getPath() .
'/' . $filePath)
84 $this->updateCssUrls($urlMap);
99 private function parseCss(array $urlMap, $cssFilePath, $packagePath, $cssContent, Package $package)
101 $cssFilePath = $this->minification->addMinifiedSign($cssFilePath);
103 $cssFileBasePath = pathinfo($cssFilePath, PATHINFO_DIRNAME);
104 $urls = $this->getCssUrls($cssContent);
106 if ($this->isExternalUrl(
$url)) {
108 'filePath' => $this->minification->addMinifiedSign($packagePath .
'/' . $cssFilePath),
109 'replace' => $this->getValidExternalUrl(
$url, $package)
113 $filePath = $this->getNormalizedFilePath($packagePath .
'/' . $cssFileBasePath .
'/' .
$url);
114 if ($this->staticDir->isReadable($this->minification->addMinifiedSign($filePath))) {
117 $lookupFileId = $this->getNormalizedFilePath($cssFileBasePath .
'/' .
$url);
119 $matchedFile = $this->getFileFromParent($lookupFileId, $package);
122 'filePath' => $this->minification->addMinifiedSign($packagePath .
'/' . $cssFilePath),
123 'replace' =>
'../../../../' 124 . str_repeat(
'../', count(explode(
'/', $cssFileBasePath)))
125 . $this->minification->addMinifiedSign($matchedFile->getDeployedFilePath())
138 private function updateCssUrls(array $urlMap)
140 foreach ($urlMap as $ref => $targetFiles) {
141 foreach ($targetFiles as $matchedFileData) {
142 $filePath = $matchedFileData[
'filePath'];
143 $oldCss = $this->staticDir->readFile($filePath);
144 $newCss = str_replace($ref, $matchedFileData[
'replace'], $oldCss);
145 if ($oldCss !== $newCss) {
146 $this->staticDir->writeFile($filePath, $newCss);
158 private function getCssUrls($cssContent)
162 if (!empty($matches[0]) && !empty($matches[1])) {
163 $urls = array_combine($matches[0], $matches[1]);
174 private function getNormalizedFilePath(
$url)
176 $urlParts = explode(
'/',
$url);
178 if (preg_match(
'/{{.*}}/',
$url)) {
179 foreach (array_reverse($urlParts) as
$index => $part) {
180 if (!preg_match(
'/^{{.*}}$/', $part)) {
186 return implode(
'/', array_reverse(
$result));
189 foreach ($urlParts as
$index => $part) {
192 unset($urlParts[$prevIndex]);
198 return implode(
'/', $urlParts);
208 private function getValidExternalUrl(
$url, Package $package)
210 $url = $this->minification->removeMinifiedSign(
$url);
211 $filePath = $this->getNormalizedFilePath(
$url);
212 if (!$this->isFileExistsInPackage($filePath, $package)) {
214 $matchedFile = $this->getFileFromParent($filePath, $package);
215 $package = $matchedFile->getPackage();
218 '/(?<=}})(.*)(?=\/{{)/',
219 $package->getArea() .
'/' . $package->getTheme(),
220 $this->minification->addMinifiedSign(
$url)
231 private function getFileFromParent(
$fileName, Package $currentPackage)
234 foreach (array_reverse($currentPackage->getParentPackages()) as $package) {
235 foreach ($package->getFiles() as $file) {
236 if ($file->getDeployedFileName() ===
$fileName) {
250 private function isExternalUrl(
$url)
252 return preg_match(
'/{{.*}}/',
$url);
262 private function isFileExistsInPackage($filePath, Package $package)
265 foreach ($package->getFiles() as $file) {
266 if ($file->getDeployedFileName() === $filePath) {
__construct(Filesystem $filesystem, Minification $minification)
process(Package $package, array $options)
const REGEX_CSS_RELATIVE_URLS