Completed
Push — master ( 89ce38...d45872 )
by Vitaly
32s
created

CSS::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 2
b 0
f 1
nc 2
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 9.6666
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