Completed
Push — master ( a79f71...19c9d7 )
by Tim
12:15
created

Classes/Utility/ModelUtility.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Utility to interact with the Model.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Utility;
9
10
use Doctrine\Common\Annotations\AnnotationReader;
11
use HDNET\Autoloader\Annotation\DatabaseTable;
12
use HDNET\Autoloader\Annotation\SmartExclude;
13
use HDNET\Autoloader\Service\SmartObjectInformationService;
14
use HDNET\Autoloader\SmartObjectRegister;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Extbase\Persistence\Generic\Session;
17
18
/**
19
 * Utility to interact with the Model.
20
 */
21
class ModelUtility
22
{
23
    /**
24
     * Get the table name by either reflection or model name.
25
     *
26
     * @param $modelClassName
27
     *
28
     * @return string
29
     */
30
    public static function getTableName($modelClassName)
31
    {
32
        $reflectionName = self::getTableNameByModelReflectionAnnotation($modelClassName);
33
34
        return '' !== $reflectionName ? $reflectionName : self::getTableNameByModelName($modelClassName);
35
    }
36
37
    /**
38
     * Get the table name by reflection.
39
     *
40
     * @param string $modelClassName
41
     *
42
     * @return string
43
     */
44
    public static function getTableNameByModelReflectionAnnotation($modelClassName)
45
    {
46
        /** @var AnnotationReader $annotationReader */
47
        $annotationReader = GeneralUtility::makeInstance(AnnotationReader::class);
48
        /** @var DatabaseTable $classAnnotation */
49
        $classAnnotation = $annotationReader->getClassAnnotation(new \ReflectionClass($modelClassName), DatabaseTable::class);
50
51
        if (null === $classAnnotation) {
52
            return '';
53
        }
54
55
        return (string)$classAnnotation->tableName;
56
    }
57
58
    /**
59
     * Resolve the table name for the given class name
60
     * Original method from extbase core to create the table name.
61
     *
62
     * @param string $className
63
     *
64
     * @return string The table name
65
     *
66
     * @see DataMapFactory->resolveTableName
67
     */
68
    public static function getTableNameByModelName($className)
69
    {
70
        $className = ltrim($className, '\\');
71
        if (false !== mb_strpos($className, '\\')) {
72
            $classNameParts = explode('\\', $className);
73
            // Skip vendor and product name for core classes
74
            if (0 === mb_strpos($className, 'TYPO3\\CMS\\')) {
75
                $classPartsToSkip = 2;
76
            } else {
77
                $classPartsToSkip = 1;
78
            }
79
            $classNameParts = \array_slice($classNameParts, $classPartsToSkip);
80
            $classNameParts = explode('\\', implode('\\', $classNameParts), 4);
81
            $tableName = 'tx_' . str_replace('\\', '_', mb_strtolower(implode('_', $classNameParts)));
82
        } else {
83
            $tableName = mb_strtolower($className);
84
        }
85
86
        return $tableName;
87
    }
88
89
    /**
90
     * get the smart exclude values e.g. language, workspace,
91
     * enableFields from the given model.
92
     */
93
    public static function getSmartExcludesByModelName(string $name): array
94
    {
95
        /** @var AnnotationReader $annotationReader */
96
        $annotationReader = GeneralUtility::makeInstance(AnnotationReader::class);
97
98
        $reflectionClass = new \ReflectionClass($name);
99
100
        $smartExcludes = $annotationReader->getClassAnnotation($reflectionClass, SmartExclude::class);
101
102
        return null === $smartExcludes ? [] : $smartExcludes->excludes;
103
    }
104
105
    /**
106
     * Get the base TCA for the given Model.
107
     *
108
     * @param string $modelClassName
109
     *
110
     * @return array
111
     */
112
    public static function getTcaInformation($modelClassName)
113
    {
114
        $informationService = SmartObjectInformationService::getInstance();
115
116
        return $informationService->getTcaInformation($modelClassName);
117
    }
118
119
    /**
120
     * Get the default TCA incl. smart object fields.
121
     * Add missing fields to the existing TCA structure.
122
     *
123
     * @param string $extensionKey
124
     * @param string $tableName
125
     *
126
     * @return array
127
     */
128
    public static function getTcaOverrideInformation($extensionKey, $tableName)
129
    {
130
        $return = $GLOBALS['TCA'][$tableName] ?? [];
131
        $classNames = SmartObjectRegister::getRegister();
132
        $informationService = SmartObjectInformationService::getInstance();
133
134
        foreach ($classNames as $className) {
135
            if (ClassNamingUtility::getExtensionKeyByModel($className) !== $extensionKey) {
136
                continue;
137
            }
138
            if (self::getTableNameByModelReflectionAnnotation($className) === $tableName) {
139
                $additionalTca = $informationService->getCustomModelFieldTca($className);
140
                foreach ($additionalTca as $fieldName => $configuration) {
141
                    if (!isset($return['columns'][$fieldName])) {
142
                        $return['columns'][$fieldName] = $configuration;
143
                    }
144
                }
145
            }
146
        }
147
148
        return $return;
149
    }
150
151
    /**
152
     * Get the target model.
153
     *
154
     * @param string $modelName
155
     * @param array  $data
156
     *
157
     * @return \TYPO3\CMS\Extbase\DomainObject\AbstractEntity|object
158
     */
159
    public static function getModel($modelName, $data)
160
    {
161
        // Base query
162
        $query = ExtendedUtility::getQuery($modelName);
163
        $settings = $query->getQuerySettings();
164
        $settings->setRespectStoragePage(false);
165
        $settings->setRespectSysLanguage(false);
166
        $query->matching($query->equals('uid', $data['uid']));
167
168
        if (TYPO3_MODE === 'BE') {
169
            GeneralUtility::makeInstance(Session::class)->destroy();
170
            $settings->setIgnoreEnableFields(true);
171
172
            if (isset($data['sys_language_uid']) && (int)$data['sys_language_uid'] > 0) {
173
                GeneralUtility::_GETset((int)$data['sys_language_uid'], 'L');
0 ignored issues
show
The method _GETset() does not seem to exist on object<TYPO3\CMS\Core\Utility\GeneralUtility>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
174
175
                if (isset($data['l18n_parent']) && $data['l18n_parent'] > 0) {
176
                    $settings->setLanguageOverlayMode(false);
177
                    $settings->setLanguageMode(null);
178
                    $settings->setRespectSysLanguage(true);
179
                    $settings->setLanguageUid((int)$data['sys_language_uid']);
180
                }
181
                $object = $query->execute()->getFirst();
182
183
                GeneralUtility::_GETset(0, 'L');
0 ignored issues
show
The method _GETset() does not seem to exist on object<TYPO3\CMS\Core\Utility\GeneralUtility>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
185
                return $object;
186
            }
187
        }
188
189
        $query->matching($query->equals('uid', $data['uid']));
190
191
        return $query->execute()
192
            ->getFirst()
193
        ;
194
    }
195
}
196