1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onigoetz\Deployer\Configuration\Containers; |
4
|
|
|
|
5
|
|
|
use Onigoetz\Deployer\Configuration\ConfigurationManager; |
6
|
|
|
|
7
|
|
|
abstract class ConfigurationContainer |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string The name of the element |
11
|
|
|
*/ |
12
|
|
|
protected $name; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array The data for this container |
16
|
|
|
*/ |
17
|
|
|
protected $data; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Onigoetz\Deployer\Configuration\ConfigurationManager |
21
|
|
|
*/ |
22
|
|
|
protected $manager; |
23
|
|
|
|
24
|
237 |
|
public function __construct($name, array $data, ConfigurationManager $manager) |
25
|
|
|
{ |
26
|
237 |
|
$this->name = $name; |
27
|
237 |
|
$this->data = $data; |
28
|
237 |
|
$this->manager = $manager; |
29
|
237 |
|
} |
30
|
|
|
|
31
|
69 |
|
public function getName() |
32
|
|
|
{ |
33
|
69 |
|
return $this->name; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the configuration manager's key |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
51 |
|
public function getContainerType() |
42
|
|
|
{ |
43
|
51 |
|
return strtolower(implode('', array_slice(explode('\\', get_class($this)), -1))); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the value or throw an exception |
48
|
|
|
* |
49
|
|
|
* @param string $key |
50
|
|
|
* @param $errorMessage |
51
|
|
|
* @throws \LogicException |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
48 |
|
protected function getValueOrFail($key, $errorMessage) |
55
|
|
|
{ |
56
|
48 |
|
if (array_key_exists($key, $this->data)) { |
57
|
39 |
|
return $this->data[$key]; |
58
|
|
|
} |
59
|
|
|
|
60
|
15 |
|
throw new \LogicException($errorMessage); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the value or return the default |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @param $default |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
18 |
|
protected function getValueOrDefault($key, $default) |
71
|
|
|
{ |
72
|
18 |
|
if (array_key_exists($key, $this->data)) { |
73
|
12 |
|
return $this->data[$key]; |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
return $default; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Tests validity |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
57 |
|
public function isValid() |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
57 |
|
return $this->checkValidity(); |
88
|
30 |
|
} catch (\LogicException $e) { |
89
|
30 |
|
$this->manager->log($e->getMessage()); |
90
|
|
|
|
91
|
30 |
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Internal validity check |
97
|
|
|
* |
98
|
|
|
* @throws \LogicException |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
abstract public function checkValidity(); |
102
|
|
|
} |
103
|
|
|
|