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\JS; |
19
|
|
|
use O2System\Framework\Http\Presenter\Assets\Collections; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Body |
23
|
|
|
* |
24
|
|
|
* @package O2System\Framework\Http\Presenter\Assets\Positions |
25
|
|
|
*/ |
26
|
|
|
class Body extends Abstracts\AbstractPosition |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Body::$javascript |
30
|
|
|
* |
31
|
|
|
* @var \O2System\Framework\Http\Presenter\Assets\Collections\Javascript |
32
|
|
|
*/ |
33
|
|
|
protected $javascript; |
34
|
|
|
|
35
|
|
|
// ------------------------------------------------------------------------ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Body::__construct |
39
|
|
|
*/ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->javascript = new Collections\Javascript(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// ------------------------------------------------------------------------ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Body::__toString |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function __toString() |
53
|
|
|
{ |
54
|
|
|
$output = []; |
55
|
|
|
$unbundledFilename = ['app', 'app.min', 'theme', 'theme.min']; |
56
|
|
|
|
57
|
|
|
// Render js |
58
|
|
|
if ($this->javascript->count()) { |
59
|
|
|
foreach($this->javascript as $js) { |
60
|
|
|
if(in_array(pathinfo($js, PATHINFO_FILENAME), $unbundledFilename)) { |
61
|
|
|
$jsVersion = $this->getVersion($js); |
62
|
|
|
$output[] = '<script type="text/javascript" src="' . $this->getUrl($js) . '?v=' . $jsVersion . '"></script>'; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$bundleJsCollection = $this->javascript->getArrayCopy(); |
67
|
|
|
$bundleJsVersion = $this->getVersion(serialize($bundleJsCollection)); |
68
|
|
|
$bundleJsFilename = 'body-' . controller()->getClassInfo()->getParameter(); |
69
|
|
|
$bundleJsFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . $bundleJsFilename . '.js'; |
70
|
|
|
$bundleJsMinifyFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . $bundleJsFilename . '.min.js'; |
71
|
|
|
|
72
|
|
|
if (is_file($bundleJsFilePath . '.map')) { |
73
|
|
|
$bundleJsMap = json_decode(file_get_contents($bundleJsFilePath . '.map'), true); |
74
|
|
|
// if the file version is changed delete it first |
75
|
|
|
if ( ! hash_equals($bundleJsVersion, $bundleJsMap[ 'version' ])) { |
76
|
|
|
unlink($bundleJsFilePath); |
77
|
|
|
unlink($bundleJsFilePath . '.map'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ( ! is_file($bundleJsFilePath)) { |
82
|
|
|
$bundleJsMap = [ |
83
|
|
|
'version' => $bundleJsVersion, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
// Create js file |
87
|
|
|
if ($bundleFileStream = @fopen($bundleJsFilePath, 'ab')) { |
88
|
|
|
flock($bundleFileStream, LOCK_EX); |
89
|
|
|
|
90
|
|
|
foreach ($this->css as $css) { |
|
|
|
|
91
|
|
|
if( ! in_array(pathinfo($css, PATHINFO_FILENAME), $unbundledFilename)) { |
92
|
|
|
$bundleJsMap[ 'sources' ][] = $css; |
93
|
|
|
fwrite($bundleFileStream, file_get_contents($css)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
flock($bundleFileStream, LOCK_UN); |
98
|
|
|
fclose($bundleFileStream); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Create js map |
102
|
|
|
if ($bundleFileStream = @fopen($bundleJsFilePath . '.map', 'ab')) { |
103
|
|
|
flock($bundleFileStream, LOCK_EX); |
104
|
|
|
|
105
|
|
|
fwrite($bundleFileStream, json_encode($bundleJsMap)); |
106
|
|
|
|
107
|
|
|
flock($bundleFileStream, LOCK_UN); |
108
|
|
|
fclose($bundleFileStream); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Create js file minify version |
112
|
|
|
$minifyJsHandler = new JS($bundleJsFilePath); |
113
|
|
|
$minifyJsHandler->minify($bundleJsMinifyFilePath); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
117
|
|
|
$output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsMinifyFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
118
|
|
|
} else { |
119
|
|
|
$output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return implode(PHP_EOL, $output); |
124
|
|
|
} |
125
|
|
|
} |