-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathConfigurationAdapter.php
More file actions
67 lines (56 loc) · 2.06 KB
/
Copy pathConfigurationAdapter.php
File metadata and controls
67 lines (56 loc) · 2.06 KB
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
<?php
/*
* This file is part of the EasyDeploy project.
*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EasyCorp\Bundle\EasyDeployBundle\Configuration;
use EasyCorp\Bundle\EasyDeployBundle\Helper\Str;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* It implements the "Adapter" pattern to allow working with the configuration
* in a consistent manner, even if the configuration of each deployer is
* completely different and defined using incompatible objects.
*/
final class ConfigurationAdapter implements \Stringable
{
private ?ParameterBag $options = null;
public function __construct(private readonly AbstractConfiguration $config)
{
}
public function __toString(): string
{
return Str::formatAsTable($this->getOptions()->all());
}
public function get(string $optionName)
{
if (!$this->getOptions()->has($optionName)) {
throw new \InvalidArgumentException(sprintf('The "%s" option is not defined.', $optionName));
}
return $this->getOptions()->get($optionName);
}
private function getOptions(): ParameterBag
{
if (null !== $this->options) {
return $this->options;
}
// it's not the most beautiful code possible, but making the properties
// private and the methods public allows to configure the deployment using
// a config builder and the IDE autocompletion. Here we need to access
// those private properties and their values
$options = new ParameterBag();
$r = new \ReflectionObject($this->config);
foreach ($r->getProperties() as $property) {
try {
$property->setAccessible(true);
$options->set($property->getName(), $property->getValue($this->config));
} catch (\ReflectionException) {
// ignore this error
}
}
return $this->options = $options;
}
}