-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactory.php
195 lines (173 loc) · 4.84 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Mods\View;
use Layout\Core\PageFactory;
use Mods\Theme\Factory as ThemeFactory;
class Factory
{
/**
* @var \Layout\Core\PageFactory $pageFactory
*/
protected $pageFactory;
/**
* @var \Mods\Theme\Factory $themeFactory
*/
protected $themeFactory;
/**
*
* @param \Layout\Core\PageFactory $pageFactory
* @param \Mods\Theme\Factory $themeFactory
*/
public function __construct(PageFactory $pageFactory, ThemeFactory $themeFactory)
{
$this->pageFactory = $pageFactory;
$this->themeFactory = $themeFactory;
}
/**
* Render the current page and return view
*
* @return \Illuminate\View\View
*/
public function render()
{
$html = $this->pageFactory->render();
$html['head'] = $this->updateAssetUrls($html['head']);
return view('root', $html);
}
/**
* Add a piece of data to the view.
*
* @param string|array $key
* @param mixed $value
* @return $this
*/
public function with($key, $value = null)
{
if (is_array($key)) {
view()->share($key);
} else {
view()->share($key, $value);
}
return $this;
}
/**
* Get the page factory
*
* @return \Layout\Core\Factory
*/
public function getPageFactory()
{
return $this->pageFactory;
}
/**
* Fix the base url for the assets and do the last mintue updates
*
* @return array
*/
protected function updateAssetUrls($head)
{
$area = app()->area();
$theme = $this->themeFactory->getActiveTheme($area);
$manifest = $this->fetchManifest($area, $theme);
$routeHandler = $this->pageFactory->routeHandler();
if (isset($manifest['webpack']) && $manifest['webpack']) {
$handleAsset = $manifest['compiledAsset'][$routeHandler];
$js = $css = '';
foreach ($handleAsset['js'] as $value) {
$js .= $this->getJsTag($this->getAssetBaseUrl($area."/".$theme).$value);
}
$head['js'] = $js;
foreach ($handleAsset['css'] as $value) {
$css .= $this->getCsssTag($this->getAssetBaseUrl($area."/".$theme).$value);
}
$head['css'] = $css;
} elseif (isset($manifest['bundled']) && $manifest['bundled']) {
$name = md5($area.$theme.$routeHandler);
$head['js'] = '<script src="'.$this->getJsBaseUrl($area, $theme).$name.'.js"></script>';
$head['css'] = '<link href="'.$this->getCssBaseUrl($area, $theme).$name.'.css" media="all" rel="stylesheet" />';
} else {
$minified = (isset($manifest['minified']) && $manifest['minified']);
$head['js'] = str_replace(
['%baseurl', '.js'], [$this->getJsBaseUrl($area, $theme), ($minified)?'.min.js':'.js'],
$head['js']
);
$head['css'] = str_replace(
['%baseurl', '.css'], [$this->getCssBaseUrl($area, $theme), ($minified)?'.min.css':'.css'],
$head['css']
);
}
return $head;
}
/**
* Get the js tag
*
* @param string $name
* @return string
*/
protected function getJsTag($name)
{
return '<script src="'.$name.'"></script>'."\n";
}
/**
* Get the css tag
*
* @param string $name
* @return string
*/
protected function getCsssTag($name)
{
return '<link href="'.$name.'" media="all" rel="stylesheet" />'."\n";
}
/**
* Get the base url for asset
*
* @param string $path
* @return string
*/
public function getAssetBaseUrl($path)
{
return asset("assets/$path").'/';
}
/**
* Get the base url for script
*
* @param string $area
* @param string $theme
* @return string
*/
public function getJsBaseUrl($area, $theme)
{
return asset("assets/{$area}/{$theme}/js").'/';
}
/**
* Get the base url for style
*
* @param string $area
* @param string $theme
* @return string
*/
public function getCssBaseUrl($area, $theme)
{
return asset("assets/{$area}/{$theme}/css").'/';
}
/**
* Get the base url for style
*
* @param string $area
* @param string $theme
* @return array
*/
protected function fetchManifest($area, $theme)
{
$manifestPath = $this->getPath(
[app('path.resources'), 'assets', $area, $theme, 'manifest.json']
);
if (!file_exists($manifestPath)) {
return [];
}
return json_decode(file_get_contents($manifestPath), true);
}
protected function getPath($paths)
{
return implode(DIRECTORY_SEPARATOR, $paths);
}
}