|
1
|
|
|
<?php |
|
2
|
|
|
namespace PSFS\base\extension\traits; |
|
3
|
|
|
|
|
4
|
|
|
use MatthiasMullie\Minify\JS; |
|
5
|
|
|
use PSFS\base\config\Config; |
|
6
|
|
|
use PSFS\base\exception\ConfigException; |
|
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
|
|
|
/** |
|
17
|
|
|
* @param array $compiledFiles |
|
18
|
|
|
* @param string $baseUrl |
|
19
|
|
|
* @param string $hash |
|
20
|
|
|
* @param bool $debug |
|
21
|
|
|
*/ |
|
22
|
1 |
|
protected function printJs(array $compiledFiles, $baseUrl, $hash, $debug = false) |
|
23
|
|
|
{ |
|
24
|
1 |
|
if ($debug && 0 < count($compiledFiles)) { |
|
25
|
1 |
|
foreach ($compiledFiles as $file) { |
|
26
|
1 |
|
echo "\t\t<script type='text/javascript' src='{$file}'></script>\n"; |
|
27
|
|
|
} |
|
28
|
|
|
} else { |
|
29
|
|
|
echo "\t\t<script type='text/javascript' src='" . $baseUrl . "/js/" . $hash . ".js'></script>\n"; |
|
30
|
|
|
} |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param $pathParts |
|
35
|
|
|
* @param string $base |
|
36
|
|
|
* @param $file |
|
37
|
|
|
* @return false|string |
|
38
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
39
|
|
|
*/ |
|
40
|
1 |
|
protected function putDebugJs($pathParts, $base, $file) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$filePath = $this->hash . "_" . $pathParts[count($pathParts) - 1]; |
|
43
|
1 |
|
$this->compiled_files[] = "/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 |
|
$this->storeContents($base . $filePath, $data); |
|
48
|
|
|
} |
|
49
|
1 |
|
return $data; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Método para guardar cualquier contenido y controlar que existe el directorio y se guarda correctamente |
|
54
|
|
|
* @param string $path |
|
55
|
|
|
* @param string $content |
|
56
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
57
|
|
|
*/ |
|
58
|
1 |
|
private function storeContents($path, $content = "") |
|
59
|
|
|
{ |
|
60
|
1 |
|
GeneratorHelper::createDir(dirname($path)); |
|
61
|
1 |
|
if ("" !== $content && false === file_put_contents($path, $content)) { |
|
62
|
|
|
throw new ConfigException(t('No se tienen permisos para escribir en ' . $path)); |
|
63
|
|
|
} |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Método que compila los ficheros javascript en función del modo de ejecución |
|
68
|
|
|
* @return $this |
|
69
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
70
|
|
|
*/ |
|
71
|
1 |
|
protected function compileJs(array $files, $basePath, $hash, $debug = false) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$base = $basePath . "js" . DIRECTORY_SEPARATOR; |
|
74
|
1 |
|
if ($debug || !file_exists($base . $hash . ".js")) { |
|
75
|
1 |
|
$data = ''; |
|
76
|
1 |
|
if (0 < count($files)) { |
|
77
|
1 |
|
$minifier = new JS(); |
|
78
|
1 |
|
foreach ($files as $file) { |
|
79
|
1 |
|
$pathParts = explode("/", $file); |
|
80
|
1 |
|
if (file_exists($file)) { |
|
81
|
1 |
|
if ($debug) { |
|
82
|
1 |
|
$data = $this->putDebugJs($pathParts, $base, $file); |
|
83
|
|
|
} elseif (!file_exists($base . $hash . ".js")) { |
|
84
|
1 |
|
$minifier->add($file); |
|
85
|
|
|
//$data = $this->putProductionJs($base, $file, $data); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
1 |
|
if($debug) { |
|
90
|
1 |
|
$this->storeContents($base . $hash . ".js", $data); |
|
91
|
|
|
} else { |
|
92
|
|
|
ini_set('max_execution_time', -1); |
|
93
|
|
|
ini_set('memory_limit', -1); |
|
94
|
|
|
GeneratorHelper::createDir($base); |
|
95
|
|
|
if(Config::getParam('assets.obfuscate', false)) { |
|
96
|
|
|
$minifier->gzip($base . $hash . ".js"); |
|
97
|
|
|
} else { |
|
98
|
|
|
$minifier->minify($base . $hash . ".js"); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
1 |
|
unset($minifier); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
1 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|