Passed
Push — master ( f38dde...147718 )
by Fran
02:30
created

AssetsParser::extractCssLineResource()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 2
b 0
f 0
nc 5
nop 2
dl 0
loc 16
ccs 0
cts 13
cp 0
crap 30
rs 9.5222
1
<?php
2
3
namespace PSFS\base\extension;
4
5
use PSFS\base\config\Config;
6
use PSFS\base\exception\ConfigException;
7
use PSFS\base\extension\traits\CssTrait;
8
use PSFS\base\extension\traits\JsTrait;
9
use PSFS\base\Request;
10
use PSFS\base\Template;
11
12
/**
13
 * Class AssetsParser
14
 * @package PSFS\base\extension
15
 */
16
class AssetsParser
17
{
18
    use CssTrait;
19
    use JsTrait;
20
21
    /**
22
     * @var array
23
     */
24
    protected $files = [];
25
    /**
26
     * @var string
27
     */
28
    protected $hash;
29
    /**
30
     * @var array
31
     */
32
    protected $compiled_files = [];
33
    /**
34
     * @var string
35
     */
36
    protected $type;
37
    /**
38
     * @var array
39
     */
40
    protected $domains = [];
41
    /**
42
     * @var bool
43
     */
44
    private $debug = false;
45
    /**
46
     * @var string
47
     */
48
    private $cdnPath = null;
49
50
    /**
51
     * Constructor por defecto
52
     *
53
     * @param string $type
54
     */
55 1
    public function __construct($type = 'js')
56
    {
57 1
        $this->type = $type;
58 1
        $this->path = WEB_DIR . DIRECTORY_SEPARATOR;
59 1
        $this->domains = Template::getDomains(true);
60 1
        $this->debug = Config::getParam('debug', true);
61 1
        $this->cdnPath = Config::getParam('resources.cdn.url', Request::getInstance()->getRootUrl());
62 1
    }
63
64
    /**
65
     * Método que añade un nuevo fichero al proceso de generación de los assets
66
     * @param $filename
67
     * @return AssetsParser
68
     * @internal param string $type
69
     */
70 1
    public function addFile($filename)
71
    {
72 1
        if (file_exists($this->path . $filename) && preg_match('/\.' . $this->type . '$/i', $filename)) {
73
            $this->files[] = $filename;
74 1
        } elseif (!empty($this->domains)) {
75 1
            foreach ($this->domains as $domain => $paths) {
76 1
                $domainFilename = str_replace($domain, $paths["public"], $filename);
77 1
                if (file_exists($domainFilename) && preg_match('/\.' . $this->type . '$/i', $domainFilename)) {
78 1
                    $this->files[] = $domainFilename;
79
                }
80
            }
81
        }
82 1
        return $this;
83
    }
84
85
    /**
86
     * Método que establece el hash con el que compilar los assets
87
     * @param string $hash
88
     *
89
     * @return AssetsParser
90
     */
91 1
    public function setHash($hash)
92
    {
93 1
        $cache = Config::getParam('cache.var', '');
94 1
        $this->hash = $hash . (strlen($cache) ? '.' : '') . $cache;
95 1
        return $this;
96
    }
97
98
    /**
99
     * Método que procesa los ficheros solicitados en función del modo de ejecución
100
     * @return AssetsParser
101
     * @internal param string $type
102
     * @throws ConfigException
103
     * @throws \PSFS\base\exception\GeneratorException
104
     */
105 1
    public function compile()
106
    {
107
        //Unificamos ficheros para que no se retarde mucho el proceso
108 1
        $this->files = array_unique($this->files);
109 1
        switch ($this->type) {
110
            default:
111 1
            case "js":
112 1
                $this->compileJs($this->files, $this->path, $this->hash, $this->debug);
113 1
                break;
114 1
            case "css":
115 1
                $this->compileCss($this->path, $this->hash, $this->debug);
116 1
                break;
117
        }
118
119 1
        return $this;
120
    }
121
122
    /**
123
     * Método que imprime el resultado de la generación de los assets
124
     */
125 1
    public function printHtml()
126
    {
127 1
        $baseUrl = $this->cdnPath ?: '';
128 1
        switch ($this->type) {
129
            default:
130 1
            case "js":
131 1
                $this->printJs($this->compiled_files, $baseUrl, $this->hash, $this->debug);
132 1
                break;
133 1
            case "css":
134 1
                $this->printCss($this->compiled_files, $baseUrl, $this->hash, $this->debug);
135 1
                break;
136
        }
137 1
    }
138
139
}
140