forked from midgardproject/midgardmvc_core
/
midgardmvc.php
209 lines (182 loc) · 6.47 KB
/
midgardmvc.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* @package midgardmvc_core
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
/**
* Midgard MVC filesystem component provider
*
* @package midgardmvc_core
*/
class midgardmvc_core_providers_component_midgardmvc implements midgardmvc_core_providers_component
{
public $components = array();
private $components_enabled = array();
private $injectors = array
(
'process' => array(),
'template' => array(),
);
private $manifests = array();
public function __construct()
{
$this->components_enabled = midgardmvc_core::get_instance()->configuration->components;
$this->load_all_manifests();
}
public function get($component)
{
if (!is_string($component))
{
throw new InvalidArgumentException('Invalid component name given, expected string but got ' . gettype($component));
}
if (isset($this->components[$component]))
{
// Component is installed and already loaded
return $this->components[$component];
}
if (!isset($this->components_enabled[$component]))
{
throw new OutOfRangeException("Component {$component} is not enabled in application configuration");
}
if (!isset($this->manifests[$component]))
{
$manifest_path = $this->get_manifest_path($component);
$this->manifests[$component] = $this->load_manifest_file($component, $manifest_path);
}
$this->components[$component] = new midgardmvc_core_providers_component_component_midgardmvc($component, $this->manifests[$component]);
midgardmvc_core::get_instance()->i18n->set_translation_domain($component);
return $this->components[$component];
}
/**
* Load a Zeta Component library
*/
public function load_library($library)
{
}
public function is_installed($component)
{
if (isset($this->components_enabled[$component]))
{
// Component is enabled
return true;
}
return false;
}
public function get_components()
{
$components = array();
foreach ($this->components_enabled as $component => $data)
{
$components[] = $this->get($component);
}
return $components;
}
public function get_routes(midgardmvc_core_request $request)
{
$routes = array();
$components = array_reverse($request->get_component_chain());
foreach ($components as $component)
{
$component_routes = $component->get_routes($request);
foreach ($component_routes as $route_id => $route)
{
$routes[$route_id] = $route;
}
}
return $routes;
}
public function inject(midgardmvc_core_request $request, $injector_type)
{
$inject_method = "inject_{$injector_type}";
foreach ($this->injectors[$injector_type] as $key => $injector_class)
{
$injector = new $injector_class();
$injector->$inject_method($request);
}
}
private function get_manifest_path($component)
{
return midgardmvc_core::get_component_path($component) . '/manifest.yml';
}
/**
* Load a component manifest file
*
* @param string $manifest_file Path of the manifest file
*/
private function load_manifest_file($component, $manifest_file)
{
if (!file_exists($manifest_file))
{
throw new OutOfRangeException("Component manifest {$manifest_file} is not installed");
}
$manifest_yaml = file_get_contents($manifest_file);
$manifest = midgardmvc_core::read_yaml($manifest_yaml);
// Normalize manifest
if (!isset($manifest['version']))
{
$manifest['version'] = '0.0.1devel';
}
if (!isset($manifest['extends']))
{
$manifest['extends'] = null;
}
elseif (!isset($this->components_enabled[$manifest['extends']]))
{
// Ensure the parent component is always enabled
$this->components_enabled[$manifest['extends']] = array();
}
if ( !isset($manifest['requires'])
|| !is_array($manifest['requires']))
{
$manifest['requires'] = array();
}
// Ensure the required components are always enabled
foreach ($manifest['requires'] as $component_required => $component_info)
{
$this->components_enabled[$component_required] = array();
}
if (isset($manifest['process_injector']))
{
// This component has an injector for the process() phase
$this->injectors['process'][$component] = $manifest['process_injector'];
}
if (isset($manifest['template_injector']))
{
// This component has an injector for the template() phase
$this->injectors['template'][$component] = $manifest['template_injector'];
}
if (!isset($manifest['observations']))
{
$manifest['observations'] = array();
}
foreach ($manifest['observations'] as $index => $observation)
{
if (!is_array($observation['type']))
{
$observation['type'] = array($observation['type']);
}
if (!is_array($observation['event']))
{
$observation['event'] = array($observation['event']);
}
$manifest['observations'][$index] = $observation;
}
return $manifest;
}
private function load_all_manifests()
{
// Load manifests enabled in site configuration and cache them
$components = midgardmvc_core::get_instance()->configuration->components;
foreach ($components as $component => $setup_info)
{
$component_path = midgardmvc_core::get_component_path($component);
if (!file_exists("{$component_path}/manifest.yml"))
{
throw new Exception("Component {$component} specified in application configuration is not installed");
}
$this->manifests[$component] = $this->load_manifest_file($component, "{$component_path}/manifest.yml");
}
}
}