Passed
Push — master ( 147718...79af4d )
by Fran
02:42
created

AssetsParser::setHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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