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 Body |
22
|
|
|
* |
23
|
|
|
* @package O2System\Framework\Http\Presenter\Assets\Positions |
24
|
|
|
*/ |
25
|
|
|
class Body extends Abstracts\AbstractPosition |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Body::$javascripts |
29
|
|
|
* |
30
|
|
|
* @var \O2System\Framework\Http\Presenter\Assets\Collections\Javascripts |
31
|
|
|
*/ |
32
|
|
|
protected $javascripts; |
33
|
|
|
|
34
|
|
|
// ------------------------------------------------------------------------ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Body::__construct |
38
|
|
|
*/ |
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
$this->javascripts = new Collections\Javascripts(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// ------------------------------------------------------------------------ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Body::loadFile |
48
|
|
|
* |
49
|
|
|
* @param string $filename |
50
|
|
|
* @param string|null $subDir |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function loadFile($filename, $subDir = null) |
55
|
|
|
{ |
56
|
|
|
if (is_file($filename)) { |
57
|
|
|
$this->javascripts->append($filename); |
58
|
|
|
} else { |
59
|
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION); |
60
|
|
|
|
61
|
|
|
if (empty($extension)) { |
62
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
63
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.min.js'))) { |
|
|
|
|
64
|
|
|
$this->javascripts->append($filePath); |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
|
|
if (false !== ($filePath = $this->getFilePath($filename . '.js', $subDir))) { |
|
|
|
|
68
|
|
|
$this->javascripts->append($filePath); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} elseif (false !== ($filePath = $this->getFilePath($filename, $subDir))) { |
|
|
|
|
72
|
|
|
$this->javascripts->append($filePath); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// ------------------------------------------------------------------------ |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Body::__toString |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function __toString() |
85
|
|
|
{ |
86
|
|
|
$output = []; |
87
|
|
|
$unbundledFilenames = ['app', 'app.min', 'theme', 'theme.min']; |
88
|
|
|
|
89
|
|
|
if (presenter()->page->file instanceof \SplFileInfo) { |
|
|
|
|
90
|
|
|
if (presenter()->page->file->getFilename() === 'index') { |
91
|
|
|
$bundleFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName(); |
92
|
|
|
} else { |
93
|
|
|
$bundleFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName() . '-' . presenter()->page->file->getFilename(); |
94
|
|
|
} |
95
|
|
|
} elseif (services()->has('controller')) { |
96
|
|
|
$bundleFilename = 'body-' . controller()->getParameter(); |
97
|
|
|
|
98
|
|
|
if (controller()->getRequestMethod() !== 'index') { |
99
|
|
|
$bundleFilename .= '-' . controller()->getRequestMethod(); |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
|
|
$bundleFilename = 'body-' . uniqid(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$bundleFilename = 'assets' . DIRECTORY_SEPARATOR . $bundleFilename; |
106
|
|
|
|
107
|
|
|
// Render js |
108
|
|
|
if ($this->javascripts->count()) { |
109
|
|
|
$bundleJavascriptSources = []; |
110
|
|
|
|
111
|
|
|
foreach ($this->javascripts as $javascript) { |
112
|
|
|
if (in_array(pathinfo($javascript, PATHINFO_FILENAME), $unbundledFilenames)) { |
113
|
|
|
$fileVersion = $this->getVersion(filemtime($javascript)); |
114
|
|
|
$output[] = '<script type="text/javascript" id="js-'.pathinfo($javascript, PATHINFO_FILENAME).'" src="' . $this->getUrl($javascript) . '?v=' . $fileVersion . '"></script>'; |
115
|
|
|
} elseif (in_array(pathinfo($javascript, PATHINFO_FILENAME), ['module', 'module.min'])) { |
116
|
|
|
$modulePublicFile = $this->publishFile($javascript); |
117
|
|
|
|
118
|
|
|
if (is_file($modulePublicFile[ 'filePath' ])) { |
119
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
120
|
|
|
$output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'minify' ][ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>'; |
121
|
|
|
} else { |
122
|
|
|
$output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>'; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} else { |
126
|
|
|
$bundleJavascriptSources[] = $javascript; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (count($bundleJavascriptSources)) { |
131
|
|
|
$bundleJavascriptPublicFile = $this->bundleFile($bundleFilename . '.js', $bundleJavascriptSources); |
132
|
|
|
|
133
|
|
|
if (is_file($bundleJavascriptPublicFile[ 'filePath' ])) { |
134
|
|
|
if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
135
|
|
|
$output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'minify' ][ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>'; |
136
|
|
|
} else { |
137
|
|
|
$output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>'; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return implode(PHP_EOL, $output); |
144
|
|
|
} |
145
|
|
|
} |