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 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
|
|
|
$bundleJsContents = []; |
60
|
|
|
|
61
|
|
|
foreach ($this->javascript as $js) { |
62
|
|
|
if (in_array(pathinfo($js, PATHINFO_FILENAME), $unbundledFilename)) { |
63
|
|
|
$jsVersion = $this->getVersion($js); |
64
|
|
|
$output[] = '<script type="text/javascript" src="' . $this->getUrl($js) . '?v=' . $jsVersion . '"></script>'; |
65
|
|
|
} else { |
66
|
|
|
$bundleJsMap[ 'sources' ][] = $js; |
67
|
|
|
$bundleJsContents[] = file_get_contents($js); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Bundled Js |
72
|
|
|
$bundleJsVersion = $this->getVersion(serialize($bundleJsContents)); |
73
|
|
|
|
74
|
|
|
if(presenter()->page->file instanceof \SplFileInfo) { |
|
|
|
|
75
|
|
|
if(presenter()->page->file->getFilename() === 'index') { |
76
|
|
|
$bundleJsFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName(); |
77
|
|
|
} else { |
78
|
|
|
$bundleJsFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName() . '-' . presenter()->page->file->getFilename(); |
79
|
|
|
} |
80
|
|
|
} elseif(services()->has('controller')) { |
81
|
|
|
$bundleJsFilename = 'body-' . controller()->getParameter(); |
82
|
|
|
|
83
|
|
|
if(controller()->getRequestMethod() !== 'index') { |
84
|
|
|
$bundleJsFilename.= '-' . controller()->getRequestMethod(); |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
$bundleJsFilename = 'body-' . md5($bundleJsVersion); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$bundlePublicDir = modules()->current()->getPublicDir() . 'assets' . DIRECTORY_SEPARATOR; |
|
|
|
|
91
|
|
|
$bundleJsFilePath = $bundlePublicDir . $bundleJsFilename . '.js'; |
92
|
|
|
$bundleJsMinifyFilePath = $bundlePublicDir . $bundleJsFilename . '.min.js'; |
93
|
|
|
|
94
|
|
|
if (is_file($bundleJsFilePath . '.map')) { |
95
|
|
|
$bundleJsMap = json_decode(file_get_contents($bundleJsFilePath . '.map'), true); |
96
|
|
|
// if the file version is changed delete it first |
97
|
|
|
if ( ! hash_equals($bundleJsVersion, $bundleJsMap[ 'version' ])) { |
98
|
|
|
unlink($bundleJsFilePath); |
99
|
|
|
unlink($bundleJsFilePath . '.map'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ( ! is_file($bundleJsFilePath)) { |
104
|
|
|
$bundleJsMap[ 'version' ] = $bundleJsVersion; |
105
|
|
|
|
106
|
|
|
// Create css file |
107
|
|
|
if (count($bundleJsContents)) { |
108
|
|
|
if ($bundleFileStream = @fopen($bundleJsFilePath, 'ab')) { |
109
|
|
|
flock($bundleFileStream, LOCK_EX); |
110
|
|
|
fwrite($bundleFileStream, implode(PHP_EOL, $bundleJsContents)); |
111
|
|
|
flock($bundleFileStream, LOCK_UN); |
112
|
|
|
fclose($bundleFileStream); |
113
|
|
|
|
114
|
|
|
// Create css map |
115
|
|
|
if ($bundleFileStream = @fopen($bundleJsFilePath . '.map', 'ab')) { |
116
|
|
|
flock($bundleFileStream, LOCK_EX); |
117
|
|
|
|
118
|
|
|
fwrite($bundleFileStream, json_encode($bundleJsMap)); |
119
|
|
|
|
120
|
|
|
flock($bundleFileStream, LOCK_UN); |
121
|
|
|
fclose($bundleFileStream); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Create javascript file minify version |
125
|
|
|
$minifyJsHandler = new JS($bundleJsFilePath); |
126
|
|
|
$minifyJsHandler->minify($bundleJsMinifyFilePath); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if(is_file($bundleJsFilePath)) { |
132
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
133
|
|
|
$output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsMinifyFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
134
|
|
|
} else { |
135
|
|
|
$output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return implode(PHP_EOL, $output); |
141
|
|
|
} |
142
|
|
|
} |