De Pcms container
Het eindresultaat is dan:
Code (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
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
<?php
namespace Pcms;
class Container
{
/**
* The stored values
* @var array
*/
protected $values = array();
/**
* Whether the services should be shared amoung calls
* @var array
*/
protected $shared = array();
/**
* The instances of shared services
* @var array
*/
protected $instances = array();
/**
* Contains an array of configurators for each service
* @see configure()
* @var array
*/
protected $configurators = array();
public function __construct()
{
$this->setUp();
}
/**
* Allows subclasses to setup itself
*/
protected function setUp()
{
}
/**
* Set a service or parameter
* @param string $key The identifier
* @param mixed|callable $value The callable (in case of a service) or value (in case of a parameter)
* @param boolean $shared Whether the service instance should be shared
*/
public function set($key, $value, $shared = false)
{
$this->values[$key] = $value;
$this->shared[$key] = $shared;
}
/**
*
* @param string $key The identifier
* @return mixed The parameter value or service instance
*/
public function get($key)
{
if(!isset($this->values[$key]))
throw new \InvalidArgumentException(sprintf(
"Value %s has not been set",
$key
));
$value = $this->values[$key];
// If value is a service
if(is_callable($value)) {
// If service is shared and already instanciated, return instance
if($this->shared[$key] && isset($this->instances[$key]))
return $this->instances[$key];
// Call the closure and create the instance
$instance = $value($this);
// If any configurators are set, apply each to the instance
if(isset($this->configurators[$key]))
foreach($this->configurators[$key] as $configurator) {
$instance = $configurator($instance, $this);
}
// Store shared services
if($this->shared[$key])
$this->instances[$key] = $instance;
return $instance;
// If value is a parameter
} else {
return $value;
}
}
/**
* Set wheter the service should be shared
* @param string $key
* @param boolean $shared
*/
public function setShared($key, $shared)
{
$this->shared[$key] = $shared;
}
/**
* Set an array of parameters
* @param array $array
*/
public function setParameterArray(array $array)
{
foreach($array as $key => $value)
{
$this->values[$key] = $value;
$this->shared[$key] = false;
}
}
/**
* Extend container with all values of the given container
* @param Container $c The container whose values should be set
*/
public function extend(Container $c)
{
$c->applyValuesTo($this);
}
/**
* Set all values at given container. Helper function for extend()
* @see extend()
* @param Container $c The container at which the values should be set
*/
public function applyValuesTo(Container $c)
{
foreach($this->values as $key => $value) {
$c->set($key, $value, $this->shared[$key]);
if(isset($this->configurators[$key]))
foreach($this->configurators[$key] as $configurator)
$c->configure($key, $configurator);
}
}
/**
* Configure a service. The $configurator callable will be aplied to the instance after creation.
* @param string $key
* @param callable $configurator
*/
public function configure($key, $configurator)
{
if(!is_callable($configurator))
throw new \InvalidArgumentException('The configurator should be a callable');
$this->configurators[$key][] = $configurator;
}
}
?>
namespace Pcms;
class Container
{
/**
* The stored values
* @var array
*/
protected $values = array();
/**
* Whether the services should be shared amoung calls
* @var array
*/
protected $shared = array();
/**
* The instances of shared services
* @var array
*/
protected $instances = array();
/**
* Contains an array of configurators for each service
* @see configure()
* @var array
*/
protected $configurators = array();
public function __construct()
{
$this->setUp();
}
/**
* Allows subclasses to setup itself
*/
protected function setUp()
{
}
/**
* Set a service or parameter
* @param string $key The identifier
* @param mixed|callable $value The callable (in case of a service) or value (in case of a parameter)
* @param boolean $shared Whether the service instance should be shared
*/
public function set($key, $value, $shared = false)
{
$this->values[$key] = $value;
$this->shared[$key] = $shared;
}
/**
*
* @param string $key The identifier
* @return mixed The parameter value or service instance
*/
public function get($key)
{
if(!isset($this->values[$key]))
throw new \InvalidArgumentException(sprintf(
"Value %s has not been set",
$key
));
$value = $this->values[$key];
// If value is a service
if(is_callable($value)) {
// If service is shared and already instanciated, return instance
if($this->shared[$key] && isset($this->instances[$key]))
return $this->instances[$key];
// Call the closure and create the instance
$instance = $value($this);
// If any configurators are set, apply each to the instance
if(isset($this->configurators[$key]))
foreach($this->configurators[$key] as $configurator) {
$instance = $configurator($instance, $this);
}
// Store shared services
if($this->shared[$key])
$this->instances[$key] = $instance;
return $instance;
// If value is a parameter
} else {
return $value;
}
}
/**
* Set wheter the service should be shared
* @param string $key
* @param boolean $shared
*/
public function setShared($key, $shared)
{
$this->shared[$key] = $shared;
}
/**
* Set an array of parameters
* @param array $array
*/
public function setParameterArray(array $array)
{
foreach($array as $key => $value)
{
$this->values[$key] = $value;
$this->shared[$key] = false;
}
}
/**
* Extend container with all values of the given container
* @param Container $c The container whose values should be set
*/
public function extend(Container $c)
{
$c->applyValuesTo($this);
}
/**
* Set all values at given container. Helper function for extend()
* @see extend()
* @param Container $c The container at which the values should be set
*/
public function applyValuesTo(Container $c)
{
foreach($this->values as $key => $value) {
$c->set($key, $value, $this->shared[$key]);
if(isset($this->configurators[$key]))
foreach($this->configurators[$key] as $configurator)
$c->configure($key, $configurator);
}
}
/**
* Configure a service. The $configurator callable will be aplied to the instance after creation.
* @param string $key
* @param callable $configurator
*/
public function configure($key, $configurator)
{
if(!is_callable($configurator))
throw new \InvalidArgumentException('The configurator should be a callable');
$this->configurators[$key][] = $configurator;
}
}
?>
« vorige pagina | volgende pagina »
Inhoudsopgave
- Inleiding
- Dependency Injection
- Dependency Injection Container
- Pcms container in opbouw - 1
- Pcms container in opbouw - 2
- De Pcms container
- Conclusie