Loader   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 4
dl 0
loc 241
rs 10
c 0
b 0
f 0
ccs 0
cts 62
cp 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A extTables() 0 6 1
A extLocalconf() 0 6 1
A loadExtTables() 0 15 3
A loadExtLocalconf() 0 15 3
A getExtensionKey() 0 4 1
A getVendorName() 0 4 1
A isInstantiableClass() 0 4 1
A buildAutoLoaderObjects() 0 18 4
A getAutoLoaderNamesInRightOrder() 0 17 4
A prepareAutoLoaderObjects() 0 16 2
A buildLoaderInformation() 0 10 2
1
<?php
2
3
/**
4
 * Central Loader object.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader;
9
10
use HDNET\Autoloader\Cache\AutoloaderFileBackend;
11
use HDNET\Autoloader\Utility\ReflectionUtility;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
14
/**
15
 * Central Loader object.
16
 */
17
class Loader implements SingletonInterface
18
{
19
    /**
20
     * The different implementations and the order of the execution.
21
     *
22
     * @var array
23
     */
24
    protected $implementations = [
25
        // class replacement
26
        'Xclass',
27
        'AlternativeImplementations',
28
        // additional functions
29
        'Hooks',
30
        'Slots',
31
        // smart object management
32
        'SmartObjects',
33
        'ContentObjects',
34
        'TcaFiles',
35
        'ExtensionTypoScriptSetup',
36
        'ContextSensitiveHelps',
37
        // non-critical
38
        'Plugins',
39
        'FlexForms',
40
        'CommandController',
41
        'StaticTyposcript',
42
        'ExtensionId',
43
        'TypeConverter',
44
        'BackendLayout',
45
        'LanguageOverride',
46
        'Icon',
47
        'Gridelement',
48
        'FluidNamespace',
49
    ];
50
51
    /**
52
     * The Extension key.
53
     *
54
     * @var string
55
     */
56
    protected $extensionKey;
57
58
    /**
59
     * The vendorName.
60
     *
61
     * @var string
62
     */
63
    protected $vendorName;
64
65
    /**
66
     * Call this method in the ext_tables.php file.
67
     *
68
     * @param string $vendorName
69
     * @param string $extensionKey
70
     */
71
    public static function extTables($vendorName, $extensionKey, array $implementations = []): void
72
    {
73
        /** @var \HDNET\Autoloader\Loader $loader */
74
        $loader = GeneralUtility::makeInstance(self::class);
75
        $loader->loadExtTables($vendorName, $extensionKey, $implementations);
76
    }
77
78
    /**
79
     * Call this method in the ext_localconf.php file.
80
     *
81
     * @param string $vendorName
82
     * @param string $extensionKey
83
     */
84
    public static function extLocalconf($vendorName, $extensionKey, array $implementations = []): void
85
    {
86
        /** @var \HDNET\Autoloader\Loader $loader */
87
        $loader = GeneralUtility::makeInstance(self::class);
88
        $loader->loadExtLocalconf($vendorName, $extensionKey, $implementations);
89
    }
90
91
    /**
92
     * Load the ext tables information.
93
     *
94
     * @param string $vendorName
95
     * @param string $extensionKey
96
     */
97
    public function loadExtTables($vendorName, $extensionKey, array $implementations = []): void
98
    {
99
        $this->extensionKey = $extensionKey;
100
        $this->vendorName = $vendorName;
101
102
        $autoLoaderObjects = $this->buildAutoLoaderObjects($implementations);
103
        $information = $this->prepareAutoLoaderObjects($autoLoaderObjects, LoaderInterface::EXT_TABLES);
104
        foreach ($autoLoaderObjects as $object) {
105
            /** @var LoaderInterface $object */
106
            $informationArray = $information[\get_class($object)];
107
            if (\is_array($informationArray)) {
108
                $object->loadExtensionTables($this, $informationArray);
109
            }
110
        }
111
    }
112
113
    /**
114
     * Load the ext localconf information.
115
     *
116
     * @param string $vendorName
117
     * @param string $extensionKey
118
     */
119
    public function loadExtLocalconf($vendorName, $extensionKey, array $implementations = []): void
120
    {
121
        $this->extensionKey = $extensionKey;
122
        $this->vendorName = $vendorName;
123
124
        $autoLoaderObjects = $this->buildAutoLoaderObjects($implementations);
125
        $information = $this->prepareAutoLoaderObjects($autoLoaderObjects, LoaderInterface::EXT_LOCAL_CONFIGURATION);
126
        foreach ($autoLoaderObjects as $object) {
127
            /** @var LoaderInterface $object */
128
            $informationArray = $information[\get_class($object)];
129
            if (\is_array($informationArray)) {
130
                $object->loadExtensionConfiguration($this, $informationArray);
131
            }
132
        }
133
    }
134
135
    /**
136
     * Get the extension key.
137
     *
138
     * @return string
139
     */
140
    public function getExtensionKey()
141
    {
142
        return $this->extensionKey;
143
    }
144
145
    /**
146
     * Get the vendor name.
147
     *
148
     * @return string
149
     */
150
    public function getVendorName()
151
    {
152
        return $this->vendorName;
153
    }
154
155
    /**
156
     * check if the class is loadable and is instantiable
157
     * (exists and is no interface or abstraction etc.).
158
     *
159
     * @param $class
160
     *
161
     * @return bool
162
     */
163
    public function isInstantiableClass($class)
164
    {
165
        return ReflectionUtility::isInstantiable($class);
166
    }
167
168
    /**
169
     * Build the Autoloader objects.
170
     *
171
     * @return array
172
     */
173
    protected function buildAutoLoaderObjects(array $objectNames = [])
174
    {
175
        static $objectCache = [];
176
        $objectNames = $this->getAutoLoaderNamesInRightOrder($objectNames);
177
        $objects = [];
178
        foreach ($objectNames as $autoLoaderObjectName) {
179
            if (!isset($objectCache[$autoLoaderObjectName])) {
180
                if (class_exists('HDNET\\Autoloader\\Loader\\' . $autoLoaderObjectName)) {
181
                    $objectCache[$autoLoaderObjectName] = GeneralUtility::makeInstance('HDNET\\Autoloader\\Loader\\' . $autoLoaderObjectName);
182
                } else {
183
                    $objectCache[$autoLoaderObjectName] = GeneralUtility::makeInstance($autoLoaderObjectName);
184
                }
185
            }
186
            $objects[] = $objectCache[$autoLoaderObjectName];
187
        }
188
189
        return $objects;
190
    }
191
192
    /**
193
     * Get the Autoloader Names in the right order.
194
     *
195
     * @return array
196
     */
197
    protected function getAutoLoaderNamesInRightOrder(array $objectNames = [])
198
    {
199
        if (empty($objectNames)) {
200
            return $this->implementations;
201
        }
202
203
        // sort
204
        $names = [];
205
        foreach ($this->implementations as $className) {
206
            if (\in_array($className, $objectNames, true)) {
207
                $names[] = $className;
208
                unset($objectNames[array_search($className, $objectNames, true)]);
209
            }
210
        }
211
212
        return array_merge($names, $objectNames);
213
    }
214
215
    /**
216
     * Prepare the autoLoader information.
217
     *
218
     * @param int $type
219
     *
220
     * @return array
221
     */
222
    protected function prepareAutoLoaderObjects(array $objects, $type)
223
    {
224
        $cacheIdentifier = $this->getVendorName() . '_' . $this->getExtensionKey() . '_' . GeneralUtility::shortMD5(serialize($objects)) . '_' . $type;
225
226
        // Do not use Caching Framework here
227
        /** @var AutoloaderFileBackend $cacheBackend */
228
        $cacheBackend = GeneralUtility::makeInstance(AutoloaderFileBackend::class, null);
229
        if ($cacheBackend->has($cacheIdentifier)) {
230
            return $cacheBackend->get($cacheIdentifier);
231
        }
232
233
        $return = $this->buildLoaderInformation($objects, $type);
234
        $cacheBackend->set($cacheIdentifier, $return);
0 ignored issues
show
Documentation introduced by
$return is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
235
236
        return $return;
237
    }
238
239
    /**
240
     * Build the loader information.
241
     *
242
     * @param $objects
243
     * @param $type
244
     *
245
     * @return array
246
     */
247
    protected function buildLoaderInformation($objects, $type)
248
    {
249
        $return = [];
250
        foreach ($objects as $object) {
251
            // LoaderInterface
252
            $return[\get_class($object)] = $object->prepareLoader($this, $type);
253
        }
254
255
        return $return;
256
    }
257
}
258