Passed
Push — master ( 96a485...3c999b )
by Fran
02:49
created

JsTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 76.19%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 100
ccs 32
cts 42
cp 0.7619
rs 10
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
B compileJs() 0 28 9
A dumpJs() 0 9 2
A putDebugJs() 0 10 3
A printJs() 0 10 4
1
<?php
2
namespace PSFS\base\extension\traits;
3
4
use MatthiasMullie\Minify\JS;
5
use PSFS\base\config\Config;
6
use PSFS\base\types\helpers\AssetsHelper;
7
use PSFS\base\types\helpers\GeneratorHelper;
8
9
/**
10
 * Trait JsTrait
11
 * @package PSFS\base\extension\traits
12
 */
13
trait JsTrait {
14
15
    use SRITrait {
16
        getSriHash as getJsSRIHash;
17
    }
18
19
    /**
20
     * @param array $compiledFiles
21
     * @param string $baseUrl
22
     * @param string $hash
23
     * @throws \PSFS\base\exception\GeneratorException
24
     */
25 1
    protected function printJs(array $compiledFiles, $baseUrl, $hash)
26
    {
27 1
        if (Config::getParam('debug') && 0 < count($compiledFiles)) {
28 1
            foreach ($compiledFiles as $file) {
29 1
                echo "\t\t<script type='text/javascript' src='{$file}'></script>\n";
30
            }
31
        } else {
32 1
            $sri = $this->getJsSRIHash($hash, 'js');
33 1
            echo "\t\t<script type='text/javascript' src='" . $baseUrl . "/js/" . $hash . ".js'" .
34 1
                " crossorigin='anonymous' integrity='sha384-" . $sri . "'></script>\n";
35
        }
36 1
    }
37
38
    /**
39
     * @param $pathParts
40
     * @param string $base
41
     * @param string $file
42
     * @param string $hash
43
     * @param array $compiledFiles
44
     * @return false|string
45
     * @throws \PSFS\base\exception\GeneratorException
46
     */
47 1
    protected function putDebugJs($pathParts, $base, $file, $hash, array &$compiledFiles)
48
    {
49 1
        $filePath = $hash . "_" . $pathParts[count($pathParts) - 1];
50 1
        $compiledFiles[] = "/js/" . $filePath;
51 1
        $data = "";
52 1
        if (!file_exists($base . $filePath) || filemtime($base . $filePath) < filemtime($file)) {
53 1
            $data = file_get_contents($file);
54 1
            AssetsHelper::storeContents($base . $filePath, $data);
55
        }
56 1
        return $data;
57
    }
58
59
    /**
60
     * Método que compila los ficheros javascript en función del modo de ejecución
61
     * @param array $files
62
     * @param string $basePath
63
     * @param string $hash
64
     * @param array $compiledFiles
65
     * @return $this
66
     * @throws \PSFS\base\exception\GeneratorException
67
     */
68 1
    protected function compileJs(array $files, $basePath, $hash, array &$compiledFiles)
69
    {
70 1
        $base = $basePath . "js" . DIRECTORY_SEPARATOR;
71 1
        $debug = Config::getParam('debug');
72 1
        if ($debug || !file_exists($base . $hash . ".js")) {
73 1
            $data = '';
74 1
            if (0 < count($files)) {
75 1
                $minifier = new JS();
76 1
                foreach ($files as $file) {
77 1
                    $pathParts = explode("/", $file);
78 1
                    if (file_exists($file)) {
79 1
                        if ($debug) {
80 1
                            $data = $this->putDebugJs($pathParts, $base, $file, $hash, $compiledFiles);
81
                        } elseif (!file_exists($base . $hash . ".js")) {
82
                            $minifier->add($file);
83
                            //$data = $this->putProductionJs($base, $file, $data);
84
                        }
85
                    }
86
                }
87 1
                if($debug) {
88 1
                    AssetsHelper::storeContents($base . $hash . ".js", $data);
89
                } else {
90
                    $this->dumpJs($hash, $base, $minifier);
91
                }
92 1
                unset($minifier);
93
            }
94
        }
95 1
        return $this;
96
    }
97
98
    /**
99
     * @param $hash
100
     * @param string $base
101
     * @param JS $minifier
102
     * @throws \PSFS\base\exception\GeneratorException
103
     */
104
    protected function dumpJs($hash, string $base, JS $minifier)
105
    {
106
        ini_set('max_execution_time', -1);
107
        ini_set('memory_limit', -1);
108
        GeneratorHelper::createDir($base);
109
        if (Config::getParam('assets.obfuscate', false)) {
110
            $minifier->gzip($base . $hash . ".js");
111
        } else {
112
            $minifier->minify($base . $hash . ".js");
113
        }
114
    }
115
}
116