-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathComponentManager.php
90 lines (80 loc) · 2.84 KB
/
ComponentManager.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
<?php
/**
* ComponentManager
*
* @author Fabrizio Branca
* @since 2011-11-24
*/
class Menta_ComponentManager {
/**
* @var array
*/
protected static $components = array();
protected static $rewrites = array();
/**
* Set rewrite
*
* @static
* @param string $originalClassname
* @param string $targetClassname
* @throws Exception|InvalidArgumentException
* @return void
*/
public static function addRewrite($originalClassname, $targetClassname) {
if (empty($originalClassname) || !is_string($originalClassname)) {
throw new InvalidArgumentException('Invalid originalClassname');
}
if (empty($targetClassname) || !is_string($targetClassname)) {
throw new InvalidArgumentException('Invalid targetClassname');
}
if (isset(self::$rewrites[$originalClassname])) {
throw new Exception("Rewrite for '$originalClassname'' already exists.");
}
self::$rewrites[$originalClassname] = $targetClassname;
}
/**
* Get component
*
* @static
* @throws Exception|InvalidArgumentException
* @param string $component
* @param string $instanceKey
* @return Menta_Interface_Component
*/
public static function get($component, $instanceKey='default') {
if (empty($component) || !is_string($component)) {
throw new InvalidArgumentException('Parameter "component" must be a classname');
}
if (empty($instanceKey) || !is_string($instanceKey)) {
throw new InvalidArgumentException('Parameter "instanceKey" must be a non empty string');
}
$originalComponentClass = $component;
// resolve rewrite
if (isset(self::$rewrites[$component])) {
$component = self::$rewrites[$component];
}
if (!isset(self::$components[$component])) {
self::$components[$component] = array();
}
if (!isset(self::$components[$component][$instanceKey])) {
if (!class_exists($component)) {
throw new Exception('Could not find component '.$component);
}
self::$components[$component][$instanceKey] = new $component();
if (!self::$components[$component][$instanceKey] instanceof Menta_Interface_Component) {
throw new Exception("Component '$component' does not implement interface 'Menta_Interfaces_Component'");
}
if ($originalComponentClass != $component && !self::$components[$component][$instanceKey] instanceof $originalComponentClass) {
throw new Exception("Rewrite '$component' does not extend original class '$originalComponentClass'");
}
// fire events
$eventParamaters = array(
'component' => self::$components[$component][$instanceKey],
'instanceKey' => $instanceKey
);
Menta_Events::dispatchEvent('after_component_create', $eventParamaters);
Menta_Events::dispatchEvent('after_component_create_' . $component, $eventParamaters);
}
return self::$components[$component][$instanceKey];
}
}