|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
|
4
|
|
|
* on 07.07.16 at 18:19 |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace samsonphp\resource; |
|
7
|
|
|
|
|
8
|
|
|
use samsonphp\resource\exception\ResourceNotFound; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* CSS assets handling class |
|
12
|
|
|
* |
|
13
|
|
|
* @author Vitaly Iegorov <[email protected]> |
|
14
|
|
|
* @package samsonphp\resource |
|
15
|
|
|
*/ |
|
16
|
|
|
class CSS |
|
17
|
|
|
{ |
|
18
|
|
|
/** Pattern for matching CSS url */ |
|
19
|
|
|
const P_URL = '/url\s*\(\s*(\'|\")?([^\)\s\'\"]+)(\'|\")?\s*\)/i'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string Path to current resource file */ |
|
22
|
|
|
protected $currentResource; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* LESS resource compiler. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $resource Resource full path |
|
28
|
|
|
* @param string $extension Resource extension |
|
29
|
|
|
* @param string $content Compiled output resource content |
|
30
|
|
|
*/ |
|
31
|
7 |
|
public function compile($resource, $extension, &$content) |
|
32
|
|
|
{ |
|
33
|
7 |
|
if ($extension === 'css') { |
|
34
|
7 |
|
$this->currentResource = $resource; |
|
35
|
|
|
|
|
36
|
|
|
// Rewrite Urls |
|
37
|
7 |
|
$content = preg_replace_callback(self::P_URL, [$this, 'rewriteUrls'], $content); |
|
38
|
5 |
|
} |
|
39
|
5 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Callback for CSS url(...) rewriting. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $matches Regular expression matches collection |
|
45
|
|
|
* |
|
46
|
|
|
* @return string Rewritten url(..) with static resource handler url |
|
47
|
|
|
* @throws ResourceNotFound |
|
48
|
|
|
*/ |
|
49
|
6 |
|
public function rewriteUrls($matches) |
|
50
|
|
|
{ |
|
51
|
|
|
// Store static resource path |
|
52
|
6 |
|
$url = $matches[2]; |
|
53
|
|
|
|
|
54
|
6 |
|
if (strpos($url, 'data:') === false) { |
|
55
|
|
|
// Remove possible GET parameters from resource path |
|
56
|
6 |
|
$url = $this->getOnlyUrl($url, '?'); |
|
57
|
|
|
|
|
58
|
|
|
// Remove possible HASH parameters from resource path |
|
59
|
6 |
|
$url = $this->getOnlyUrl($url, '#'); |
|
60
|
|
|
|
|
61
|
|
|
// Try to find resource and output full error |
|
62
|
|
|
try { |
|
63
|
6 |
|
$path = ResourceValidator::getProjectRelativePath($url, dirname($this->currentResource)); |
|
64
|
6 |
|
} catch (ResourceNotFound $e) { |
|
65
|
2 |
|
throw new ResourceNotFound('Cannot find resource "' . $url . '" in "' . $this->currentResource . '"'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Build path to static resource handler |
|
69
|
4 |
|
return 'url("/' . STATIC_RESOURCE_HANDLER . '/?p=' . $path . '")'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $matches[0]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get only path or URL before marker. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $path Full URL with possible unneeded data |
|
79
|
|
|
* @param string $marker Marker for separation |
|
80
|
|
|
* |
|
81
|
|
|
* @return string Filtered asset URL |
|
82
|
|
|
*/ |
|
83
|
6 |
|
protected function getOnlyUrl($path, $marker) |
|
84
|
|
|
{ |
|
85
|
|
|
// Remove possible GET parameters from resource path |
|
86
|
6 |
|
if (($getStart = strpos($path, $marker)) !== false) { |
|
87
|
4 |
|
return substr($path, 0, $getStart); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
6 |
|
return $path; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|