Completed
Push — master ( 195796...d42aae )
by Vitaly
07:47 queued 04:52
created

CSS::rewriteUrls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 2
eloc 9
c 3
b 0
f 2
nc 2
nop 1
dl 0
loc 21
ccs 8
cts 8
cp 1
crap 2
rs 9.3142
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 6
        }
39 6
    }
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 5
    public function rewriteUrls($matches)
50
    {
51
        // Store static resource path
52 5
        $url = $matches[2];
53
54
        // Remove possible GET parameters from resource path
55 5
        $url = $this->getOnlyUrl($url, '?');
56
57
        // Remove possible HASH parameters from resource path
58 5
        $url = $this->getOnlyUrl($url, '#');
59
60
        // Try to find resource and output full error
61
        try {
62 5
            $path = ResourceValidator::getProjectRelativePath($url, dirname($this->currentResource));
63 5
        } catch (ResourceNotFound $e) {
64 1
            throw new ResourceNotFound('Cannot find resource "' . $url . '" in "' . $this->currentResource . '"');
65
        }
66
67
        // Build path to static resource handler
68 4
        return 'url("/' . STATIC_RESOURCE_HANDLER . '/?p=' . $path . '")';
69
    }
70
71
    /**
72
     * Get only path or URL before marker.
73
     *
74
     * @param string $path   Full URL with possible unneeded data
75
     * @param string $marker Marker for separation
76
     *
77
     * @return string Filtered asset URL
78
     */
79 5
    protected function getOnlyUrl($path, $marker)
80
    {
81
        // Remove possible GET parameters from resource path
82 5
        if (($getStart = strpos($path, $marker)) !== false) {
83 4
            return substr($path, 0, $getStart);
84
        }
85
86 5
        return $path;
87
    }
88
}
89