1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Config; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Provides an object with a {@see ConfigInterface configuration container}. |
9
|
|
|
* |
10
|
|
|
* This is a full implementation of {@see ConfigurableInterface}. |
11
|
|
|
*/ |
12
|
|
|
trait ConfigurableTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The config object. |
16
|
|
|
* |
17
|
|
|
* @var ConfigInterface |
18
|
|
|
*/ |
19
|
|
|
private $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Sets the object's configuration container. |
23
|
|
|
* |
24
|
|
|
* @param mixed $config The Config object, datamap, or filepath. |
25
|
|
|
* @throws InvalidArgumentException If the parameter is invalid. |
26
|
|
|
* @return self Chainable |
27
|
|
|
*/ |
28
|
|
|
public function setConfig($config) |
29
|
|
|
{ |
30
|
|
|
if (is_string($config)) { |
31
|
|
|
// Treat the parameter as a filepath |
32
|
|
|
$this->config = $this->createConfig($config); |
33
|
|
|
} elseif (is_array($config)) { |
34
|
|
|
$this->config = $this->createConfig($config); |
35
|
|
|
} elseif ($config instanceof ConfigInterface) { |
36
|
|
|
$this->config = $config; |
37
|
|
|
} else { |
38
|
|
|
throw new InvalidArgumentException(sprintf( |
39
|
|
|
'Configset must be an associative array, a file path, or an instance of %s', |
40
|
|
|
ConfigInterface::class |
41
|
|
|
)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Gets the object's configuration container or a specific key from the container. |
49
|
|
|
* |
50
|
|
|
* @param string|null $key If provided, the data key to retrieve. |
51
|
|
|
* @param mixed $default The fallback value to return if $key does not exist. |
52
|
|
|
* @return mixed If $key is NULL, the Config object is returned. |
53
|
|
|
* If $key is given, its value on the Config object is returned. |
54
|
|
|
* If the value of $key is NULL, the value of $default is returned. |
55
|
|
|
*/ |
56
|
|
|
public function config($key = null, $default = null) |
57
|
|
|
{ |
58
|
|
|
if ($this->config === null) { |
59
|
|
|
$this->config = $this->createConfig(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($key !== null) { |
63
|
|
|
if ($this->config->has($key)) { |
64
|
|
|
return $this->config->get($key); |
65
|
|
|
} elseif (!is_string($default) && is_callable($default)) { |
66
|
|
|
return $default(); |
67
|
|
|
} else { |
68
|
|
|
return $default; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->config; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create a new ConfigInterface instance for the object. |
77
|
|
|
* |
78
|
|
|
* @see AbstractConfig |
79
|
|
|
* @param mixed $data Initial data. Either a filepath, a datamap, or a Config object. |
80
|
|
|
* @return ConfigInterface A new Config object. |
81
|
|
|
*/ |
82
|
|
|
protected function createConfig($data = null) |
83
|
|
|
{ |
84
|
|
|
return new GenericConfig($data); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|