Completed
Push — master ( 216ca8...2dc9c5 )
by Robbie
11s
created

AbstractConfigCommand::getStaticConfig()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 6
eloc 12
nc 7
nop 0
1
<?php
2
3
namespace SilverLeague\Console\Command\Config;
4
5
use SilverLeague\Console\Command\SilverStripeCommand;
6
use SilverStripe\Core\ClassInfo;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Object;
9
10
/**
11
 * Provide base functionality for retrieving configuration from SilverStripe
12
 *
13
 * @package silverstripe-console
14
 * @author  Robbie Averill <[email protected]>
15
 */
16
class AbstractConfigCommand extends SilverStripeCommand
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $yamlConfig;
22
23
    /**
24
     * @var \SilverStripe\Core\Manifest\ConfigManifest
25
     */
26
    protected $configManifest;
27
28
    /**
29
     * Gets the parsed YAML configuration array from the ConfigManifest
30
     *
31
     * @return array
32
     */
33
    public function getYamlConfig()
34
    {
35
        if ($this->yamlConfig === null) {
36
            $manifest = $this->getConfigManifest();
37
            $this->yamlConfig = $this->getPropertyValue($manifest, 'yamlConfig');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getPropertyValue($manifest, 'yamlConfig') of type * is incompatible with the declared type array of property $yamlConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        }
39
        return $this->yamlConfig;
40
    }
41
42
    /**
43
     * Assemble a data-set that would be returned from ConfigStaticManifest, if it were a bit more
44
     * useful for external API access.
45
     *
46
     * @return array
47
     */
48
    public function getStaticConfig()
49
    {
50
        $output = [];
51
        foreach (ClassInfo::subclassesFor(Object::class) as $class => $filename) {
52
            $classConfig = [];
53
            $reflection = new \ReflectionClass($class);
54
            foreach ($reflection->getProperties() as $property) {
55
                /** @var ReflectionProperty $property */
56
                if ($property->isPrivate() && $property->isStatic()) {
57
                    $property->setAccessible(true);
58
                    $classConfig[$property->getName()] = $property->getValue();
59
                }
60
            }
61
            if (!empty($classConfig)) {
62
                $output[$class] = $classConfig;
63
            }
64
        }
65
        return $output;
66
    }
67
68
    /**
69
     * Gets the ConfigManifest from the current Config instance
70
     *
71
     * @return \SilverStripe\Core\Manifest\ConfigManifest
72
     */
73
    public function getConfigManifest()
74
    {
75
        if ($this->configManifest === null) {
76
            $manifests = $this->getPropertyValue($this->getConfig(), 'manifests');
77
            $this->configManifest = array_shift($manifests);
78
        }
79
        return $this->configManifest;
80
    }
81
82
    /**
83
     * Gets any overrides made to the manifest
84
     *
85
     * @return array
86
     */
87
    public function getConfigOverrides()
88
    {
89
        $overrides = (array) $this->getPropertyValue($this->getConfig(), 'overrides');
90
        return !empty($overrides) ? array_shift($overrides) : [];
91
    }
92
93
    /**
94
     * Get the SilverStripe Config model
95
     *
96
     * @return Config
97
     */
98
    public function getConfig()
99
    {
100
        return Config::inst();
101
    }
102
103
    /**
104
     * Gets the value of a non-public property from the given class instance
105
     *
106
     * @param  object $class
107
     * @param  string $propertyName
108
     * @return mixed
109
     */
110
    protected function getPropertyValue($class, $propertyName)
111
    {
112
        $reflectionClass = new \ReflectionClass($class);
113
        $property = $reflectionClass->getProperty($propertyName);
114
        $property->setAccessible(true);
115
        return $property->getValue($class);
116
    }
117
}
118