|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: egorov |
|
5
|
|
|
* Date: 13.08.2015 |
|
6
|
|
|
* Time: 10:43 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace samsonphp\compressor\resource; |
|
9
|
|
|
use samsonphp\compressor\Compressor; |
|
10
|
|
|
use samson\core\Module; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Module compression CSS resource management |
|
14
|
|
|
* @package samsonphp\compressor |
|
15
|
|
|
*/ |
|
16
|
|
|
class CSS extends Generic |
|
17
|
|
|
{ |
|
18
|
|
|
/** CSS url() matching pattern */ |
|
19
|
|
|
const PAT_URL = '/url\s*\(\s*(\'|\")*(?<url>[^\'\"\)]+)\s*(\'|\")*\)/i'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Module URL resolving module */ |
|
22
|
|
|
protected $resolver; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string[] Collection of ignored file extensions */ |
|
25
|
|
|
protected $ignoredExtensions = array('php', 'js', 'md', 'map', 'dbs', 'vphp', 'less', 'gz', 'lock', 'json', 'sql', 'xml', 'yml'); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Compressor $compressor Pointer to parent compressing object |
|
29
|
|
|
* @param Module $compressor URL resolving module |
|
30
|
|
|
* @param string[] $ignoredExtensions Collection of ignored file extensions |
|
31
|
|
|
* @param string[] $ignoredFiles Collection of ignored files |
|
32
|
|
|
* @param string[] $ignoredFolders Collection of ignored folders |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
Compressor & $compressor, |
|
36
|
|
|
Module $resolver, |
|
37
|
|
|
$ignoredExtensions = array(), |
|
38
|
|
|
$ignoredFiles = array(), |
|
39
|
|
|
$ignoredFolders = array()) |
|
|
|
|
|
|
40
|
|
|
{ |
|
|
|
|
|
|
41
|
|
|
$this->resolver = $resolver; |
|
42
|
|
|
|
|
43
|
|
|
parent::__construct($compressor, $ignoredExtensions, $ignoredFiles, $ignoredFolders); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Update file resource |
|
48
|
|
|
* @param string $fromFilePath Source file path |
|
49
|
|
|
* @param string $toFilePath Destination file path |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function update($fromFilePath, $toFilePath) |
|
52
|
|
|
{ |
|
53
|
|
|
// Read source file |
|
54
|
|
|
$text = file_get_contents($fromFilePath); |
|
55
|
|
|
|
|
56
|
|
|
// Найдем ссылки в ресурса |
|
57
|
|
|
if (preg_match_all(self::PAT_URL, $text, $matches)) { |
|
58
|
|
|
// Если мы нашли шаблон - переберем все найденные патерны |
|
59
|
|
|
if (isset($matches['url'])) { |
|
60
|
|
|
foreach ($matches['url'] as $url) { |
|
61
|
|
|
$module = ''; |
|
62
|
|
|
$path = ''; |
|
63
|
|
|
// Получим путь к ресурсу используя маршрутизацию |
|
64
|
|
|
if ($this->resolver->parseURL($url, $module, $path)) { |
|
65
|
|
|
//trace($matches['url'][$i].'-'.url()->base().$module.'/'.$path); |
|
66
|
|
|
// Always remove first public path /www/ |
|
67
|
|
|
$path = ltrim(str_replace(__SAMSON_PUBLIC_PATH, '', $path), '/'); |
|
68
|
|
|
// Заменим путь в исходном файле |
|
69
|
|
|
$text = str_replace($url, url()->base() . ($module == 'local' ? '' : $module . '/www/') . $path, $text); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Write destination file |
|
76
|
|
|
file_put_contents($toFilePath, $text); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|