Completed
Push — master ( 05a043...a79f71 )
by Tim
15:11
created

Slots::prepareLoader()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 9.0008
c 0
b 0
f 0
ccs 0
cts 23
cp 0
cc 5
nc 3
nop 2
crap 30
1
<?php
2
3
/**
4
 * Loading Slots.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Loader;
9
10
use Doctrine\Common\Annotations\AnnotationReader;
11
use HDNET\Autoloader\Annotation\SignalClass;
12
use HDNET\Autoloader\Annotation\SignalName;
13
use HDNET\Autoloader\Annotation\SignalPriority;
14
use HDNET\Autoloader\Loader;
15
use HDNET\Autoloader\LoaderInterface;
16
use HDNET\Autoloader\Utility\ClassNamingUtility;
17
use HDNET\Autoloader\Utility\FileUtility;
18
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Core\Utility\MathUtility;
21
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
22
23
/**
24
 * Loading Slots.
25
 */
26
class Slots implements LoaderInterface
27
{
28
    /**
29
     * Get all the complex data for the loader.
30
     * This return value will be cached and stored in the database
31
     * There is no file monitoring for this cache.
32
     */
33
    public function prepareLoader(Loader $loader, int $type): array
34
    {
35
        $slots = [];
36
        /** @var AnnotationReader $annotationReader */
37
        $annotationReader = GeneralUtility::makeInstance(AnnotationReader::class);
38
        $slotPath = ExtensionManagementUtility::extPath($loader->getExtensionKey()) . 'Classes/Slots/';
39
        $slotClasses = FileUtility::getBaseFilesInDir($slotPath, 'php');
40
41
        foreach ($slotClasses as $slot) {
42
            $slotClass = ClassNamingUtility::getFqnByPath(
43
                $loader->getVendorName(),
44
                $loader->getExtensionKey(),
45
                'Slots/' . $slot
46
            );
47
48
            if (!$loader->isInstantiableClass($slotClass)) {
49
                continue;
50
            }
51
            $reflectionClass = new \ReflectionClass($slotClass);
52
53
            foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
54
                $signalClassAnnotation = $annotationReader->getMethodAnnotation($method, SignalClass::class);
55
                if (null !== $signalClassAnnotation) {
56
                    $priority = (int)$annotationReader->getMethodAnnotation($method, SignalPriority::class)->argumentName;
57
                    $priority = MathUtility::forceIntegerInRange($priority, 0, 100);
58
59
                    $slots[$priority][] = [
60
                        'signalClassName' => (string)$signalClassAnnotation->argumentName,
61
                        'signalName' => (string)$annotationReader->getMethodAnnotation($method, SignalName::class)->argumentName,
62
                        'slotClassNameOrObject' => $slotClass,
63
                        'slotMethodName' => $method->getName(),
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
64
                    ];
65
                }
66
            }
67
        }
68
69
        return $this->flattenSlotsByPriority($slots);
70
    }
71
72
    /**
73
     * Flatten slots by prio.
74
     *
75
     * @return array
76
     */
77
    public function flattenSlotsByPriority(array $array)
78
    {
79
        krsort($array);
80
        $result = [];
81
        foreach ($array as $slots) {
82
            foreach ($slots as $slot) {
83
                $result[] = $slot;
84
            }
85
        }
86
87 1
        return $result;
88
    }
89 1
90 1
    /**
91 1
     * Run the loading process for the ext_tables.php file.
92 1
     */
93 1
    public function loadExtensionTables(Loader $autoLoader, array $loaderInformation): void
94
    {
95
    }
96
97 1
    /**
98
     * Run the loading process for the ext_localconf.php file.
99
     */
100
    public function loadExtensionConfiguration(Loader $autoLoader, array $loaderInformation): void
101
    {
102
        if (!empty($loaderInformation)) {
103
            /** @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher */
104
            $signalSlotDispatcher = GeneralUtility::makeInstance(Dispatcher::class);
105
            foreach ($loaderInformation as $slot) {
106
                $signalSlotDispatcher->connect(
107
                    $slot['signalClassName'],
108
                    $slot['signalName'],
109
                    $slot['slotClassNameOrObject'],
110
                    $slot['slotMethodName'],
111
                    true
112
                );
113
            }
114
        }
115
    }
116
}
117