forked from midgardproject/midgardmvc_core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.php
448 lines (394 loc) · 12.6 KB
/
interface.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?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 interface class
*
* @package midgardmvc_core
*/
class midgardmvc_core
{
/**
* @var midgardmvc_core_services_configuration_yaml
*/
public $configuration;
/**
* @var midgardmvc_core_helpers_context
*/
public $context;
private static $instance = null;
public function __construct()
{
}
/**
* Load all basic services needed for Midgard MVC usage. This includes configuration, authorization and the component loader.
*/
public function load_base_services(array $local_configuration = null)
{
// Load the context helper and initialize first context
$this->context = new midgardmvc_core_helpers_context();
$this->configuration = new midgardmvc_core_services_configuration_chain($local_configuration);
}
/**
* Helper for service initialization. Usually called via getters
*
* @param string $service Name of service to load
*/
private function load_service($service)
{
$interface_file = MIDGARDMVC_ROOT . "/midgardmvc_core/services/{$service}.php";
if (!file_exists($interface_file))
{
throw new InvalidArgumentException("Service {$service} not installed");
}
if (!$this->configuration->exists("services_{$service}"))
{
throw new Exception("No implementation defined for service {$service}");
}
$service_implementation = $this->configuration->get("services_{$service}");
if (strpos($service_implementation, '_') === false)
{
// Built-in service implementation called using the shorthand notation
$service_implementation = "midgardmvc_core_services_{$service}_{$service_implementation}";
}
$this->$service = new $service_implementation();
}
/**
* Helper for service initialization. Usually called via getters
*
* @param string $service Name of service to load
*/
private function load_provider($provider)
{
$interface_file = MIDGARDMVC_ROOT . "/midgardmvc_core/providers/{$provider}.php";
if (!file_exists($interface_file))
{
throw new InvalidArgumentException("Provider {$provider} not installed");
}
if (!$this->configuration->exists("providers_{$provider}"))
{
throw new Exception("No implementation defined for provider {$provider}");
}
$provider_implementation = $this->configuration->get("providers_{$provider}");
if (strpos($provider_implementation, '_') === false)
{
// Built-in provider implementation called using the shorthand notation
$provider_implementation = "midgardmvc_core_providers_{$provider}_{$provider_implementation}";
}
$this->$provider = new $provider_implementation();
}
/**
* Logging interface
*
* @param string $prefix Prefix to file the log under
* @param string $message Message to be logged
* @param string $loglevel Logging level, may be one of debug, info, message and warning
*/
public function log($prefix, $message, $loglevel = 'debug')
{
if (!extension_loaded('midgard2'))
{
$this->log_with_helper($prefix, $message, $loglevel);
return;
}
midgard_error::$loglevel("{$prefix}: {$message}");
}
private function log_with_helper($prefix, $message, $loglevel)
{
// Temporary non-Midgard logger until midgard_error is backported to Ragnaroek
static $logger = null;
if (!$logger)
{
try
{
$logger = new midgardmvc_core_helpers_log();
}
catch (Exception $e)
{
// Unable to instantiate logger
return;
}
}
static $log_levels = array
(
'debug' => 4,
'info' => 3,
'message' => 2,
'warn' => 1,
);
if ($log_levels[$loglevel] > $log_levels[$this->configuration->get('log_level')])
{
// Skip logging, too low level
return;
}
$logger->log("{$prefix}: {$message}");
}
/**
* Magic getter for service and provider loading
*/
public function __get($key)
{
try
{
// First try loading as a service
$this->load_service($key);
}
catch (InvalidArgumentException $e)
{
// Load a provider instead
$this->load_provider($key);
}
return $this->$key;
}
/**
* Automatically load missing class files
*
* @param string $class_name Name of a missing PHP class
*/
public static function autoload($class_name)
{
$class_parts = explode('_', $class_name);
$component = '';
foreach ($class_parts as $i => $part)
{
if ($component == '')
{
$component = $part;
}
else
{
$component .= "_{$part}";
}
unset($class_parts[$i]);
if (is_dir(MIDGARDMVC_ROOT . "/{$component}"))
{
break;
}
}
$path_under_component = implode('/', $class_parts);
if ($path_under_component == '')
{
$path_under_component = 'interface';
}
$path = MIDGARDMVC_ROOT . "/{$component}/{$path_under_component}.php";
if (!file_exists($path))
{
return;
}
require($path);
}
/**
* Process the current request, loading the node's component and dispatching the request to it
*/
public function process()
{
try
{
$request = $this->_process();
}
catch (Exception $e)
{
// ->serve() wouldn't be called — do cleanup here
$this->_after_process();
$this->cleanup_after_request();
// rethrowing exception, if there is one
throw $e;
}
$this->_after_process();
return $request;
}
private function _process()
{
// Let dispatcher populate request with the node and other information used
$request = $this->dispatcher->get_request();
$request->add_component_to_chain($this->component->get('midgardmvc_core'));
// TODO: We give it to context to emulate legacy functionality
$this->context->create($request);
// Load the head helper
$this->head = new midgardmvc_core_helpers_head();
// Check authentication
$this->authentication->check_session();
// Disable cache for now
$request->set_data_item('cache_enabled', false);
$this->log('Midgard MVC', 'Serving ' . $request->get_method() . ' ' . $request->get_path() . ' at ' . gmdate('r'), 'info');
// Let injectors do their work
$this->component->inject($request, 'process');
try
{
$this->dispatcher->dispatch($request);
}
catch (midgardmvc_exception_unauthorized $exception)
{
// Pass the exception to authentication handler
$this->authentication->handle_exception($exception);
}
$this->dispatcher->header('Content-Type: ' . $request->get_data_item('mimetype'));
return $request;
}
private function _after_process()
{
// add any cleanup after process() here
}
/**
* Serve a request either through templating or the WebDAV server
*/
public function serve(midgardmvc_core_request $request)
{
try
{
$this->_serve($request);
}
catch (Exception $e)
{
// this will be executed even if _serve() had exception
$this->_after_serve();
throw $e;
}
$this->_after_serve();
}
private function _serve(midgardmvc_core_request $request)
{
// Prepate the templates
$this->templating->template($request);
// Read contents from the output buffer and pass to Midgard MVC rendering
$this->templating->display($request);
}
private function _after_serve()
{
// add any cleanup after serve() here
$this->cleanup_after_request();
}
private function cleanup_after_request()
{
// commit session
if ($this->dispatcher->session_is_started())
{
$this->dispatcher->session_commit();
}
// Clean up the context
$this->context->delete();
}
/**
* Access to the Midgard MVC instance
*/
public static function get_instance($local_configuration = null)
{
if (!is_null(self::$instance))
{
return self::$instance;
}
if (is_array($local_configuration))
{
// Configuration passed as a PHP array
$configuration = $local_configuration;
}
elseif ( is_string($local_configuration)
&& substr($local_configuration, 0, 1) == '/')
{
// Application YAML file provided, load configuration from it
if (!file_exists($local_configuration))
{
throw new Exception("Application configuration file {$local_configuration} not found");
}
$configuration_yaml = file_get_contents($local_configuration);
$configuration = midgardmvc_core::read_yaml($configuration_yaml);
}
else
{
throw new Exception('Unrecognized configuration given for Midgard MVC initialization');
}
if (!isset($configuration['services_dispatcher']))
{
throw new Exception('Dispatcher not defined in configuration given for Midgard MVC initialization');
}
if (!isset($configuration['providers_component']))
{
throw new Exception('Component provider not defined in configuration given for Midgard MVC initialization');
}
// Load and return MVC instance
self::$instance = new midgardmvc_core();
self::$instance->load_base_services($configuration);
return self::$instance;
}
public static function clear_instance()
{
self::$instance = null;
}
public static function read_yaml($yaml_string)
{
if (empty($yaml_string))
{
return array();
}
static $use_yaml = null;
static $yaml_function = null;
if (is_null($use_yaml))
{
// Check for YAML extension
if (extension_loaded('yaml'))
{
$yaml_function = 'yaml_parse';
$use_yaml = true;
}
elseif (extension_loaded('syck'))
{
$yaml_function = 'syck_load';
$use_yaml = true;
}
if (!$use_yaml)
{
// YAML PHP extension is not loaded, include the pure-PHP implementation
if (!class_exists('sfYaml'))
{
require MIDGARDMVC_ROOT . '/midgardmvc_core/helpers/sfYaml/sfYaml.php';
}
}
}
if (!$use_yaml)
{
return sfYaml::load($yaml_string);
}
return $yaml_function($yaml_string);
}
public static function write_yaml($yaml)
{
if (empty($yaml))
{
return '';
}
static $use_yaml = null;
static $yaml_function = null;
if (is_null($use_yaml))
{
// Check for YAML extension
if (extension_loaded('yaml'))
{
$yaml_function = 'yaml_emit';
$use_yaml = true;
}
elseif (extension_loaded('syck'))
{
$yaml_function = 'syck_dump';
$use_yaml = true;
}
if (!$use_yaml)
{
// YAML PHP extension is not loaded, include the pure-PHP implementation
if (!class_exists('sfYaml'))
{
require MIDGARDMVC_ROOT . '/midgardmvc_core/helpers/sfYaml/sfYaml.php';
}
}
}
if (!$use_yaml)
{
return sfYaml::dump($yaml);
}
return $yaml_function($yaml);
}
}
?>