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; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use MatthiasMullie\Minify\CSS; |
19
|
|
|
use MatthiasMullie\Minify\JS; |
20
|
|
|
use O2System\Framework\Http\Presenter\Assets\Collections; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Head |
24
|
|
|
* |
25
|
|
|
* @package O2System\Framework\Http\Presenter\Assets\Positions |
26
|
|
|
*/ |
27
|
|
|
class Head extends Abstracts\AbstractPosition |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Head::$font |
31
|
|
|
* |
32
|
|
|
* @var \O2System\Framework\Http\Presenter\Assets\Collections\Font |
33
|
|
|
*/ |
34
|
|
|
protected $font; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Head::$css |
38
|
|
|
* |
39
|
|
|
* @var \O2System\Framework\Http\Presenter\Assets\Collections\Css |
40
|
|
|
*/ |
41
|
|
|
protected $css; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Head::$javascript |
45
|
|
|
* |
46
|
|
|
* @var \O2System\Framework\Http\Presenter\Assets\Collections\Javascript |
47
|
|
|
*/ |
48
|
|
|
protected $javascript; |
49
|
|
|
|
50
|
|
|
// ------------------------------------------------------------------------ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Head::__construct |
54
|
|
|
*/ |
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
$this->font = new Collections\Font(); |
58
|
|
|
$this->css = new Collections\Css(); |
59
|
|
|
$this->javascript = new Collections\Javascript(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// ------------------------------------------------------------------------ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Head::__toString |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function __toString() |
70
|
|
|
{ |
71
|
|
|
$output = []; |
72
|
|
|
|
73
|
|
|
// Render fonts |
74
|
|
|
if ($this->font->count()) { |
75
|
|
|
|
76
|
|
|
$bundleFontCollection = $this->font->getArrayCopy(); |
77
|
|
|
$bundleFontVersion = $this->getVersion(serialize($bundleFontCollection)); |
78
|
|
|
$bundleFontFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'fonts.css'; |
79
|
|
|
$bundleFontMinifyFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'fonts.min.css'; |
80
|
|
|
|
81
|
|
|
if (is_file($bundleFontFilePath . '.map')) { |
82
|
|
|
$bundleFontMap = json_decode(file_get_contents($bundleFontFilePath . '.map'), true); |
83
|
|
|
// if the file version is changed delete it first |
84
|
|
|
if ( ! hash_equals($bundleFontVersion, $bundleFontMap[ 'version' ])) { |
85
|
|
|
unlink($bundleFontFilePath); |
86
|
|
|
unlink($bundleFontFilePath . '.map'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ( ! is_file($bundleFontFilePath)) { |
91
|
|
|
$bundleFontMap = [ |
92
|
|
|
'version' => $bundleFontVersion, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
// Create css font file |
96
|
|
|
if ($bundleFileStream = @fopen($bundleFontFilePath, 'ab')) { |
97
|
|
|
flock($bundleFileStream, LOCK_EX); |
98
|
|
|
|
99
|
|
|
foreach ($this->font as $font) { |
100
|
|
|
$bundleFontMap[ 'sources' ][] = $font; |
101
|
|
|
fwrite($bundleFileStream, file_get_contents($font)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
flock($bundleFileStream, LOCK_UN); |
105
|
|
|
fclose($bundleFileStream); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Create css font map |
109
|
|
|
if ($bundleFileStream = @fopen($bundleFontFilePath . '.map', 'ab')) { |
110
|
|
|
flock($bundleFileStream, LOCK_EX); |
111
|
|
|
|
112
|
|
|
fwrite($bundleFileStream, json_encode($bundleFontMap)); |
113
|
|
|
|
114
|
|
|
flock($bundleFileStream, LOCK_UN); |
115
|
|
|
fclose($bundleFileStream); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Create css font file minify version |
119
|
|
|
$minifyFontHandler = new CSS($bundleFontFilePath); |
120
|
|
|
$minifyFontHandler->minify($bundleFontMinifyFilePath); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
124
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->getUrl($bundleFontMinifyFilePath) . '?v=' . $bundleFontVersion . '">'; |
125
|
|
|
} else { |
126
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->getUrl($bundleFontFilePath) . '?v=' . $bundleFontVersion . '">'; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$unbundledFilename = ['app', 'app.min', 'theme', 'theme.min']; |
131
|
|
|
|
132
|
|
|
// Render css |
133
|
|
|
if ($this->css->count()) { |
134
|
|
|
foreach($this->css as $css) { |
135
|
|
|
if(in_array(pathinfo($css, PATHINFO_FILENAME), $unbundledFilename)) { |
136
|
|
|
$cssVersion = $this->getVersion($css); |
137
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->getUrl($css) . '?v=' . $cssVersion . '">'; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$bundleCssCollection = $this->css->getArrayCopy(); |
142
|
|
|
$bundleCssVersion = $this->getVersion(serialize($bundleCssCollection)); |
143
|
|
|
$bundleCssFilename = 'head-' . controller()->getClassInfo()->getParameter(); |
144
|
|
|
$bundleCssFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . $bundleCssFilename . '.css'; |
145
|
|
|
$bundleCssMinifyFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . $bundleCssFilename . '.min.css'; |
146
|
|
|
|
147
|
|
|
if (is_file($bundleCssFilePath . '.map')) { |
148
|
|
|
$bundleCssMap = json_decode(file_get_contents($bundleCssFilePath . '.map'), true); |
149
|
|
|
// if the file version is changed delete it first |
150
|
|
|
if ( ! hash_equals($bundleCssVersion, $bundleCssMap[ 'version' ])) { |
151
|
|
|
unlink($bundleCssFilePath); |
152
|
|
|
unlink($bundleCssFilePath . '.map'); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ( ! is_file($bundleCssFilePath)) { |
157
|
|
|
$bundleCssMap = [ |
158
|
|
|
'version' => $bundleCssVersion, |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
// Create css file |
162
|
|
|
if ($bundleFileStream = @fopen($bundleCssFilePath, 'ab')) { |
163
|
|
|
flock($bundleFileStream, LOCK_EX); |
164
|
|
|
|
165
|
|
|
foreach ($this->css as $css) { |
166
|
|
|
if( ! in_array(pathinfo($css, PATHINFO_FILENAME), $unbundledFilename)) { |
167
|
|
|
$bundleCssMap[ 'sources' ][] = $css; |
168
|
|
|
fwrite($bundleFileStream, file_get_contents($css)); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
flock($bundleFileStream, LOCK_UN); |
173
|
|
|
fclose($bundleFileStream); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Create css map |
177
|
|
|
if ($bundleFileStream = @fopen($bundleCssFilePath . '.map', 'ab')) { |
178
|
|
|
flock($bundleFileStream, LOCK_EX); |
179
|
|
|
|
180
|
|
|
fwrite($bundleFileStream, json_encode($bundleCssMap)); |
181
|
|
|
|
182
|
|
|
flock($bundleFileStream, LOCK_UN); |
183
|
|
|
fclose($bundleFileStream); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// Create css file minify version |
187
|
|
|
$minifyCssHandler = new CSS($bundleCssFilePath); |
188
|
|
|
$minifyCssHandler->minify($bundleCssMinifyFilePath); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
192
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->getUrl($bundleCssMinifyFilePath) . '?v=' . $bundleCssVersion . '">'; |
193
|
|
|
} else { |
194
|
|
|
$output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->getUrl($bundleCssFilePath) . '?v=' . $bundleCssVersion . '">'; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Render js |
199
|
|
|
if ($this->javascript->count()) { |
200
|
|
|
foreach($this->javascript as $js) { |
201
|
|
|
if(in_array(pathinfo($css, PATHINFO_FILENAME), $unbundledFilename)) { |
|
|
|
|
202
|
|
|
$jsVersion = $this->getVersion($js); |
203
|
|
|
$output[] = '<script type="text/javascript" src="' . $this->getUrl($js) . '?v=' . $jsVersion . '"></script>'; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$bundleJsCollection = $this->css->getArrayCopy(); |
208
|
|
|
$bundleJsVersion = $this->getVersion(serialize($bundleJsCollection)); |
209
|
|
|
$bundleJsFilename = 'head-' . controller()->getClassInfo()->getParameter(); |
210
|
|
|
$bundleJsFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . $bundleJsFilename . '.js'; |
211
|
|
|
$bundleJsMinifyFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . $bundleJsFilename . '.min.js'; |
212
|
|
|
|
213
|
|
|
if (is_file($bundleJsFilePath . '.map')) { |
214
|
|
|
$bundleJsMap = json_decode(file_get_contents($bundleJsFilePath . '.map'), true); |
215
|
|
|
// if the file version is changed delete it first |
216
|
|
|
if ( ! hash_equals($bundleJsVersion, $bundleJsMap[ 'version' ])) { |
217
|
|
|
unlink($bundleJsFilePath); |
218
|
|
|
unlink($bundleJsFilePath . '.map'); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ( ! is_file($bundleJsFilePath)) { |
223
|
|
|
$bundleJsMap = [ |
224
|
|
|
'version' => $bundleJsVersion, |
225
|
|
|
]; |
226
|
|
|
|
227
|
|
|
// Create js file |
228
|
|
|
if ($bundleFileStream = @fopen($bundleJsFilePath, 'ab')) { |
229
|
|
|
flock($bundleFileStream, LOCK_EX); |
230
|
|
|
|
231
|
|
|
foreach ($this->css as $css) { |
232
|
|
|
if( ! in_array(pathinfo($css, PATHINFO_FILENAME), $unbundledFilename)) { |
233
|
|
|
$bundleJsMap[ 'sources' ][] = $css; |
234
|
|
|
fwrite($bundleFileStream, file_get_contents($css)); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
flock($bundleFileStream, LOCK_UN); |
239
|
|
|
fclose($bundleFileStream); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
// Create js map |
243
|
|
|
if ($bundleFileStream = @fopen($bundleJsFilePath . '.map', 'ab')) { |
244
|
|
|
flock($bundleFileStream, LOCK_EX); |
245
|
|
|
|
246
|
|
|
fwrite($bundleFileStream, json_encode($bundleJsMap)); |
247
|
|
|
|
248
|
|
|
flock($bundleFileStream, LOCK_UN); |
249
|
|
|
fclose($bundleFileStream); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
// Create js file minify version |
253
|
|
|
$minifyJsHandler = new JS($bundleJsFilePath); |
254
|
|
|
$minifyJsHandler->minify($bundleJsMinifyFilePath); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
258
|
|
|
$output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsMinifyFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
259
|
|
|
} else { |
260
|
|
|
$output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return implode(PHP_EOL, $output); |
265
|
|
|
} |
266
|
|
|
} |