1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouter\Config; |
4
|
|
|
|
5
|
|
|
use \ArrayAccess; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A wrapper object to the SnappyRouter configuration. |
9
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
10
|
|
|
* @author Dan Bruce <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Config implements ArrayAccess, ConfigInterface |
13
|
|
|
{ |
14
|
|
|
/** the config key for the list of handlers */ |
15
|
|
|
const KEY_HANDLERS = 'handlers'; |
16
|
|
|
/** the config key for the DI provider */ |
17
|
|
|
const KEY_DI = 'di'; |
18
|
|
|
/** the config key for the list of handler options */ |
19
|
|
|
const KEY_OPTIONS = 'options'; |
20
|
|
|
/** the config key for a class */ |
21
|
|
|
const KEY_CLASS = 'class'; |
22
|
|
|
/** the config key for a file */ |
23
|
|
|
const KEY_FILE = 'file'; |
24
|
|
|
/** the config key for the list of services (deprecated) */ |
25
|
|
|
const KEY_SERVICES = 'services'; |
26
|
|
|
/** the config key for the list of controllers */ |
27
|
|
|
const KEY_CONTROLLERS = 'services'; |
28
|
|
|
/** the config key for the list of plugins */ |
29
|
|
|
const KEY_PLUGINS = 'plugins'; |
30
|
|
|
/** the config key for the list of controller namespaces */ |
31
|
|
|
const KEY_NAMESPACES = 'namespaces'; |
32
|
|
|
/** the config key for the list of controller folders */ |
33
|
|
|
const KEY_FOLDERS = 'folders'; |
34
|
|
|
/** the config key for the list of tasks */ |
35
|
|
|
const KEY_TASKS = 'tasks'; |
36
|
|
|
/** the config key for debug mode */ |
37
|
|
|
const KEY_DEBUG = 'debug'; |
38
|
|
|
|
39
|
|
|
// the internal config array |
40
|
|
|
private $config; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor for the class. |
44
|
|
|
* @param mixed $config An array of config settings (or something that easily |
45
|
|
|
* typecasts to an array like an stdClass). |
46
|
|
|
*/ |
47
|
10 |
|
public function __construct($config) |
48
|
|
|
{ |
49
|
10 |
|
$this->config = (array)$config; |
50
|
10 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns whether or not the given key exists in the config. |
54
|
|
|
* @param string $key The key to be checked. |
55
|
|
|
* @return Returns true if the key exists and false otherwise. |
56
|
|
|
*/ |
57
|
9 |
|
public function offsetExists($key) |
58
|
|
|
{ |
59
|
9 |
|
return isset($this->config[$key]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the value associated with the key or null if no value exists. |
64
|
|
|
* @param string $key The key to be fetched. |
65
|
|
|
* @return Returns the value associated with the key or null if no value exists. |
66
|
|
|
*/ |
67
|
9 |
|
public function offsetGet($key) |
68
|
|
|
{ |
69
|
9 |
|
return $this->offsetExists($key) ? $this->config[$key] : null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets the value associated with the given key. |
74
|
|
|
* @param string $key The key to be used. |
75
|
|
|
* @param mixed $value The value to be set. |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
2 |
|
public function offsetSet($key, $value) |
79
|
|
|
{ |
80
|
2 |
|
if (null === $key) { |
81
|
1 |
|
throw new \Exception('Config values must contain a key.'); |
82
|
|
|
} |
83
|
1 |
|
$this->config[$key] = $value; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Removes the value set to the given key. |
88
|
|
|
* @param string $key The key to unset. |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
1 |
|
public function offsetUnset($key) |
92
|
|
|
{ |
93
|
1 |
|
unset($this->config[$key]); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the value associated with the given key. An optional default value |
98
|
|
|
* can be provided and will be returned if no value is associated with the key. |
99
|
|
|
* @param string $key The key to be used. |
100
|
|
|
* @param mixed $defaultValue The default value to return if the key currently |
101
|
|
|
* has no value associated with it. |
102
|
|
|
* @return Returns the value associated with the key or the default value if |
103
|
|
|
* no value is associated with the key. |
104
|
|
|
*/ |
105
|
9 |
|
public function get($key, $defaultValue = null) |
106
|
|
|
{ |
107
|
9 |
|
return $this->offsetExists($key) ? $this->offsetGet($key) : $defaultValue; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets the current value associated with the given key. |
112
|
|
|
* @param string $key The key to be set. |
113
|
|
|
* @param mixed $value The value to be set to the key. |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
1 |
|
public function set($key, $value) |
117
|
|
|
{ |
118
|
1 |
|
return $this->offsetSet($key, $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns an array representation of the whole configuration. |
123
|
|
|
* @return array An array representation of the whole configuration. |
124
|
|
|
*/ |
125
|
1 |
|
public function toArray() |
126
|
|
|
{ |
127
|
1 |
|
return $this->config; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns whether or not we are in debug mode. |
132
|
|
|
* @return boolean Returns true if the router is in debug mode and false |
133
|
|
|
* otherwise. |
134
|
|
|
*/ |
135
|
1 |
|
public function isDebug() |
136
|
|
|
{ |
137
|
1 |
|
return (bool)$this->get(self::KEY_DEBUG, false); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|