Plugins::addPluginInformation()   B
last analyzed

Complexity

Conditions 6
Paths 20

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.6897
c 0
b 0
f 0
ccs 0
cts 11
cp 0
cc 6
nc 20
nop 5
crap 42
1
<?php
2
3
/**
4
 * Loading Plugins.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Loader;
9
10
use Doctrine\Common\Annotations\AnnotationReader;
11
use HDNET\Autoloader\Annotation\NoCache;
12
use HDNET\Autoloader\Annotation\Plugin;
13
use HDNET\Autoloader\Loader;
14
use HDNET\Autoloader\LoaderInterface;
15
use HDNET\Autoloader\Utility\ClassNamingUtility;
16
use HDNET\Autoloader\Utility\FileUtility;
17
use HDNET\Autoloader\Utility\TranslateUtility;
18
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Utility\ExtensionUtility;
21
22
/**
23
 * Loading Plugins.
24
 */
25
class Plugins implements LoaderInterface
26
{
27
    /**
28
     * Get all the complex data for the loader.
29
     * This return value will be cached and stored in the database
30
     * There is no file monitoring for this cache.
31
     */
32
    public function prepareLoader(Loader $loader, int $type): array
33
    {
34
        $pluginInformation = [];
35
        /** @var AnnotationReader $annotationReader */
36
        $annotationReader = GeneralUtility::makeInstance(AnnotationReader::class);
37
38
        $controllerPath = ExtensionManagementUtility::extPath($loader->getExtensionKey()) . 'Classes/Controller/';
39
        $controllers = FileUtility::getBaseFilesRecursivelyInDir($controllerPath, 'php');
40
41
        foreach ($controllers as $controller) {
42
            $controllerName = ClassNamingUtility::getFqnByPath(
43
                $loader->getVendorName(),
44
                $loader->getExtensionKey(),
45
                'Controller/' . $controller
46
            );
47
            if (!$loader->isInstantiableClass($controllerName)) {
48
                continue;
49
            }
50
51
            $reflectionClass = new \ReflectionClass($controllerName);
52
53
            $controllerKey = str_replace('/', '\\', $controller);
54
            $controllerKey = str_replace('Controller', '', $controllerKey);
55
56
            foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
57
                $configuration = $annotationReader->getMethodAnnotation($method, Plugin::class);
58
59
                if (null !== $configuration) {
60
                    $pluginKeys = GeneralUtility::trimExplode(' ', (string)$configuration, true);
61
                    $actionName = str_replace('Action', '', $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...
62
63
                    foreach ($pluginKeys as $pluginKey) {
64
                        $pluginInformation = $this->addPluginInformation(
65
                            $pluginInformation,
66
                            $pluginKey,
67
                            $controllerKey,
68
                            $actionName,
69
                            null !== $annotationReader->getMethodAnnotation($method, NoCache::class)
70
                        );
71
                    }
72
                }
73
            }
74
        }
75
76
        return $pluginInformation;
77
    }
78
79
    /**
80
     * Run the loading process for the ext_tables.php file.
81
     */
82
    public function loadExtensionTables(Loader $loader, array $loaderInformation): void
83
    {
84
        foreach (array_keys($loaderInformation) as $key) {
85
            $label = TranslateUtility::getLllOrHelpMessage('plugin.' . $key, $loader->getExtensionKey());
86
            ExtensionUtility::registerPlugin($loader->getExtensionKey(), $key, $label);
87
        }
88
    }
89
90
    /**
91
     * Run the loading process for the ext_localconf.php file.
92
     */
93
    public function loadExtensionConfiguration(Loader $loader, array $loaderInformation): void
94
    {
95
        $prefix = $loader->getVendorName() . '.' . $loader->getExtensionKey();
96
        foreach ($loaderInformation as $key => $information) {
97
            ExtensionUtility::configurePlugin($prefix, $key, $information['cache'], $information['noCache']);
98
        }
99
    }
100
101
    /**
102
     * Add the given plugin information to the plugin information array.
103
     *
104
     * @param string $pluginKey
105
     * @param string $controllerKey
106
     * @param string $actionName
107
     * @param bool   $noCache
108
     *
109
     * @return array
110
     */
111
    protected function addPluginInformation(array $pluginInformation, $pluginKey, $controllerKey, $actionName, $noCache)
112
    {
113
        $first = false !== mb_strpos($pluginKey, '!');
114
        $pluginKey = trim($pluginKey, '!');
115
116
        if (!isset($pluginInformation[$pluginKey])) {
117
            $pluginInformation[$pluginKey] = [
118
                'cache' => [],
119
                'noCache' => [],
120
            ];
121
        }
122
123
        $parts = $noCache ? [
124
            'cache',
125
            'noCache',
126
        ] : ['cache'];
127
128
        foreach ($parts as $part) {
129
            if (!isset($pluginInformation[$pluginKey][$part][$controllerKey])) {
130
                $pluginInformation[$pluginKey][$part][$controllerKey] = '';
131
            }
132
            $actions = GeneralUtility::trimExplode(',', $pluginInformation[$pluginKey][$part][$controllerKey], true);
133
            if ($first) {
134
                array_unshift($actions, $actionName);
135
                $targetController = [
136
                    $controllerKey => $pluginInformation[$pluginKey][$part][$controllerKey],
137
                ];
138
                unset($pluginInformation[$pluginKey][$part][$controllerKey]);
139
                $pluginInformation[$pluginKey][$part] = array_merge($targetController, $pluginInformation[$pluginKey][$part]);
140
            } else {
141
                $actions[] = $actionName;
142
            }
143
144
            $pluginInformation[$pluginKey][$part][$controllerKey] = implode(',', $actions);
145
        }
146
147
        return $pluginInformation;
148
    }
149
}
150