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

JsTrait::dumpJs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 3
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
    /**
16
     * @param array $compiledFiles
17
     * @param string $baseUrl
18
     * @param string $hash
19
     */
20 1
    protected function printJs(array $compiledFiles, $baseUrl, $hash)
21
    {
22 1
        if (Config::getParam('debug') && 0 < count($compiledFiles)) {
23 1
            foreach ($compiledFiles as $file) {
24 1
                echo "\t\t<script type='text/javascript' src='{$file}'></script>\n";
25
            }
26
        } else {
27 1
            echo "\t\t<script type='text/javascript' src='" . $baseUrl . "/js/" . $hash . ".js'></script>\n";
28
        }
29 1
    }
30
31
    /**
32
     * @param $pathParts
33
     * @param string $base
34
     * @param string $file
35
     * @param string $hash
36
     * @param array $compiledFiles
37
     * @return false|string
38
     * @throws \PSFS\base\exception\GeneratorException
39
     */
40 1
    protected function putDebugJs($pathParts, $base, $file, $hash, array &$compiledFiles)
41
    {
42 1
        $filePath = $hash . "_" . $pathParts[count($pathParts) - 1];
43 1
        $compiledFiles[] = "/js/" . $filePath;
44 1
        $data = "";
45 1
        if (!file_exists($base . $filePath) || filemtime($base . $filePath) < filemtime($file)) {
46 1
            $data = file_get_contents($file);
47 1
            AssetsHelper::storeContents($base . $filePath, $data);
48
        }
49 1
        return $data;
50
    }
51
52
    /**
53
     * Método que compila los ficheros javascript en función del modo de ejecución
54
     * @param array $files
55
     * @param string $basePath
56
     * @param string $hash
57
     * @param array $compiledFiles
58
     * @return $this
59
     * @throws \PSFS\base\exception\GeneratorException
60
     */
61 1
    protected function compileJs(array $files, $basePath, $hash, array &$compiledFiles)
62
    {
63 1
        $base = $basePath . "js" . DIRECTORY_SEPARATOR;
64 1
        $debug = Config::getParam('debug');
65 1
        if ($debug || !file_exists($base . $hash . ".js")) {
66 1
            $data = '';
67 1
            if (0 < count($files)) {
68 1
                $minifier = new JS();
69 1
                foreach ($files as $file) {
70 1
                    $pathParts = explode("/", $file);
71 1
                    if (file_exists($file)) {
72 1
                        if ($debug) {
73 1
                            $data = $this->putDebugJs($pathParts, $base, $file, $hash, $compiledFiles);
74
                        } elseif (!file_exists($base . $hash . ".js")) {
75 1
                            $minifier->add($file);
76
                            //$data = $this->putProductionJs($base, $file, $data);
77
                        }
78
                    }
79
                }
80 1
                if($debug) {
81 1
                    AssetsHelper::storeContents($base . $hash . ".js", $data);
82
                } else {
83
                    $this->dumpJs($hash, $base, $minifier);
84
                }
85 1
                unset($minifier);
86
            }
87
        }
88 1
        return $this;
89
    }
90
91
    /**
92
     * @param $hash
93
     * @param string $base
94
     * @param JS $minifier
95
     * @throws \PSFS\base\exception\GeneratorException
96
     */
97
    protected function dumpJs($hash, string $base, JS $minifier)
98
    {
99
        ini_set('max_execution_time', -1);
100
        ini_set('memory_limit', -1);
101
        GeneratorHelper::createDir($base);
102
        if (Config::getParam('assets.obfuscate', false)) {
103
            $minifier->gzip($base . $hash . ".js");
104
        } else {
105
            $minifier->minify($base . $hash . ".js");
106
        }
107
    }
108
}
109