|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Default implementation of Puice\Config |
|
4
|
|
|
* |
|
5
|
|
|
* LICENSE: Free for all |
|
6
|
|
|
* |
|
7
|
|
|
* @category Dependency Injection |
|
8
|
|
|
* @package Puice |
|
9
|
|
|
* @copyright Copyright (c) 2014 Alwin Mark |
|
10
|
|
|
* @license Free |
|
11
|
|
|
* @link https://github.com/CansaSCityShuffle/Puice |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Puice\Config; |
|
15
|
|
|
|
|
16
|
|
|
use Puice\Container; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Default implmementation of Puice\Config |
|
20
|
|
|
* |
|
21
|
|
|
* This Class is a Container Class for Puice Dependency Configurations |
|
22
|
|
|
* |
|
23
|
|
|
* @category Dependency Injection |
|
24
|
|
|
* @package Puice |
|
25
|
|
|
* @copyright Copyright (c) 2014 Alwin Mark |
|
26
|
|
|
*/ |
|
27
|
|
|
class DefaultConfig implements Container |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
private $_configurations = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Resets all previous defined configurations |
|
34
|
|
|
*/ |
|
35
|
|
|
public function reset() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->_configurations = array(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Gets a pre defined Dependecy for a specific type and name. |
|
42
|
|
|
* If there is only one Dependency defined for that Kind of type, the |
|
43
|
|
|
* name does not matter. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $type currently only ClassNames with Namespaces are |
|
46
|
|
|
* supported |
|
47
|
|
|
* @param string $name (optional) name of the Dependency. Default is |
|
48
|
|
|
* 'default' |
|
49
|
|
|
* |
|
50
|
|
|
* @return mixed predefined Dependency |
|
51
|
|
|
*/ |
|
52
|
|
|
public function get($type, $name = 'default') |
|
53
|
|
|
{ |
|
54
|
|
|
if (isset($this->_configurations[$type])) { |
|
55
|
|
|
if (isset($this->_configurations[$type][$name])) { |
|
56
|
|
|
return $this->_configurations[$type][$name]; |
|
57
|
|
|
} else |
|
58
|
|
|
// if only one configuration exist autochoose |
|
59
|
|
|
if (count($this->_configurations[$type]) == 1) { |
|
60
|
|
|
return array_pop($this->_configurations[$type]); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Sets a Dependency with the specified type, name and value. |
|
69
|
|
|
* Please make sure, that you call this Method only in (the) config |
|
70
|
|
|
* script(s). |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $type type of the Dependency. Currently only Class |
|
73
|
|
|
* names with Namespaces are supported |
|
74
|
|
|
* @param string $name name of the Dependency |
|
75
|
|
|
* @param mixed $value the Dependency itself |
|
76
|
|
|
*/ |
|
77
|
|
|
public function set($type, $name, $value) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!array_key_exists($type, $this->_configurations)) { |
|
80
|
|
|
$this->_configurations[$type] = array(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (array_key_exists($name, $this->_configurations[$type])) { |
|
84
|
|
|
throw new Exception('Duplicate definition of one Dependency'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->_configurations[$type][$name] = $value; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Bulk sets Dependencies for a given array with two levels: |
|
92
|
|
|
* @example |
|
93
|
|
|
* array( |
|
94
|
|
|
* 'Puice\Config' => array( |
|
95
|
|
|
* 'appConfig' => new MyConfig() |
|
96
|
|
|
* ) |
|
97
|
|
|
* ); |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $configs bulk of dependency definitions like the |
|
100
|
|
|
* example above |
|
101
|
|
|
*/ |
|
102
|
|
|
public function bulkSet(array $configs) |
|
103
|
|
|
{ |
|
104
|
|
|
foreach ($configs as $type => $typeConfigs) { |
|
105
|
|
|
if (! is_array($typeConfigs)) { |
|
106
|
|
|
throw new \InvalidArgumentException( |
|
107
|
|
|
"Given configs for type: $type, must be an array itself" |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
foreach ($typeConfigs as $name => $value) { |
|
112
|
|
|
self::$_appConfig->set($type, $name, $value); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|