1
|
|
|
<?php |
2
|
|
|
namespace samsonphp\resource; |
3
|
|
|
|
4
|
|
|
use samson\core\ExternalModule; |
5
|
|
|
use samsonphp\event\Event; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Resource router for serving static resource from unreachable web-root paths. |
9
|
|
|
* |
10
|
|
|
* TODO: Validate old files that do not exists anymore to remove them |
11
|
|
|
* |
12
|
|
|
* @author Vitaly Iegorov <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Router extends ExternalModule |
15
|
|
|
{ |
16
|
|
|
/** @deprecated Use E_MODULES */ |
17
|
|
|
const EVENT_START_GENERATE_RESOURCES = 'resourcer.modulelist'; |
18
|
|
|
/** Event for modifying modules */ |
19
|
|
|
const E_MODULES = 'resourcer.modulelist'; |
20
|
|
|
/** Event for resources preloading */ |
21
|
|
|
const E_RESOURCE_PRELOAD = ResourceManager::E_ANALYZE; |
22
|
|
|
/** Event for resources compiling */ |
23
|
|
|
const E_RESOURCE_COMPILE = ResourceManager::E_COMPILE; |
24
|
|
|
/** Event when recourse management is finished */ |
25
|
|
|
const E_FINISHED = 'resourcer.finished'; |
26
|
|
|
|
27
|
|
|
/** @deprecated Identifier */ |
28
|
|
|
protected $id = STATIC_RESOURCE_HANDLER; |
29
|
|
|
|
30
|
|
|
/** @var array Template markers for inserting assets */ |
31
|
|
|
protected $templateMarkers = ['css' => '</head>', 'js' => '</body>']; |
32
|
|
|
|
33
|
|
|
/** @var array Collection of static resources */ |
34
|
|
|
protected $resources = []; |
35
|
|
|
|
36
|
|
|
/** @var array Collection of static resource URLs */ |
37
|
|
|
protected $resourceUrls = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Module initialization stage. |
41
|
|
|
* |
42
|
|
|
* @see ModuleConnector::init() |
43
|
|
|
* |
44
|
|
|
* @param array $params Initialization parameters |
45
|
|
|
* |
46
|
|
|
* @return bool True if resource successfully initialized |
47
|
|
|
*/ |
48
|
1 |
|
public function init(array $params = array()) |
49
|
|
|
{ |
50
|
|
|
// Subscribe for CSS handling |
51
|
1 |
|
Event::subscribe(self::E_RESOURCE_COMPILE, [new CSS(), 'compile']); |
52
|
|
|
|
53
|
|
|
// Subscribe to core template rendering event |
54
|
1 |
|
Event::subscribe('core.rendered', [$this, 'renderTemplate']); |
55
|
|
|
|
56
|
1 |
|
$resourceManager = new ResourceManager(new FileManager()); |
57
|
1 |
|
$resourceManager::$cacheRoot = $this->cache_path; |
58
|
1 |
|
$resourceManager::$webRoot = getcwd(); |
59
|
1 |
|
$resourceManager::$projectRoot = dirname($resourceManager::$webRoot) . '/'; |
60
|
|
|
|
61
|
|
|
// Get loaded modules |
62
|
1 |
|
$moduleList = $this->system->module_stack; |
|
|
|
|
63
|
|
|
|
64
|
|
|
// Event for modification of resource list |
65
|
1 |
|
Event::fire(self::E_MODULES, [&$moduleList]); |
66
|
|
|
|
67
|
1 |
|
$appResourcePaths = $this->getAssets($moduleList); |
68
|
|
|
|
69
|
|
|
// Get assets |
70
|
1 |
|
$this->resources = $resourceManager->manage($appResourcePaths); |
71
|
|
|
|
72
|
|
|
// Fire completion event |
73
|
1 |
|
Event::fire(self::E_FINISHED, [&$this->resources]); |
74
|
|
|
|
75
|
|
|
// Get asset URLs |
76
|
1 |
|
$this->resourceUrls = array_map([$this, 'getAssetCachedUrl'], $this->resources); |
77
|
|
|
|
78
|
|
|
// Continue parent initialization |
79
|
1 |
|
return parent::init($params); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get asset files from modules. |
84
|
|
|
* |
85
|
|
|
* @param array $moduleList Collection of modules |
86
|
|
|
* |
87
|
|
|
* @return array Resources paths |
88
|
|
|
*/ |
89
|
1 |
|
private function getAssets($moduleList) |
90
|
|
|
{ |
91
|
1 |
|
$projectRoot = dirname(getcwd()) . '/'; |
92
|
|
|
|
93
|
|
|
// Add resource paths |
94
|
1 |
|
$paths = []; |
95
|
1 |
|
foreach ($moduleList as $module) { |
96
|
|
|
/** |
97
|
|
|
* We need to exclude project root because vendor folder will be scanned |
98
|
|
|
* and all assets from there would be added. |
99
|
|
|
*/ |
100
|
1 |
|
if ($module->path() !== $projectRoot) { |
101
|
1 |
|
$paths[] = $module->path(); |
102
|
1 |
|
} |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
// Add web-root as last path |
106
|
1 |
|
$paths[] = getcwd(); |
107
|
|
|
|
108
|
1 |
|
return $paths; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Template rendering handler by injecting static assets url |
113
|
|
|
* in appropriate. |
114
|
|
|
* |
115
|
|
|
* @param $view |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
1 |
|
public function renderTemplate(&$view) |
120
|
|
|
{ |
121
|
1 |
|
foreach ($this->resourceUrls as $type => $urls) { |
122
|
1 |
|
if (array_key_exists($type, $this->templateMarkers)) { |
123
|
|
|
// Replace template marker by type with collection of links to resources of this type |
124
|
1 |
|
$view = str_ireplace( |
125
|
1 |
|
$this->templateMarkers[$type], |
126
|
1 |
|
implode("\n", array_map(function ($value) use ($type) { |
127
|
1 |
|
if ($type === 'css') { |
128
|
1 |
|
return '<link type="text/css" rel="stylesheet" property="stylesheet" href="' . $value . '">'; |
129
|
1 |
|
} elseif ($type === 'js') { |
130
|
1 |
|
return '<script type="text/javascript" src="' . $value . '"></script>'; |
131
|
|
|
} |
132
|
1 |
|
}, $urls)) . "\n" . $this->templateMarkers[$type], |
133
|
|
|
$view |
134
|
1 |
|
); |
135
|
1 |
|
} |
136
|
1 |
|
} |
137
|
|
|
|
138
|
1 |
|
return $view; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get cached asset URL. |
143
|
|
|
* |
144
|
|
|
* @param string $cachedAsset Full path to cached asset |
145
|
|
|
* |
146
|
|
|
* @return mixed Cached asset URL |
147
|
|
|
*/ |
148
|
1 |
|
private function getAssetCachedUrl($cachedAsset) |
149
|
|
|
{ |
150
|
1 |
|
return str_replace(ResourceManager::$webRoot, '', $cachedAsset); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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: