Passed
Branch master (6e47b6)
by Nicolaas
05:16 queued 03:28
created

ConfigList::configurableClasses()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.2888
1
<?php
2
3
namespace Sunnysideup\ConfigManager\Api;
4
5
use ReflectionClass;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\ClassInfo;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Config\Configurable;
10
use SilverStripe\Core\Extensible;
11
use SilverStripe\Core\Injector\Injectable;
12
13
class ConfigList
14
{
15
    use Extensible;
16
    use Injectable;
17
    use Configurable;
18
19
    protected $locationIncludes = [];
20
21
    protected $classNameIncludes = [];
22
23
    private static $do_not_show = [
1 ignored issue
show
introduced by
The private property $do_not_show is not used, and could be removed.
Loading history...
24
        'extra_methods',
25
        'built_in_methods',
26
        // 'db',
27
        // 'has_one',
28
        // 'has_many',
29
        // 'many_many',
30
        // 'belongs_many_many',
31
        // 'many_many_extraFields',
32
        // 'belongs',
33
        // 'field_labels',
34
        // 'searchable_fields',
35
        // 'defaults',
36
        // 'casting',
37
        // 'indexes',
38
        // 'summary_fields',
39
        // 'singular_name',
40
        // 'plural_name',
41
        // 'allowed_actions',
42
        // 'api_access',
43
        // 'validation_enabled',
44
        // 'cache_has_own_table',
45
        // 'fixed_fields',
46
        // 'classname_spec_cache',
47
        // 'subclass_access',
48
        // 'create_table_options',
49
        // 'default_records',
50
        // 'belongs_to',
51
        // 'many_many_extraFields',
52
        // 'default_sort',
53
    ];
54
55
    public function setLocationIncludes($a)
56
    {
57
        $this->locationIncludes = $a;
58
59
        return $this;
60
    }
61
62
    public function setClassNameIncludes($a)
63
    {
64
        $this->classNameIncludes = $a;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function getListOfConfigs()
73
    {
74
75
        $resultArray = [];
76
77
        $doNotShow = $this->Config()->get('do_not_show');
78
79
        $config = Config::inst();
80
        $alsoSet = $config->getAll();
81
82
        $classes = $this->configurableClasses();
83
        foreach ($classes as $class) {
84
            $reflector = new ReflectionClass($class);
85
            $fileName = $reflector->getFileName();
86
            $fileName = str_replace(Director::baseFolder(), '', $fileName);
87
            $statics = $reflector->getStaticProperties();
88
89
            //lists
90
            $deltaList = $this->getDeltas($config, $class);
91
            $staticList = [];
92
            $dynamicList = array_keys($alsoSet[strtolower($class)]);
93
            $staticListDefaultOnes = [];
94
            $staticListCachingStatics = [];
95
            foreach (array_keys($statics) as $key) {
96
                if (in_array($key, $doNotShow, true)) {
97
                    $staticListDefaultOnes[$key] = $key;
98
                } elseif (
99
                    substr($key, 0, 1) === '_' ||
100
                    strpos($key, 'cache') !== false
101
                ) {
102
                    $staticListCachingStatics[$key] = $key;
103
                } else {
104
                    $staticList[$key] = $key;
105
                }
106
            }
107
            $vendor = 'n/a';
108
            $package = 'n/a';
109
            $shorterClassname = $class;
110
            $classNameArray = explode('\\', $class);
111
            if (count($classNameArray) > 1) {
112
                $vendor = $classNameArray[0];
113
                $package = $classNameArray[1];
114
                array_shift($classNameArray);
115
                array_shift($classNameArray);
116
                $shorterClassname = implode(' / ', $classNameArray);
117
            }
118
            $lists = [
119
                'runtime' => $deltaList,
120
                'property' => $staticList,
121
                'caching' => $staticListCachingStatics,
122
                'system' => $staticListDefaultOnes,
123
                'dynamic' => $dynamicList,
124
            ];
125
            $shortClassName = ClassInfo::shortName($class);
126
            $ancestry = ClassInfo::ancestry($class);
127
            foreach($lists as $type => $list) {
128
                if (count($list)) {
129
                    foreach($list as $property) {
130
                        $key = str_replace('/', '-', $fileName.'-'.$property);
131
                        if(!isset($resultArray[$key])) {
132
                            $value = $config->get($class, $property, Config::UNINHERITED);
133
                            $hasValue = $value ? true : false;
134
                            $originalValue = '';
135
                            if(is_object($value)) {
136
                                $value = 'object';
137
                            } else {
138
                                if($reflector->hasProperty($property)) {
139
                                    $propertyObject = $reflector->getProperty($property);
140
                                    $propertyObject->setAccessible(true);
141
                                    $originalValue = $propertyObject->getValue($reflector);
142
                                }
143
                            }
144
                            $isDefault = true;
145
                            $default = '';
146
                            if($originalValue && $originalValue !== $value) {
147
                                $isDefault = false;
148
                                if($value && $originalValue) {
149
                                    $default = $originalValue;
150
                                }
151
                            }
152
                            $hasDefault = $originalValue ? true : false;
153
                            $resultArray[$key] = [
154
                                'Vendor' => $vendor,
155
                                'Package' => $package,
156
                                'ClassName' => $class,
157
                                'ShorterClassName' => $shorterClassname,
158
                                'ShortClassName' => $shortClassName,
159
                                'FileLocation' => $fileName,
160
                                'ParentClasses' => $ancestry,
161
                                'Property' => $property,
162
                                'Type' => $type,
163
                                'IsDefault' => $isDefault,
164
                                'HasDefault' => $hasDefault,
165
                                'HasValue' => $hasValue,
166
                                'Default' => $default,
167
                                'Value' => $value,
168
                            ];
169
                        }
170
                    }
171
                }
172
            }
173
        }
174
175
        return $resultArray;
176
    }
177
178
    protected function configurableClasses()
179
    {
180
        $definedClasses = ClassInfo::allClasses();
181
        return array_filter(
182
            $definedClasses,
183
            function ($className) {
184
                if (class_exists($className)) {
185
                    $autoload = true;
186
                    $traits = [];
187
                    $class = $className;
188
                    // Get traits of all parent classes
189
                    do {
190
                        $traits = array_merge(class_uses($class, $autoload), $traits);
191
                        $class = get_parent_class($class);
192
                    } while ($class);
193
194
                    // Get traits of all parent traits
195
                    $traitsToSearch = $traits;
196
                    while (! empty($traitsToSearch)) {
197
                        $newTraits = class_uses(array_pop($traitsToSearch), $autoload);
198
                        $traits = array_merge($newTraits, $traits);
199
                        $traitsToSearch = array_merge($newTraits, $traitsToSearch);
200
                    }
201
202
                    foreach (array_keys($traits) as $trait) {
203
                        $traits = array_merge(class_uses($trait, $autoload), $traits);
204
                    }
205
                    return isset($traits[Configurable::class]);
206
                }
207
            }
208
        );
209
    }
210
211
    public function getDeltas($config, $className)
212
    {
213
        $deltaList = [];
214
        $deltas = $config->getDeltas($className);
215
        if(count($deltas)) {
216
            foreach($deltas as $deltaInners) {
217
                if(isset($deltaInners['config'])) {
218
                    $deltaList = array_merge(
219
                        $deltaList,
220
                        array_keys($deltaInners['config'])
221
                    );
222
                }
223
            }
224
        }
225
226
        return $deltaList;
227
    }
228
}
229