|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
|
4
|
|
|
* on 07.07.16 at 18:19 |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace samsonphp\css; |
|
7
|
|
|
|
|
8
|
|
|
use samson\core\ExternalModule; |
|
9
|
|
|
use samsonphp\compressor\Compressor; |
|
10
|
|
|
use samsonphp\event\Event; |
|
11
|
|
|
use samsonphp\resource\exception\ResourceNotFound; |
|
12
|
|
|
use samsonphp\resource\ResourceValidator; |
|
13
|
|
|
use samsonphp\resource\Router; |
|
14
|
|
|
use samsonphp\resource\ResourceManager; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* CSS assets handling class |
|
18
|
|
|
* |
|
19
|
|
|
* @author Vitaly Iegorov <[email protected]> |
|
20
|
|
|
* @package samsonphp\resource |
|
21
|
|
|
* TODO: Remove ResourceValidator as it is unnecessary |
|
22
|
|
|
*/ |
|
23
|
|
|
class CSS extends ExternalModule |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var string Module identifer */ |
|
26
|
|
|
protected $id = 'resource_css'; |
|
27
|
|
|
|
|
28
|
|
|
/** Pattern for matching CSS url */ |
|
29
|
|
|
const P_URL = '/url\s*\(\s*(\'|\")?([^\)\s\'\"]+)(\'|\")?\s*\)/i'; |
|
30
|
|
|
|
|
31
|
|
|
/** Event for firing before handling CSS resource */ |
|
32
|
|
|
const E_BEFORE_HANDLER = 'samsonphp.css.before_handle'; |
|
33
|
|
|
|
|
34
|
|
|
/** Event for firing after handling CSS resource */ |
|
35
|
|
|
const E_AFTER_HANDLER = 'samsonphp.css.after_handle'; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string Path to current resource file */ |
|
38
|
|
|
protected $currentResource; |
|
39
|
|
|
|
|
40
|
7 |
|
/** Module preparation stage handler */ |
|
41
|
|
|
public function prepare(array $params = []) |
|
42
|
|
|
{ |
|
43
|
7 |
|
// Subscribe for CSS handling |
|
44
|
|
|
Event::subscribe(Router::E_RESOURCE_COMPILE, [$this, 'compile']); |
|
45
|
7 |
|
|
|
46
|
|
|
Event::subscribe(Compressor::E_RESOURCE_COMPRESS, [$this, 'deCompile']); |
|
47
|
|
|
|
|
48
|
|
|
return parent::prepare($params); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* LESS resource compiler. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $resource Resource full path |
|
55
|
7 |
|
* @param string $extension Resource extension |
|
56
|
|
|
* @param string $content Compiled output resource content |
|
57
|
7 |
|
*/ |
|
58
|
7 |
|
public function compile($resource, $extension, &$content) |
|
59
|
|
|
{ |
|
60
|
|
|
if (in_array($extension, [ResourceManager::T_CSS, ResourceManager::T_LESS, ResourceManager::T_SASS, ResourceManager::T_SCSS])) { |
|
61
|
7 |
|
$this->currentResource = $resource; |
|
62
|
|
|
|
|
63
|
|
|
// Fire event |
|
64
|
7 |
|
Event::fire(self::E_BEFORE_HANDLER, [&$content, $resource]); |
|
65
|
|
|
|
|
66
|
|
|
// Rewrite Urls |
|
67
|
6 |
|
$content = preg_replace_callback(self::P_URL, [$this, 'rewriteUrls'], $content); |
|
68
|
6 |
|
|
|
69
|
6 |
|
// Fire event |
|
70
|
|
|
Event::fire(self::E_AFTER_HANDLER, [&$content, $resource]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function deCompile($extension, &$content) |
|
75
|
|
|
{ |
|
76
|
|
|
if (in_array($extension, [ResourceManager::T_CSS, ResourceManager::T_LESS, ResourceManager::T_SASS, ResourceManager::T_SCSS])) { |
|
77
|
|
|
if (preg_match_all('/url\s*\(\s*(\'|\")*(?<url>[^\'\"\)]+)\s*(\'|\")*\)/i', $content, $matches)) { |
|
78
|
|
|
if (isset($matches['url'])) { |
|
79
|
7 |
|
foreach ($matches['url'] as $url) { |
|
|
|
|
|
|
80
|
|
|
if (preg_match('/' . STATIC_RESOURCE_HANDLER . '\/\?p=(((\/src\/|\/vendor\/samson[^\/]+\/)(?<module>[^\/]+)(?<path>.+))|((?<local>.+)))/ui', $url, $matches)) { |
|
81
|
|
|
if (array_key_exists('local', $matches)) { |
|
82
|
7 |
|
$module = 'local'; |
|
83
|
|
|
$path = $matches['local']; |
|
84
|
|
|
} else { |
|
85
|
7 |
|
$module = $matches['module']; |
|
86
|
7 |
|
$path = $matches['path']; |
|
87
|
7 |
|
} |
|
88
|
|
|
// Always remove first public path /www/ |
|
89
|
|
|
$path = ltrim(str_replace(__SAMSON_PUBLIC_PATH, '', $path), '/'); |
|
90
|
7 |
|
// Replace url in file |
|
91
|
|
|
$content = str_replace($url, url()->base() . ($module == 'local' ? '' : $module . '/www/') . $path, $content); |
|
92
|
4 |
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
4 |
|
} |
|
97
|
4 |
|
} |
|
98
|
1 |
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Callback for CSS url(...) rewriting. |
|
101
|
|
|
* |
|
102
|
3 |
|
* @param array $matches Regular expression matches collection |
|
103
|
|
|
* |
|
104
|
|
|
* @return string Rewritten url(..) with static resource handler url |
|
105
|
3 |
|
* @throws ResourceNotFound |
|
106
|
|
|
*/ |
|
107
|
|
|
public function rewriteUrls($matches) |
|
108
|
|
|
{ |
|
109
|
|
|
// Store static resource path |
|
110
|
|
|
$url = $matches[2]; |
|
111
|
|
|
|
|
112
|
|
|
// Validate url for restricted protocols and inline images |
|
113
|
|
|
$validation = array_filter(['data/', 'data:', 'http:', 'https:'], function ($item) use ($url) { |
|
114
|
|
|
return strpos($url, $item) !== false; |
|
115
|
|
|
}); |
|
116
|
4 |
|
|
|
117
|
|
|
// Ignore inline resources |
|
118
|
|
|
if (!count($validation)) { |
|
119
|
4 |
|
// Remove possible GET, HASH parameters from resource path |
|
120
|
3 |
|
$url = $this->getOnlyUrl($this->getOnlyUrl($url, '#'), '?'); |
|
121
|
|
|
|
|
122
|
|
|
// Try to find resource and output full error |
|
123
|
4 |
|
try { |
|
124
|
|
|
$path = ResourceValidator::getProjectRelativePath($url, dirname($this->currentResource)); |
|
125
|
|
|
} catch (ResourceNotFound $e) { |
|
126
|
|
|
throw new ResourceNotFound('Cannot find resource "' . $url . '" in "' . $this->currentResource . '"'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// Build path to static resource handler |
|
130
|
|
|
return 'url("/' . STATIC_RESOURCE_HANDLER . '/?p=' . $path . '")'; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $matches[0]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get only path or URL before marker. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $path Full URL with possible unneeded data |
|
140
|
|
|
* @param string $marker Marker for separation |
|
141
|
|
|
* |
|
142
|
|
|
* @return string Filtered asset URL |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function getOnlyUrl($path, $marker) |
|
145
|
|
|
{ |
|
146
|
|
|
// Remove possible GET parameters from resource path |
|
147
|
|
|
if (($getStart = strpos($path, $marker)) !== false) { |
|
148
|
|
|
return substr($path, 0, $getStart); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $path; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.