1 | <?php |
||
18 | abstract class MinifyComponent |
||
19 | { |
||
20 | |||
21 | const CACHE_TAG = 'minify-view-tag'; |
||
22 | |||
23 | /** |
||
24 | * @var View |
||
25 | */ |
||
26 | protected $view; |
||
27 | |||
28 | /** |
||
29 | * MinifyComponent constructor. |
||
30 | * @param View $view |
||
31 | */ |
||
32 | 9 | public function __construct(View $view) |
|
33 | { |
||
34 | 9 | $this->view = $view; |
|
35 | 9 | } |
|
36 | |||
37 | abstract public function export(); |
||
38 | |||
39 | /** |
||
40 | * @param string $file |
||
41 | * @return string |
||
42 | */ |
||
43 | 8 | protected function getAbsoluteFilePath($file) |
|
50 | |||
51 | /** |
||
52 | * @param string $file |
||
53 | * @return string |
||
54 | */ |
||
55 | 8 | protected function cleanFileName($file) |
|
61 | |||
62 | /** |
||
63 | * @param string $file |
||
64 | * @param string $html |
||
65 | * @return bool |
||
66 | */ |
||
67 | 8 | protected function thisFileNeedMinify($file, $html) |
|
73 | |||
74 | /** |
||
75 | * @param string $url |
||
76 | * @param boolean $checkSlash |
||
77 | * @return bool |
||
78 | */ |
||
79 | protected function isUrl($url, $checkSlash = true) |
||
80 | { |
||
81 | 8 | $schemas = array_map(function ($val) { |
|
82 | 8 | return str_replace('/', '\/', $val); |
|
83 | 8 | }, $this->view->schemas); |
|
84 | |||
85 | 8 | $regexp = '#^(' . implode('|', $schemas) . ')#i'; |
|
86 | |||
87 | 8 | if ($checkSlash) { |
|
88 | 6 | $regexp = '#^(/|\\\\|' . implode('|', $schemas) . ')#i'; |
|
89 | } |
||
90 | |||
91 | 8 | return (bool)preg_match($regexp, $url); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param string $string |
||
96 | * @return bool |
||
97 | */ |
||
98 | 8 | protected function isContainsConditionalComment($string) |
|
102 | |||
103 | /** |
||
104 | * @param string $file |
||
105 | * @return bool |
||
106 | */ |
||
107 | 8 | protected function isExcludedFile($file) |
|
108 | { |
||
109 | 8 | $result = false; |
|
110 | |||
111 | 8 | foreach ((array)$this->view->excludeFiles as $excludedFile) { |
|
112 | 2 | if (!preg_match('!' . $excludedFile . '!i', $file)) { |
|
113 | 2 | continue; |
|
114 | } |
||
115 | |||
116 | 2 | $result = true; |
|
117 | 2 | break; |
|
118 | } |
||
119 | |||
120 | 8 | return $result; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param string $resultFile |
||
125 | * @return string |
||
126 | */ |
||
127 | 8 | protected function prepareResultFile($resultFile) |
|
128 | { |
||
129 | 8 | $basePath = $this->view->basePath; |
|
130 | 8 | $webPath = $this->view->webPath; |
|
131 | |||
132 | 8 | $file = sprintf('%s%s', $webPath, str_replace($basePath, '', $resultFile)); |
|
133 | |||
134 | 8 | $AssetManager = $this->view->getAssetManager(); |
|
135 | |||
136 | 8 | if ($AssetManager->appendTimestamp && ($timestamp = @filemtime($resultFile)) > 0) { |
|
137 | 2 | $file .= '?v=' . $timestamp; |
|
138 | } |
||
139 | |||
140 | 8 | return $file; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param array $files |
||
145 | * @return string |
||
146 | */ |
||
147 | 8 | protected function _getSummaryFilesHash($files) |
|
148 | { |
||
149 | 8 | $result = ''; |
|
150 | |||
151 | 8 | foreach ($files as $file => $html) { |
|
152 | 8 | $path = $this->getAbsoluteFilePath($file); |
|
153 | |||
154 | 8 | if (!$this->thisFileNeedMinify($file, $html) || !file_exists($path)) { |
|
155 | continue; |
||
156 | } |
||
157 | |||
158 | 8 | switch ($this->view->fileCheckAlgorithm) { |
|
159 | default: |
||
160 | 8 | case 'filemtime': |
|
161 | 1 | $result .= filemtime($path) . $file; |
|
162 | 1 | break; |
|
163 | 7 | case 'sha1': // deprecated |
|
164 | 7 | case 'hash': |
|
165 | 7 | $result .= hash_file($this->view->currentHashAlgo, $path); |
|
166 | 7 | break; |
|
167 | } |
||
168 | } |
||
169 | |||
170 | 8 | return hash($this->view->currentHashAlgo, $result); |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param string $file |
||
175 | * @return string |
||
176 | */ |
||
177 | 8 | protected function buildCacheKey($file) |
|
181 | |||
182 | /** |
||
183 | * @param string $key |
||
184 | * @return string|false |
||
185 | */ |
||
186 | 8 | protected function getFromCache($key) |
|
194 | |||
195 | /** |
||
196 | * @param string $key |
||
197 | * @param string $content |
||
198 | * @return bool |
||
199 | */ |
||
200 | 8 | protected function saveToCache($key, $content) |
|
208 | } |
||
209 |