Completed
Push — master ( 78a5a7...ff3135 )
by Tim
03:32
created

getSmartObjectClassLoadingIgnorePattern()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * Management for Smart Objects.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Autoloader;
9
10
use HDNET\Autoloader\Service\SmartObjectInformationService;
11
use HDNET\Autoloader\Utility\ClassNamingUtility;
12
use HDNET\Autoloader\Utility\FileUtility;
13
use HDNET\Autoloader\Utility\ModelUtility;
14
use HDNET\Autoloader\Utility\ReflectionUtility;
15
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18
/**
19
 * Management for Smart Objects.
20
 */
21
class SmartObjectManager implements SingletonInterface
22
{
23
    /**
24
     * Return the SQL String for all registered smart objects.
25
     *
26
     * @return string
27
     */
28
    public static function getSmartObjectRegisterSql()
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 \implode(LF, $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
        // $riskAutoLoader = [
54
        //     'SJBR\\StaticInfoTables\\Cache\\CachedClassLoader',
55
        //     'autoload',
56
        // ];
57
        // $registerAutoLoader = spl_autoload_unregister($riskAutoLoader);
58
59
        $smartObjectClassLoadingIgnorePattern = self::getSmartObjectClassLoadingIgnorePattern();
60
        if ('' !== \trim($smartObjectClassLoadingIgnorePattern) && \preg_match($smartObjectClassLoadingIgnorePattern, $className)) {
61
            return false;
62
        }
63
64
        if (!\class_exists($className)) {
65
            return false;
66
        }
67
68
        if (!ReflectionUtility::isInstantiable($className)) {
69
            return false;
70
        }
71
72
        $return = false !== ReflectionUtility::getFirstTagValue($className, 'db');
73
74
        // if ($registerAutoLoader) {
75
        //     spl_autoload_register($riskAutoLoader, true, true);
76
        // }
77
78
        return $return;
79
    }
80
81
    /**
82
     * Check and create the TCA information
83
     * disable this for better performance.
84
     */
85
    public static function checkAndCreateTcaInformation()
86
    {
87
        $register = SmartObjectRegister::getRegister();
88
89
        $baseTemplatePath = ExtensionManagementUtility::extPath('autoloader', 'Resources/Private/Templates/TcaFiles/');
90
        $defaultTemplate = GeneralUtility::getUrl($baseTemplatePath . 'Default.tmpl');
91
        $overrideTemplate = GeneralUtility::getUrl($baseTemplatePath . 'Override.tmpl');
92
93
        $search = [
94
            '__modelName__',
95
            '__tableName__',
96
            '__extensionKey__',
97
        ];
98
99
        foreach ($register as $model) {
100
            $extensionKey = ClassNamingUtility::getExtensionKeyByModel($model);
101
            $basePath = ExtensionManagementUtility::extPath($extensionKey) . 'Configuration/TCA/';
102
103
            $tableName = ModelUtility::getTableNameByModelReflectionAnnotation($model);
104
            if ('' !== $tableName) {
105
                $tcaFileName = $basePath . 'Overrides/' . $tableName . '.php';
106
                $template = $overrideTemplate;
107
            } else {
108
                $tableName = ModelUtility::getTableNameByModelName($model);
109
                $tcaFileName = $basePath . $tableName . '.php';
110
                $template = $defaultTemplate;
111
            }
112
113
            if (!\is_file($tcaFileName)) {
114
                $replace = [
115
                    '\\' . \trim($model) . '::class',
116
                    $tableName,
117
                    $extensionKey,
118
                ];
119
120
                $content = \str_replace($search, $replace, $template);
121
                FileUtility::writeFileAndCreateFolder($tcaFileName, $content);
122
            }
123
        }
124
    }
125
126
    /**
127
     * Get ignore pattern.
128
     *
129
     * @return string
130
     */
131
    protected static function getSmartObjectClassLoadingIgnorePattern()
132
    {
133
        $configuration = \unserialize((string) $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['autoloader']);
134
135
        return isset($configuration['smartObjectClassLoadingIgnorePattern']) ? (string) $configuration['smartObjectClassLoadingIgnorePattern'] : '';
136
    }
137
}
138