1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Assets; |
12
|
|
|
|
13
|
|
|
use Phalcon\Assets\Collection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Manager |
17
|
|
|
* {@inheritDoc} |
18
|
|
|
* |
19
|
|
|
* @author Julien Turbide <[email protected]> |
20
|
|
|
* @copyright Zemit Team <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* @version 1.0 |
24
|
|
|
* |
25
|
|
|
* @package Zemit\Assets |
26
|
|
|
*/ |
27
|
|
|
class Manager extends \Phalcon\Assets\Manager |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Version of your app (ex. 1.0.0) |
31
|
|
|
* @var string Set the version to be added in the asset path |
32
|
|
|
*/ |
33
|
|
|
protected string $version; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
* @var bool true to automatically add the file time to the asset path |
38
|
|
|
*/ |
39
|
|
|
protected bool $fileTime; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Minify Javascript |
43
|
|
|
* @var bool true automatically minify javascript files |
44
|
|
|
*/ |
45
|
|
|
protected bool $minifyJS; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Minify CSS |
49
|
|
|
* @var bool true automatically minify stylesheet files |
50
|
|
|
*/ |
51
|
|
|
protected bool $minifyCSS; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Force the version manually |
55
|
|
|
* Version will be added to the assets path |
56
|
|
|
* |
57
|
|
|
* @param string $version |
58
|
|
|
*/ |
59
|
|
|
public function setVersion($version) |
60
|
|
|
{ |
61
|
|
|
$this->version = $version; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the version if forced |
66
|
|
|
* @return string Version |
67
|
|
|
*/ |
68
|
|
|
public function getVersion() |
69
|
|
|
{ |
70
|
|
|
return $this->version; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set File Time |
75
|
|
|
* @param $fileTime True to enable |
76
|
|
|
*/ |
77
|
|
|
public function setFileTime(bool $fileTime) : void |
78
|
|
|
{ |
79
|
|
|
$this->fileTime = $fileTime ? true : false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get File Time |
84
|
|
|
* @return bool True if enabled |
85
|
|
|
*/ |
86
|
|
|
public function getFileTime() |
87
|
|
|
{ |
88
|
|
|
return $this->fileTime ? true : false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set minify JS |
93
|
|
|
* @todo to be removed |
94
|
|
|
* @deprecated You should use webpack or something else |
95
|
|
|
* @param $minifyJS True to enable |
96
|
|
|
*/ |
97
|
|
|
public function setMinifyJS(bool $minifyJS) : void |
98
|
|
|
{ |
99
|
|
|
$this->minifyJS = $minifyJS ? true : false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
* @todo to be removed |
105
|
|
|
* @deprecated You should use webpack or something else |
106
|
|
|
*/ |
107
|
|
|
public function getMinifyJS() : bool |
108
|
|
|
{ |
109
|
|
|
return $this->minifyJS ? true : false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @todo to be removed |
114
|
|
|
* @deprecated You should use webpack or something else |
115
|
|
|
* @param $minifyCSS |
116
|
|
|
*/ |
117
|
|
|
public function setMinifyCSS(bool $minifyCSS) : void |
118
|
|
|
{ |
119
|
|
|
$this->minifyCSS = $minifyCSS ? true : false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @todo to be removed |
124
|
|
|
* @deprecated You should use webpack or something else |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function getMinifyCSS() : bool |
128
|
|
|
{ |
129
|
|
|
return $this->minifyCSS ? true : false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string|null $collectionName |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function outputJs(?string $collectionName = 'js'): string |
138
|
|
|
{ |
139
|
|
|
$this->setCollectionVersion($collectionName); |
140
|
|
|
|
141
|
|
|
return parent::outputJs($collectionName); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param null $collectionName |
|
|
|
|
146
|
|
|
* |
147
|
|
|
* @return string|null |
148
|
|
|
*/ |
149
|
|
|
public function outputCss(?string $collectionName = 'css'): string |
150
|
|
|
{ |
151
|
|
|
$this->setCollectionVersion($collectionName); |
152
|
|
|
|
153
|
|
|
return parent::outputCss($collectionName); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Add version to the collection |
158
|
|
|
* |
159
|
|
|
* @param string|null $collectionName |
160
|
|
|
* |
161
|
|
|
* @return Collection |
162
|
|
|
*/ |
163
|
|
|
public function setCollectionVersion(?string $collectionName = null): Collection |
164
|
|
|
{ |
165
|
|
|
$collection = $this->exists($collectionName) ? $this->get($collectionName) : false; |
|
|
|
|
166
|
|
|
if ($collection) { |
167
|
|
|
$collection = $this->get($collectionName); |
168
|
|
|
if ($collection) { |
|
|
|
|
169
|
|
|
$version = $this->getVersion(); |
170
|
|
|
if (empty($version)) { |
171
|
|
|
$collection->setVersion($version); |
172
|
|
|
} |
173
|
|
|
else { |
174
|
|
|
$collection->setAutoVersion(true); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $collection; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Add version to a path |
184
|
|
|
* |
185
|
|
|
* @todo to be removed or check if phalcon team implemented it according to our needs |
186
|
|
|
* @deprecated Now natively supported by phalcon asset collection itself |
187
|
|
|
* |
188
|
|
|
* @param $path |
189
|
|
|
* @param $version |
190
|
|
|
* @param false $addFileMTimeToPath |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
private static function _addVersionToPath($path, $version, $addFileMTimeToPath = false) |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
if ($addFileMTimeToPath) { |
|
|
|
|
197
|
|
|
$path = self::_addFileMtimeToPath($path); |
198
|
|
|
} |
199
|
|
|
if (!empty($version)) { |
200
|
|
|
$path = explode('.', $path); |
201
|
|
|
$ext = array_pop($path); |
202
|
|
|
$path = implode('.', $path) . '.' . $version . '.' . $ext; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $path; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Add File Mime Time to the path |
210
|
|
|
* |
211
|
|
|
* @deprecated |
212
|
|
|
* |
213
|
|
|
* @param $filepath |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
private static function _addFileMtimeToPath(string $filepath) |
218
|
|
|
{ |
219
|
|
|
$path = $filepath; |
220
|
|
|
if (file_exists($filepath)) { |
221
|
|
|
$path = explode('.', $filepath); |
222
|
|
|
$ext = array_pop($path); |
223
|
|
|
$path = implode('.', $path) . '.' . date('Ymdhis', filemtime($filepath)) . '.' . $ext; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $path; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|