|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouter\Di; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A simple store for DI purposes. |
|
9
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
|
10
|
|
|
* @author Dan Bruce <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class Di implements DiInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private $elements; // a cache of instantiated elements |
|
15
|
|
|
private $elementMap; // the map between keys and their elements |
|
16
|
|
|
|
|
17
|
|
|
private static $instance; // a static instance of this class for static use |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor for the class. |
|
21
|
|
|
* @param array $elementMap An optional initial set of elements to use. |
|
22
|
|
|
*/ |
|
23
|
61 |
|
public function __construct($elementMap = array()) |
|
24
|
|
|
{ |
|
25
|
61 |
|
$this->elementMap = is_array($elementMap) ? $elementMap : array(); |
|
26
|
61 |
|
$this->elements = array(); |
|
27
|
61 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Returns the element associated with the specified key. |
|
31
|
|
|
* @param string $element The key for the element. |
|
32
|
|
|
* @param boolean $useCache An optional flag for whether we can use the |
|
33
|
|
|
* cached version of the element (defaults to true). |
|
34
|
|
|
* @return Returns the associated element. |
|
35
|
|
|
* @throws \Exception Throws an exception if no element is registered for |
|
36
|
|
|
* the given key. |
|
37
|
|
|
*/ |
|
38
|
34 |
|
public function get($element, $useCache = true) |
|
39
|
|
|
{ |
|
40
|
34 |
|
if ($useCache && isset($this->elements[$element])) { |
|
41
|
|
|
// return the cached version |
|
42
|
7 |
|
return $this->elements[$element]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
34 |
|
if (isset($this->elementMap[$element])) { |
|
46
|
31 |
|
if (is_callable($this->elementMap[$element])) { |
|
47
|
|
|
// if we have callback, invoke it and cache the result |
|
48
|
1 |
|
$this->elements[$element] = call_user_func( |
|
49
|
1 |
|
$this->elementMap[$element], |
|
50
|
1 |
|
$this |
|
51
|
|
|
); |
|
52
|
|
|
} else { |
|
53
|
|
|
// otherwise simply cache the result and return it |
|
54
|
30 |
|
$this->elements[$element] = $this->elementMap[$element]; |
|
55
|
|
|
} |
|
56
|
31 |
|
return $this->elements[$element]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
7 |
|
throw new Exception('No element registered for key: '.$element); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Assigns a specific element to the given key. This method will override |
|
64
|
|
|
* any previously assigned element for the given key. |
|
65
|
|
|
* @param string $key The key for the specified element. |
|
66
|
|
|
* @param mixed $element The specified element. This can be an instance of the |
|
67
|
|
|
* element or a callback to be invoked. |
|
68
|
|
|
* @return Returns $this. |
|
69
|
|
|
*/ |
|
70
|
25 |
|
public function set($key, $element) |
|
71
|
|
|
{ |
|
72
|
|
|
// clear the cached element |
|
73
|
25 |
|
unset($this->elements[$key]); |
|
74
|
25 |
|
$this->elementMap[$key] = $element; |
|
75
|
25 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns whether or not a given element has been registered. |
|
80
|
|
|
* @param string $element The key for the element. |
|
81
|
|
|
* @return Returns true if the element is registered and false otherwise. |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function hasElement($element) |
|
84
|
|
|
{ |
|
85
|
2 |
|
return isset($this->elementMap[$element]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns an array of all registered keys. |
|
90
|
|
|
* @return An array of all registered keys. |
|
91
|
|
|
*/ |
|
92
|
3 |
|
public function allRegisteredElements() |
|
93
|
|
|
{ |
|
94
|
3 |
|
return array_keys($this->elementMap); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns the current default DI instance. |
|
99
|
|
|
* @return Di The current default DI instance. |
|
100
|
|
|
*/ |
|
101
|
19 |
|
public static function getDefault() |
|
102
|
|
|
{ |
|
103
|
19 |
|
if (isset(self::$instance)) { |
|
104
|
19 |
|
return self::$instance; |
|
105
|
|
|
} |
|
106
|
1 |
|
self::$instance = new self(); |
|
107
|
1 |
|
return self::$instance; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Sets the current default DI instance.. |
|
112
|
|
|
* @param DiInterface $instance An instance of DI. |
|
113
|
|
|
* @return Di Returns the new default DI instance. |
|
114
|
|
|
*/ |
|
115
|
11 |
|
public static function setDefault(DiInterface $instance) |
|
116
|
|
|
{ |
|
117
|
11 |
|
self::$instance = $instance; |
|
118
|
11 |
|
return self::$instance; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Clears the current default DI instance. |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public static function clearDefault() |
|
125
|
|
|
{ |
|
126
|
1 |
|
self::$instance = null; |
|
127
|
1 |
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|