|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System PHP Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Framework\Http\Presenter\Assets\Positions\Abstracts; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Kernel\Http\Message\Uri; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class AbstractPosition |
|
22
|
|
|
* |
|
23
|
|
|
* @package O2System\Framework\Http\Presenter\Assets\Positions\Abstracts |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class AbstractPosition |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* AbstractPosition::__get |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $property |
|
31
|
|
|
* |
|
32
|
|
|
* @return \O2System\Framework\Http\Presenter\Assets\Collections\Css|\O2System\Framework\Http\Presenter\Assets\Collections\Font|\O2System\Framework\Http\Presenter\Assets\Collections\Javascript|null |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __get($property) |
|
35
|
|
|
{ |
|
36
|
|
|
return isset($this->{$property}) ? $this->{$property} : null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// ------------------------------------------------------------------------ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* AbstractPosition::getUrl |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $realPath |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getUrl($realPath) |
|
49
|
|
|
{ |
|
50
|
|
|
if (strpos($realPath, 'http') !== false) { |
|
51
|
|
|
return $realPath; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return (new Uri()) |
|
55
|
|
|
->withQuery(null) |
|
56
|
|
|
->withSegments( |
|
57
|
|
|
new Uri\Segments( |
|
58
|
|
|
str_replace( |
|
59
|
|
|
[PATH_PUBLIC, DIRECTORY_SEPARATOR], |
|
60
|
|
|
['', '/'], |
|
61
|
|
|
$realPath |
|
62
|
|
|
) |
|
63
|
|
|
) |
|
64
|
|
|
) |
|
65
|
|
|
->__toString(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// ------------------------------------------------------------------------ |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* AbstractPosition::loadCollections |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $collections |
|
74
|
|
|
*/ |
|
75
|
|
|
public function loadCollections(array $collections) |
|
76
|
|
|
{ |
|
77
|
|
|
foreach ($collections as $subDir => $files) { |
|
78
|
|
|
if (is_array($files) and count($files)) { |
|
79
|
|
|
// normalize the subDirectory with a trailing separator |
|
80
|
|
|
$subDir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $subDir); |
|
81
|
|
|
$subDir = rtrim($subDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
82
|
|
|
|
|
83
|
|
|
foreach ($files as $file) { |
|
84
|
|
|
if (strpos($file, 'http') !== false) { |
|
85
|
|
|
$this->loadUrl($file, $subDir); |
|
86
|
|
|
} else { |
|
87
|
|
|
$this->loadFile($file, $subDir); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} elseif (is_string($files)) { |
|
91
|
|
|
$this->loadFile($files); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// ------------------------------------------------------------------------ |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* AbstractPosition::loadUrl |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $url |
|
102
|
|
|
* @param string|null $subDir |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
|
public function loadUrl($url, $subDir = null) |
|
107
|
|
|
{ |
|
108
|
|
|
$property = is_null($subDir) ? 'css' : null; |
|
109
|
|
|
|
|
110
|
|
|
if(is_null($property)) { |
|
111
|
|
|
switch ($subDir) { |
|
112
|
|
|
default: |
|
113
|
|
|
case 'css/': |
|
114
|
|
|
$property = 'css'; |
|
115
|
|
|
break; |
|
116
|
|
|
case 'font/': |
|
117
|
|
|
case 'fonts/': |
|
118
|
|
|
$property = 'font'; |
|
119
|
|
|
break; |
|
120
|
|
|
case 'js/': |
|
121
|
|
|
$property = 'javascript'; |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (property_exists($this, $property)) { |
|
127
|
|
|
if ( ! call_user_func_array([$this->{$property}, 'has'], [$url])) { |
|
128
|
|
|
$this->{$property}->append($url); |
|
129
|
|
|
|
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// ------------------------------------------------------------------------ |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* AbstractPosition::loadFile |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $filePath |
|
143
|
|
|
* @param string $subDir |
|
144
|
|
|
* |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
public function loadFile($filePath, $subDir = null) |
|
148
|
|
|
{ |
|
149
|
|
|
$directories = loader()->getPublicDirs(true); |
|
150
|
|
|
$filePath = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $filePath); |
|
151
|
|
|
|
|
152
|
|
|
// set filepaths |
|
153
|
|
|
foreach ($directories as $directory) { |
|
154
|
|
|
$extension = pathinfo($directory . $filePath, PATHINFO_EXTENSION); |
|
155
|
|
|
|
|
156
|
|
|
if(empty($extension) and empty($subDir)) { |
|
157
|
|
|
$extensions = ['.css', '.js']; |
|
158
|
|
|
} elseif(empty($extension) and isset($subDir)) { |
|
159
|
|
|
switch ($subDir) { |
|
160
|
|
|
default: |
|
161
|
|
|
case 'css/': |
|
162
|
|
|
$property = 'css'; |
|
163
|
|
|
$extensions = ['.css']; |
|
164
|
|
|
break; |
|
165
|
|
|
case 'font/': |
|
166
|
|
|
case 'fonts/': |
|
167
|
|
|
$property = 'font'; |
|
168
|
|
|
$extensions = ['.css']; |
|
169
|
|
|
break; |
|
170
|
|
|
case 'js/': |
|
171
|
|
|
case 'javascript/': |
|
172
|
|
|
case 'javascripts/': |
|
173
|
|
|
$property = 'javascript'; |
|
174
|
|
|
$extensions = ['.js']; |
|
175
|
|
|
break; |
|
176
|
|
|
} |
|
177
|
|
|
} else { |
|
178
|
|
|
// remove filename extension |
|
179
|
|
|
$filePath = str_replace('.' . $extension, '', $filePath); |
|
180
|
|
|
switch ($extension) { |
|
181
|
|
|
default: |
|
182
|
|
|
case 'css': |
|
183
|
|
|
$property = 'css'; |
|
184
|
|
|
$subDir = 'css' . DIRECTORY_SEPARATOR; |
|
185
|
|
|
$extensions = ['.css']; |
|
186
|
|
|
break; |
|
187
|
|
|
case 'font': |
|
188
|
|
|
$property = 'font'; |
|
189
|
|
|
$subDir = 'fonts' . DIRECTORY_SEPARATOR; |
|
190
|
|
|
$extensions = ['.css']; |
|
191
|
|
|
break; |
|
192
|
|
|
case 'js': |
|
193
|
|
|
case 'javascript': |
|
194
|
|
|
$property = 'javascript'; |
|
195
|
|
|
$subDir = 'js' . DIRECTORY_SEPARATOR; |
|
196
|
|
|
$extensions = ['.js']; |
|
197
|
|
|
break; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
foreach($extensions as $extension) { |
|
202
|
|
|
// without subdirectory |
|
203
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
|
204
|
|
|
$filePaths[] = $directory . $filePath . '.min' . $extension; // minify version support |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$filePaths[] = $directory . $filePath . $extension; |
|
208
|
|
|
|
|
209
|
|
|
// with subdirectory |
|
210
|
|
|
if (isset($subDir)) { |
|
211
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
|
212
|
|
|
$filePaths[] = $directory . $subDir . $filePath . '.min' . $extension; // minify version support |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$filePaths[] = $directory . $subDir . $filePath . $extension; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
foreach ($filePaths as $filePath) { |
|
|
|
|
|
|
221
|
|
|
if (is_file($filePath)) { |
|
222
|
|
|
if(empty($property)) { |
|
223
|
|
|
$extension = pathinfo($filePath, PATHINFO_EXTENSION); |
|
224
|
|
|
switch ($extension) { |
|
225
|
|
|
case 'font': |
|
226
|
|
|
$extension = 'font'; |
|
|
|
|
|
|
227
|
|
|
break; |
|
228
|
|
|
case 'js': |
|
229
|
|
|
$extension = 'javascript'; |
|
230
|
|
|
break; |
|
231
|
|
|
default: |
|
232
|
|
|
case 'css': |
|
233
|
|
|
$extension = 'css'; |
|
234
|
|
|
break; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
if (property_exists($this, $property)) { |
|
|
|
|
|
|
239
|
|
|
if ( ! call_user_func_array([$this->{$property}, 'has'], [$filePath])) { |
|
240
|
|
|
$this->{$property}->append($filePath); |
|
241
|
|
|
|
|
242
|
|
|
return true; |
|
243
|
|
|
break; |
|
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return false; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
// ------------------------------------------------------------------------ |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* AbstractPosition::getVersion |
|
256
|
|
|
* |
|
257
|
|
|
* @param string $code |
|
258
|
|
|
* |
|
259
|
|
|
* @return string |
|
260
|
|
|
*/ |
|
261
|
|
|
public function getVersion($codeSerialize) |
|
262
|
|
|
{ |
|
263
|
|
|
$codeMd5 = md5($codeSerialize); |
|
264
|
|
|
|
|
265
|
|
|
$strSplit = str_split($codeMd5, 4); |
|
266
|
|
|
foreach ($strSplit as $strPart) { |
|
267
|
|
|
$strInt[] = str_pad(hexdec($strPart), 5, '0', STR_PAD_LEFT); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
$codeVersion = round( implode('', $strInt), 10 ); |
|
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
return substr_replace($codeVersion, '.', 3, strlen($codeVersion) - 5); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
// ------------------------------------------------------------------------ |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* AbstractPosition::__toString |
|
279
|
|
|
* |
|
280
|
|
|
* @return string |
|
281
|
|
|
*/ |
|
282
|
|
|
abstract public function __toString(); |
|
283
|
|
|
} |