SmartObjectManager   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 9
dl 0
loc 108
rs 10
c 0
b 0
f 0
ccs 0
cts 40
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSmartObjectRegisterSql() 0 12 2
A isSmartObjectClass() 0 22 5
A checkAndCreateTcaInformation() 0 40 4
A getSmartObjectClassLoadingIgnorePattern() 0 6 2
1
<?php
2
3
/**
4
 * Management for Smart Objects.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader;
9
10
use Doctrine\Common\Annotations\AnnotationReader;
11
use HDNET\Autoloader\Annotation\DatabaseTable;
12
use HDNET\Autoloader\Service\SmartObjectInformationService;
13
use HDNET\Autoloader\Utility\ClassNamingUtility;
14
use HDNET\Autoloader\Utility\FileUtility;
15
use HDNET\Autoloader\Utility\ModelUtility;
16
use HDNET\Autoloader\Utility\ReflectionUtility;
17
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
/**
21
 * Management for Smart Objects.
22
 */
23
class SmartObjectManager implements SingletonInterface
24
{
25
    /**
26
     * Return the SQL String for all registered smart objects.
27
     */
28
    public static function getSmartObjectRegisterSql(): array
29
    {
30
        $informationService = SmartObjectInformationService::getInstance();
31
        $register = SmartObjectRegister::getRegister();
32
33
        $output = [];
34
        foreach ($register as $modelName) {
35
            $output[] = $informationService->getDatabaseInformation($modelName);
36
        }
37
38
        return $output;
39
    }
40
41
    /**
42
     * Check if the given class is a smart object.
43
     *
44
     * Also add a work around, because the static_info_tables SPL Autoloader
45
     * get into a conflict with different classes.
46
     *
47
     * @param string $className
48
     *
49
     * @return bool
50
     */
51
    public static function isSmartObjectClass($className)
52
    {
53
        $smartObjectClassLoadingIgnorePattern = self::getSmartObjectClassLoadingIgnorePattern();
54
        if ('' !== trim($smartObjectClassLoadingIgnorePattern) && preg_match($smartObjectClassLoadingIgnorePattern, $className)) {
55
            return false;
56
        }
57
58
        if (!class_exists($className)) {
59
            return false;
60
        }
61
62
        if (!ReflectionUtility::isInstantiable($className)) {
63
            return false;
64
        }
65
66
        /** @var AnnotationReader $annotationReader */
67
        $annotationReader = GeneralUtility::makeInstance(AnnotationReader::class);
68
69
        $classNameRef = new \ReflectionClass($className);
70
71
        return null !== $annotationReader->getClassAnnotation($classNameRef, DatabaseTable::class);
72
    }
73
74
    /**
75
     * Check and create the TCA information
76
     * disable this for better performance.
77
     */
78
    public static function checkAndCreateTcaInformation(): void
79
    {
80
        $register = SmartObjectRegister::getRegister();
81
82
        $baseTemplatePath = ExtensionManagementUtility::extPath('autoloader', 'Resources/Private/Templates/TcaFiles/');
83
        $defaultTemplate = GeneralUtility::getUrl($baseTemplatePath . 'Default.tmpl');
84
        $overrideTemplate = GeneralUtility::getUrl($baseTemplatePath . 'Override.tmpl');
85
86
        $search = [
87
            '__modelName__',
88
            '__tableName__',
89
            '__extensionKey__',
90
        ];
91
92
        foreach ($register as $model) {
93
            $extensionKey = ClassNamingUtility::getExtensionKeyByModel($model);
94
            $basePath = ExtensionManagementUtility::extPath($extensionKey) . 'Configuration/TCA/';
95
96
            $tableName = ModelUtility::getTableNameByModelReflectionAnnotation($model);
97
            if ('' !== $tableName) {
98
                $tcaFileName = $basePath . 'Overrides/' . $tableName . '.php';
99
                $template = $overrideTemplate;
100
            } else {
101
                $tableName = ModelUtility::getTableNameByModelName($model);
102
                $tcaFileName = $basePath . $tableName . '.php';
103
                $template = $defaultTemplate;
104
            }
105
106
            if (!is_file($tcaFileName)) {
107
                $replace = [
108
                    '\\' . trim($model) . '::class',
109
                    $tableName,
110
                    $extensionKey,
111
                ];
112
113
                $content = str_replace($search, $replace, $template);
114
                FileUtility::writeFileAndCreateFolder($tcaFileName, $content);
115
            }
116
        }
117
    }
118
119
    /**
120
     * Get ignore pattern.
121
     *
122
     * @return string
123
     */
124
    protected static function getSmartObjectClassLoadingIgnorePattern()
125
    {
126
        $configuration = unserialize((string)$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['autoloader']);
127
128
        return isset($configuration['smartObjectClassLoadingIgnorePattern']) ? (string)$configuration['smartObjectClassLoadingIgnorePattern'] : '';
129
    }
130
}
131