Completed
Push — master ( 3cc9f7...230e2b )
by Vitaly
07:31
created

CSS::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 7
rs 9.4285
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
    public function compile($resource, $extension, &$content)
32
    {
33
        $this->currentResource = $resource;
34
35
        // Rewrite Urls
36
        $content = preg_replace_callback(self::P_URL, [$this, 'rewriteUrls'], file_get_contents($resource));
37
    }
38
39
    /**
40
     * Callback for CSS url(...) rewriting.
41
     *
42
     * @param array $matches Regular expression matches collection
43
     *
44
     * @return string Rewritten url(..) with static resource handler url
45
     * @throws ResourceNotFound
46
     */
47
    public function rewriteUrls($matches)
48
    {
49
        // If we have found static resource path definition and its not inline
50
        if (array_key_exists(2, $matches) && strpos($matches[2], 'data:') === false) {
51
            // Store static resource path
52
            $url = $matches[2];
53
54
            // Ignore preprocessor vars
55
            // TODO: This is totally wrong need to come up with decision
56
            if (strpos($url, '@') !== false) {
57
                return $matches[0];
58
            }
59
60
            // Remove possible GET parameters from resource path
61 View Code Duplication
            if (($getStart = strpos($url, '?')) !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
                $url = substr($url, 0, $getStart);
63
            }
64
65
            // Remove possible HASH parameters from resource path
66 View Code Duplication
            if (($getStart = strpos($url, '#')) !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
                $url = substr($url, 0, $getStart);
68
            }
69
70
            // Try to find resource and output full error
71
            try {
72
                $path = Resource::getProjectRelativePath($url, dirname($this->currentResource));
73
            } catch (ResourceNotFound $e) {
74
                throw new ResourceNotFound('Cannot find resource "'.$url.'" in "'.$this->currentResource.'"');
75
            }
76
77
            // Build path to static resource handler
78
            return 'url("/' . STATIC_RESOURCE_HANDLER . '/?p=' . $path . '")';
79
        }
80
81
        return $matches[0];
82
    }
83
}
84