1
|
|
|
<?php |
2
|
|
|
namespace samsonphp\resource; |
3
|
|
|
|
4
|
|
|
use Aws\CloudFront\Exception\Exception; |
5
|
|
|
use samson\core\ExternalModule; |
6
|
|
|
use samson\core\Module; |
7
|
|
|
use samsonframework\resource\ResourceMap; |
8
|
|
|
use samsonphp\event\Event; |
9
|
|
|
use samsonphp\resource\exception\ResourceNotFound; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Resource router for serving static resource from unreachable web-root paths. |
13
|
|
|
* |
14
|
|
|
* @author Vitaly Iegorov <[email protected]> |
15
|
|
|
* @author Nikita Kotenko <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Router extends ExternalModule |
18
|
|
|
{ |
19
|
|
|
/** @deprecated Use E_MODULES */ |
20
|
|
|
const EVENT_START_GENERATE_RESOURCES = 'resourcer.modulelist'; |
21
|
|
|
/** Event for modifying modules */ |
22
|
|
|
const E_MODULES = 'resourcer.modulelist'; |
23
|
|
|
/** Event for resources preloading */ |
24
|
|
|
const E_RESOURCE_PRELOAD = 'resourcer.preload'; |
25
|
|
|
/** Event for resources compiling */ |
26
|
|
|
const E_RESOURCE_COMPILE = 'resourcer.compile'; |
27
|
|
|
|
28
|
|
|
/** Collection of registered resource types */ |
29
|
|
|
protected $types = ['less', 'css', 'js', 'coffee', 'ts']; |
30
|
|
|
|
31
|
|
|
/** @var array Assets cache */ |
32
|
|
|
protected $cache = []; |
33
|
|
|
|
34
|
|
|
/** @var array Template markers for inserting assets */ |
35
|
|
|
protected $templateMarkers = [ |
36
|
|
|
'css' => '</head>', |
37
|
|
|
'js' => '</body>' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** @var array Collection of static resources */ |
41
|
|
|
protected $resources = []; |
42
|
|
|
/** @var array Collection of static resource URLs */ |
43
|
|
|
protected $resourceUrls = []; |
44
|
|
|
|
45
|
|
|
/** Identifier */ |
46
|
|
|
protected $id = STATIC_RESOURCE_HANDLER; |
47
|
|
|
|
48
|
|
|
/** @see ModuleConnector::init() */ |
49
|
|
|
public function init(array $params = array()) |
50
|
|
|
{ |
51
|
|
|
// Subscribe for CSS handling |
52
|
|
|
Event::subscribe(self::E_RESOURCE_COMPILE, [new CSS(), 'compile']); |
53
|
|
|
|
54
|
|
|
$moduleList = $this->system->module_stack; |
|
|
|
|
55
|
|
|
$paths = []; |
56
|
|
|
|
57
|
|
|
// Event for modification of module list |
58
|
|
|
Event::fire(self::E_MODULES, array(&$moduleList)); |
59
|
|
|
|
60
|
|
|
$projectRoot = dirname(getcwd()).'/'; |
61
|
|
|
|
62
|
|
|
// Add module paths |
63
|
|
|
foreach ($moduleList as $module) { |
64
|
|
|
if ($module->path() !== $projectRoot) { |
65
|
|
|
$paths[] = $module->path(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
$paths[] = getcwd(); |
69
|
|
|
|
70
|
|
|
$files = Resource::scan($paths, $this->types); |
71
|
|
|
|
72
|
|
|
$this->createAssets($files); |
73
|
|
|
|
74
|
|
|
// Subscribe to core template rendering event |
75
|
|
|
Event::subscribe('core.rendered', [$this, 'renderTemplate']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function getAssetPathData($resource, $extension = null) |
79
|
|
|
{ |
80
|
|
|
$extension = $extension === null ? pathinfo($resource, PATHINFO_EXTENSION) : $extension; |
81
|
|
|
switch ($extension) { |
82
|
|
|
case 'css': |
83
|
|
|
case 'less': |
84
|
|
|
case 'scss': |
85
|
|
|
case 'sass': $extension = 'css'; break; |
|
|
|
|
86
|
|
|
case 'ts': |
87
|
|
|
case 'cofee': $extension = 'js'; break; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$wwwRoot = getcwd(); |
91
|
|
|
$projectRoot = dirname($wwwRoot).'/'; |
92
|
|
|
$relativePath = str_replace($projectRoot, '', $resource); |
93
|
|
|
|
94
|
|
|
$fileName = pathinfo($resource, PATHINFO_FILENAME); |
95
|
|
|
|
96
|
|
|
return dirname($this->cache_path.$relativePath).'/'.$fileName.'.'.$extension; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create static assets. |
101
|
|
|
* |
102
|
|
|
* @param array $files Collection of paths for gathering resources |
103
|
|
|
*/ |
104
|
|
|
public function createAssets(array $files) |
105
|
|
|
{ |
106
|
|
|
$wwwRoot = getcwd(); |
107
|
|
|
|
108
|
|
|
$assets = []; |
109
|
|
|
|
110
|
|
|
// Scan folder and gather |
111
|
|
|
foreach ($files as $file) { |
112
|
|
|
// Generate cached resource path with possible new extension after compiling |
113
|
|
|
$assets[$file] = $this->getAssetPathData($file); |
114
|
|
|
$extension = pathinfo($assets[$file], PATHINFO_EXTENSION); |
115
|
|
|
|
116
|
|
|
// If cached assets was modified or new |
117
|
|
|
if (!file_exists($assets[$file]) || filemtime($file) !== filemtime($assets[$file])) { |
118
|
|
|
// Read asset content |
119
|
|
|
$this->cache[$file] = file_get_contents($file); |
120
|
|
|
|
121
|
|
|
// Fire event for analyzing resource |
122
|
|
|
Event::fire(self::E_RESOURCE_PRELOAD, [$file, pathinfo($file, PATHINFO_EXTENSION), &$this->cache[$file]]); |
123
|
|
|
} else { |
124
|
|
|
// Add this resource to resource collection grouped by resource type |
125
|
|
|
$this->resources[$extension][] = $assets[$file]; |
126
|
|
|
$this->resourceUrls[$extension][] = str_replace($wwwRoot, '', $assets[$file]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$wwwRoot = getcwd(); |
131
|
|
|
foreach ($this->cache as $file => $content) { |
132
|
|
|
$extension = pathinfo($file, PATHINFO_EXTENSION); |
133
|
|
|
|
134
|
|
|
$compiled = $content; |
135
|
|
|
Event::fire(self::E_RESOURCE_COMPILE, [$file, &$extension, &$compiled]); |
136
|
|
|
|
137
|
|
|
// Create folder structure and file only if it is not empty |
138
|
|
|
$resource = $this->getAssetPathData($file, $extension); |
139
|
|
|
|
140
|
|
|
// Create cache path |
141
|
|
|
$path = dirname($resource); |
142
|
|
|
if (!file_exists($path)) { |
143
|
|
|
mkdir($path, 0777, true); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
file_put_contents($resource, $compiled); |
147
|
|
|
|
148
|
|
|
// Sync cached file with source file |
149
|
|
|
touch($resource, filemtime($file)); |
150
|
|
|
|
151
|
|
|
// Add this resource to resource collection grouped by resource type |
152
|
|
|
$this->resources[$extension][] = $resource; |
153
|
|
|
$this->resourceUrls[$extension][] = str_replace($wwwRoot, '', $resource); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Template rendering handler by injecting static assets url |
159
|
|
|
* in appropriate. |
160
|
|
|
* |
161
|
|
|
* @param $view |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
|
|
public function renderTemplate(&$view) |
166
|
|
|
{ |
167
|
|
|
foreach ($this->resourceUrls as $type => $urls) { |
168
|
|
|
// Replace template marker by type with collection of links to resources of this type |
169
|
|
|
$view = str_ireplace( |
170
|
|
|
$this->templateMarkers[$type], |
171
|
|
|
implode("\n", array_map(function($value) use ($type) { |
|
|
|
|
172
|
|
|
if ($type === 'css') { |
173
|
|
|
return '<link type="text/css" rel="stylesheet" property="stylesheet" href="' . $value . '">'; |
174
|
|
|
} elseif ($type === 'js') { |
175
|
|
|
return '<script async type="text/javascript" src="' . $value . '"></script>'; |
176
|
|
|
} |
177
|
|
|
}, $urls)) . "\n" . $this->templateMarkers[$type], |
178
|
|
|
$view |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $view; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: