1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base\extension\traits; |
3
|
|
|
|
4
|
|
|
use MatthiasMullie\Minify\CSS; |
5
|
|
|
use PSFS\base\exception\ConfigException; |
6
|
|
|
use PSFS\base\Logger; |
7
|
|
|
use PSFS\base\types\helpers\AssetsHelper; |
8
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait CssTrait |
12
|
|
|
* @package PSFS\base\extension\traits |
13
|
|
|
*/ |
14
|
|
|
trait CssTrait { |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $path; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $basePath |
22
|
|
|
* @param string $hash |
23
|
|
|
* @param bool $debug |
24
|
|
|
* @return $this |
25
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
26
|
|
|
*/ |
27
|
1 |
|
protected function compileCss($basePath, $hash, $debug = false) |
28
|
|
|
{ |
29
|
1 |
|
$base = $basePath . "css" . DIRECTORY_SEPARATOR; |
30
|
1 |
|
if ($debug || !file_exists($base . $hash . ".css")) { |
31
|
1 |
|
$data = ''; |
32
|
1 |
|
if (0 < count($this->files)) { |
33
|
1 |
|
$minifier = new CSS(); |
34
|
1 |
|
foreach ($this->files as $file) { |
35
|
1 |
|
$data = $this->processCssLine($file, $base, $data); |
36
|
|
|
} |
37
|
|
|
} |
38
|
1 |
|
if($debug) { |
39
|
1 |
|
$this->storeContents($base . $hash . ".css", $data); |
|
|
|
|
40
|
|
|
} else { |
41
|
|
|
$minifier = new CSS(); |
42
|
|
|
$minifier->add($data); |
43
|
|
|
ini_set('max_execution_time', -1); |
44
|
|
|
ini_set('memory_limit', -1); |
45
|
|
|
GeneratorHelper::createDir($base); |
46
|
|
|
$minifier->minify($base . $hash . ".css"); |
47
|
|
|
} |
48
|
1 |
|
unset($minifier); |
49
|
|
|
} |
50
|
1 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Método que procesa cada línea de la hoja de estilos para copiar los recursos asociados |
55
|
|
|
* @param string $file |
56
|
|
|
* @param string $base |
57
|
|
|
* @param string $data |
58
|
|
|
* @return false|string |
59
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
60
|
|
|
*/ |
61
|
1 |
|
protected function processCssLine($file, $base, $data) |
62
|
|
|
{ |
63
|
1 |
|
if (file_exists($file)) { |
64
|
1 |
|
$pathParts = explode("/", $file); |
65
|
1 |
|
$filePath = $this->hash . "_" . $pathParts[count($pathParts) - 1]; |
66
|
1 |
|
if (!file_exists($base . $filePath) || filemtime($base . $filePath) < filemtime($file) || $this->debug) { |
67
|
|
|
//Si tenemos modificaciones tenemos que compilar de nuevo todos los ficheros modificados |
68
|
1 |
|
if (file_exists($base . $this->hash . ".css") && @unlink($base . $this->hash . ".css") === false) { |
69
|
|
|
throw new ConfigException("Can't unlink file " . $base . $this->hash . ".css"); |
70
|
|
|
} |
71
|
1 |
|
$this->loopCssLines($file); |
72
|
|
|
} |
73
|
1 |
|
if ($this->debug) { |
74
|
1 |
|
$data = file_get_contents($file); |
75
|
1 |
|
$this->storeContents($base . $filePath, $data); |
76
|
|
|
} else { |
77
|
|
|
$data .= file_get_contents($file); |
78
|
|
|
} |
79
|
1 |
|
$this->compiled_files[] = "/css/" . $filePath; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return $data; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $file |
87
|
|
|
*/ |
88
|
1 |
|
protected function loopCssLines($file) |
89
|
|
|
{ |
90
|
1 |
|
$handle = @fopen($file, 'r'); |
91
|
1 |
|
if ($handle) { |
|
|
|
|
92
|
1 |
|
while (!feof($handle)) { |
93
|
1 |
|
$line = fgets($handle); |
94
|
1 |
|
$urls = array(); |
95
|
1 |
|
if (preg_match_all('#url\((.*?)\)#', $line, $urls, PREG_SET_ORDER)) { |
96
|
1 |
|
foreach ($urls as $source) { |
97
|
1 |
|
$this->extractCssResources($source, $file); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
1 |
|
fclose($handle); |
102
|
|
|
} |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $source |
107
|
|
|
* @param string $file |
108
|
|
|
*/ |
109
|
1 |
|
protected function extractCssResources($source, $file) |
110
|
|
|
{ |
111
|
1 |
|
$sourceFile = AssetsHelper::extractSourceFilename($source); |
112
|
1 |
|
$orig = realpath(dirname($file) . DIRECTORY_SEPARATOR . $sourceFile); |
113
|
1 |
|
$origPart = preg_split('/(\/|\\\)public(\/|\\\)/i', $orig); |
114
|
|
|
try { |
115
|
1 |
|
if (count($source) > 1 && array_key_exists(1, $origPart)) { |
|
|
|
|
116
|
|
|
$dest = $this->path . $origPart[1]; |
117
|
|
|
GeneratorHelper::createDir(dirname($dest)); |
118
|
|
|
if (!file_exists($dest) || filemtime($orig) > filemtime($dest)) { |
119
|
|
|
if (@copy($orig, $dest) === FALSE) { |
120
|
|
|
throw new \RuntimeException('Can\' copy ' . $dest . ''); |
121
|
|
|
} |
122
|
1 |
|
Logger::log("$orig copiado a $dest", LOG_INFO); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} catch (\Exception $e) { |
126
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
127
|
|
|
} |
128
|
1 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array $compiledFiles |
132
|
|
|
* @param string $baseUrl |
133
|
|
|
* @param string $hash |
134
|
|
|
* @param bool $debug |
135
|
|
|
*/ |
136
|
1 |
|
protected function printCss(array $compiledFiles, $baseUrl, $hash, $debug = false) |
137
|
|
|
{ |
138
|
1 |
|
if ($debug && 0 < count($compiledFiles)) { |
139
|
1 |
|
foreach ($compiledFiles as $file) { |
140
|
1 |
|
echo "\t\t<link href='{$file}' rel='stylesheet' media='screen, print'>"; |
141
|
|
|
} |
142
|
|
|
} else { |
143
|
|
|
echo "\t\t<link href='" . $baseUrl . "/css/" . $hash . ".css' rel='stylesheet'>"; |
144
|
|
|
} |
145
|
1 |
|
} |
146
|
|
|
} |
147
|
|
|
|