1 | <?php |
||
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) |
|
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 | 5 | $url = $this->getOnlyUrl($url, '?'); |
|
57 | |||
58 | // Remove possible HASH parameters from resource path |
||
59 | 5 | $url = $this->getOnlyUrl($url, '#'); |
|
60 | |||
61 | // Try to find resource and output full error |
||
62 | try { |
||
63 | 5 | $path = ResourceValidator::getProjectRelativePath($url, dirname($this->currentResource)); |
|
64 | 5 | } catch (ResourceNotFound $e) { |
|
65 | 1 | 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 | 1 | 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 | 5 | protected function getOnlyUrl($path, $marker) |
|
92 | } |
||
93 |