1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System 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; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Framework\Http\Presenter\Assets\Collections; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Head |
22
|
|
|
* |
23
|
|
|
* @package O2System\Framework\Http\Presenter\Assets\Positions |
24
|
|
|
*/ |
25
|
|
|
class Head extends Abstracts\AbstractPosition |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Head::$fonts |
29
|
|
|
* |
30
|
|
|
* @var \O2System\Framework\Http\Presenter\Assets\Collections\Fonts |
31
|
|
|
*/ |
32
|
|
|
protected $fonts; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Head::$styles |
36
|
|
|
* |
37
|
|
|
* @var \O2System\Framework\Http\Presenter\Assets\Collections\Styles |
38
|
|
|
*/ |
39
|
|
|
protected $styles; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Head::$javascripts |
43
|
|
|
* |
44
|
|
|
* @var \O2System\Framework\Http\Presenter\Assets\Collections\Javascripts |
45
|
|
|
*/ |
46
|
|
|
protected $javascripts; |
47
|
|
|
|
48
|
|
|
// ------------------------------------------------------------------------ |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Head::__construct |
52
|
|
|
*/ |
53
|
|
|
public function __construct() |
54
|
|
|
{ |
55
|
|
|
$this->fonts = new Collections\Fonts(); |
56
|
|
|
$this->styles = new Collections\Styles(); |
57
|
|
|
$this->javascripts = new Collections\Javascripts(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// ------------------------------------------------------------------------ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Head::loadFile |
64
|
|
|
* |
65
|
|
|
* @param string $filename |
66
|
|
|
* @param string|null $subDir |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function loadFile($filename, $subDir = null) |
71
|
|
|
{ |
72
|
|
|
if (is_file($filename)) { |
73
|
|
|
if (strpos($filename, 'font') !== false) { |
74
|
|
|
$this->fonts->append($filename); |
75
|
|
|
} else { |
76
|
|
|
$type = pathinfo($filename, PATHINFO_EXTENSION); |
77
|
|
|
|
78
|
|
|
switch ($type) { |
79
|
|
|
case 'css': |
80
|
|
|
$this->styles->append($filename); |
81
|
|
|
break; |
82
|
|
|
|
83
|
|
|
case 'js': |
84
|
|
|
$this->javascripts->append($filename); |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} elseif (isset($subDir)) { |
89
|
|
|
$type = pathinfo($filename, PATHINFO_EXTENSION); |
90
|
|
|
|
91
|
|
|
if (empty($type)) { |
92
|
|
|
$type = substr($subDir, 0, -1); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
switch ($type) { |
96
|
|
|
case 'css': |
97
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
98
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.min.css', $subDir))) { |
|
|
|
|
99
|
|
|
$this->styles->append($filePath); |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.css', $subDir))) { |
|
|
|
|
103
|
|
|
$this->styles->append($filePath); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
break; |
107
|
|
|
|
108
|
|
|
case 'fonts': |
109
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
110
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.min.css', $subDir))) { |
|
|
|
|
111
|
|
|
$this->fonts->append($filePath); |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.css', $subDir))) { |
|
|
|
|
115
|
|
|
$this->fonts->append($filePath); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
break; |
119
|
|
|
|
120
|
|
|
case 'js': |
121
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
122
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.min.js', $subDir))) { |
|
|
|
|
123
|
|
|
$this->styles->append($filePath); |
124
|
|
|
} |
125
|
|
|
} else { |
126
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.js', $subDir))) { |
|
|
|
|
127
|
|
|
$this->styles->append($filePath); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
break; |
131
|
|
|
} |
132
|
|
|
} else { |
133
|
|
|
$type = pathinfo($filename, PATHINFO_EXTENSION); |
134
|
|
|
|
135
|
|
|
if (empty($type)) { |
136
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
137
|
|
|
// Search Style or Font |
138
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.min.css'))) { |
|
|
|
|
139
|
|
|
$this->styles->append($filePath); |
140
|
|
|
} elseif (false !== ($filePath = $this->getFilePath($filename . '.min.css'))) { |
141
|
|
|
$this->fonts->append($filePath); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Search Javascript |
145
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.min.js'))) { |
|
|
|
|
146
|
|
|
$this->javascripts->append($filePath); |
147
|
|
|
} |
148
|
|
|
} else { |
149
|
|
|
// Search Style or Font |
150
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.css'))) { |
|
|
|
|
151
|
|
|
$this->styles->append($filePath); |
152
|
|
|
} elseif (false !== ($filePath = $this->getFilePath($filename . '.css'))) { |
153
|
|
|
$this->fonts->append($filePath); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Search Javascript |
157
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.js'))) { |
|
|
|
|
158
|
|
|
$this->javascripts->append($filePath); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} else { |
162
|
|
|
switch ($type) { |
163
|
|
|
case 'css': |
164
|
|
|
if (false !== ($filePath = $this->getFilePath($filename))) { |
|
|
|
|
165
|
|
|
$this->styles->append($filePath); |
166
|
|
|
} |
167
|
|
|
break; |
168
|
|
|
|
169
|
|
|
case 'js': |
170
|
|
|
if (false !== ($filePath = $this->getFilePath($filename))) { |
|
|
|
|
171
|
|
|
$this->javascripts->append($filePath); |
172
|
|
|
} |
173
|
|
|
break; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// ------------------------------------------------------------------------ |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Head::__toString |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function __toString() |
187
|
|
|
{ |
188
|
|
|
$output = []; |
189
|
|
|
|
190
|
|
|
// Render fonts |
191
|
|
|
if ($this->fonts->count()) { |
192
|
|
|
|
193
|
|
|
$bundleFontsFile = $this->bundleFile('assets' . DIRECTORY_SEPARATOR . 'fonts.css', |
194
|
|
|
implode(PHP_EOL, $this->fonts)); |
|
|
|
|
195
|
|
|
|
196
|
|
|
if (is_file($bundleFontsFile[ 'filePath' ])) { |
197
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
198
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleFontsFile[ 'url' ] . '?v=' . $bundleFontsFile[ 'version' ] . '">'; |
199
|
|
|
} else { |
200
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleFontsFile[ 'url' ] . '?v=' . $bundleFontsFile[ 'version' ] . '">'; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$unbundledFilenames = ['app', 'app.min', 'theme', 'theme.min']; |
206
|
|
|
|
207
|
|
|
if (presenter()->page->file instanceof \SplFileInfo) { |
|
|
|
|
208
|
|
|
if (presenter()->page->file->getFilename() === 'index') { |
209
|
|
|
$bundleFilename = 'head-' . presenter()->page->file->getDirectoryInfo()->getDirName(); |
210
|
|
|
} else { |
211
|
|
|
$bundleFilename = 'head-' . presenter()->page->file->getDirectoryInfo()->getDirName() . '-' . presenter()->page->file->getFilename(); |
212
|
|
|
} |
213
|
|
|
} elseif (services()->has('controller')) { |
214
|
|
|
$bundleFilename = 'head-' . controller()->getParameter(); |
215
|
|
|
|
216
|
|
|
if (controller()->getRequestMethod() !== 'index') { |
217
|
|
|
$bundleFilename .= '-' . controller()->getRequestMethod(); |
218
|
|
|
} |
219
|
|
|
} else { |
220
|
|
|
$bundleFilename = 'head-' . uniqid(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$bundleFilename = 'assets' . DIRECTORY_SEPARATOR . $bundleFilename; |
224
|
|
|
|
225
|
|
|
// Render style |
226
|
|
|
if ($this->styles->count()) { |
227
|
|
|
$bundleStyleSources = []; |
228
|
|
|
|
229
|
|
|
foreach ($this->styles as $style) { |
230
|
|
|
if (in_array(pathinfo($style, PATHINFO_FILENAME), $unbundledFilenames)) { |
231
|
|
|
$fileVersion = $this->getVersion(filemtime($style)); |
232
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->getUrl($style) . '?v=' . $fileVersion . '">'; |
233
|
|
|
} elseif (in_array(pathinfo($style, PATHINFO_FILENAME), ['module', 'module.min'])) { |
234
|
|
|
$modulePublicFile = $this->publishFile($style); |
235
|
|
|
|
236
|
|
|
if (is_file($modulePublicFile[ 'filePath' ])) { |
237
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
238
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $modulePublicFile[ 'minify' ][ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '">'; |
239
|
|
|
} else { |
240
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $modulePublicFile[ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '">'; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} else { |
244
|
|
|
$bundleStyleSources[] = $style; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
if (count($bundleStyleSources)) { |
249
|
|
|
$bundleStylePublicFile = $this->bundleFile($bundleFilename . '.css', $bundleStyleSources); |
250
|
|
|
|
251
|
|
|
if (is_file($bundleStylePublicFile[ 'filePath' ])) { |
252
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
253
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleStylePublicFile[ 'minify' ][ 'url' ] . '?v=' . $bundleStylePublicFile[ 'version' ] . '">'; |
254
|
|
|
} else { |
255
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleStylePublicFile[ 'url' ] . '?v=' . $bundleStylePublicFile[ 'version' ] . '">'; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// Render javascript |
262
|
|
|
if ($this->javascripts->count()) { |
263
|
|
|
$bundleJavascriptSources = []; |
264
|
|
|
|
265
|
|
|
foreach ($this->javascripts as $javascript) { |
266
|
|
|
if (in_array(pathinfo($style, PATHINFO_FILENAME), $unbundledFilenames)) { |
|
|
|
|
267
|
|
|
$fileVersion = $this->getVersion(filemtime($javascript)); |
268
|
|
|
$output[] = '<script type="text/javascript" id="js-'.pathinfo($javascript, PATHINFO_FILENAME).'" src="' . $this->getUrl($javascript) . '?v=' . $fileVersion . '"></script>'; |
269
|
|
|
} elseif (in_array(pathinfo($javascript, PATHINFO_FILENAME), ['module', 'module.min'])) { |
270
|
|
|
$modulePublicFile = $this->publishFile($javascript); |
271
|
|
|
|
272
|
|
|
if (is_file($modulePublicFile[ 'filePath' ])) { |
273
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
274
|
|
|
$output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'minify' ][ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>'; |
275
|
|
|
} else { |
276
|
|
|
$output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>'; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} else { |
280
|
|
|
$bundleJavascriptSources[] = $javascript; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if (count($bundleJavascriptSources)) { |
285
|
|
|
$bundleJavascriptPublicFile = $this->bundleFile($bundleFilename . '.js', $bundleJavascriptSources); |
286
|
|
|
|
287
|
|
|
if (is_file($bundleJavascriptPublicFile[ 'filePath' ])) { |
288
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
289
|
|
|
$output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'minify' ][ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>'; |
290
|
|
|
} else { |
291
|
|
|
$output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>'; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return implode(PHP_EOL, $output); |
298
|
|
|
} |
299
|
|
|
} |