Completed
Push — master ( a4db1b...f41d1c )
by
unknown
02:57 queued 28s
created

Core::injectObjectManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * 2016 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Configuration Object project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\ConfigurationObject\Core;
15
16
use Romm\ConfigurationObject\Exceptions\MethodNotFoundException;
17
use Romm\ConfigurationObject\Service\Items\Parents\ParentsUtility;
18
use Romm\ConfigurationObject\Service\ServiceFactory;
19
use Romm\ConfigurationObject\Validation\ValidatorResolver;
20
use TYPO3\CMS\Core\Cache\CacheManager;
21
use TYPO3\CMS\Core\SingletonInterface;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Extbase\Object\ObjectManager;
24
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
25
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
26
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
27
28
/**
29
 * General functions.
30
 *
31
 * The structure is here to help unit tests to mock correctly what is needed.
32
 */
33
class Core implements SingletonInterface
34
{
35
36
    /**
37
     * @var Core
38
     */
39
    protected static $instance;
40
41
    /**
42
     * @var ObjectManagerInterface
43
     */
44
    protected $objectManager;
45
46
    /**
47
     * @var ReflectionService
48
     */
49
    protected $reflectionService;
50
51
    /**
52
     * @var ValidatorResolver
53
     */
54
    protected $validatorResolver;
55
56
    /**
57
     * @var CacheManager
58
     */
59
    protected $cacheManager;
60
61
    /**
62
     * @var ParentsUtility
63
     */
64
    protected $parentsUtility;
65
66
    /**
67
     * @var array
68
     */
69
    protected $existingClassList = [];
70
71
    /**
72
     * @var array[]
73
     */
74
    protected $gettablePropertiesOfObjects = [];
75
76
    /**
77
     * @return Core
78
     */
79
    public static function get()
80
    {
81
        if (null === self::$instance) {
82
            /** @var ObjectManager $objectManager */
83
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
84
85
            self::$instance = $objectManager->get(self::class);
86
        }
87
88
        return self::$instance;
89
    }
90
91
    /**
92
     * Internal function which will check if the given class exists. This is
93
     * useful because of the calls to undefined class, which can lead to a lack
94
     * of performance due to the auto-loader called if the name of the class
95
     * is not registered yet.
96
     *
97
     * This function will store the already checked class name in local cache.
98
     *
99
     * @param string $className
100
     * @return bool
101
     */
102
    public function classExists($className)
103
    {
104
        if (false === isset($this->existingClassList[$className])) {
105
            $this->existingClassList[$className] = class_exists($className);
106
        }
107
108
        return $this->existingClassList[$className];
109
    }
110
111
    /**
112
     * Returns the list of properties which are accessible for this given
113
     * object.
114
     *
115
     * Properties are stored in local cache to improve performance.
116
     *
117
     * @param object $object
118
     * @return array
119
     */
120
    public function getGettablePropertiesOfObject($object)
121
    {
122
        $className = get_class($object);
123
124
        if (false === isset($this->gettablePropertiesOfObjects[$className])) {
125
            $this->gettablePropertiesOfObjects[$className] = [];
126
            $properties = $this->getReflectionService()->getClassPropertyNames($className);
127
128
            foreach ($properties as $propertyName) {
129
                if (true === $this->isPropertyGettable($object, $propertyName)) {
130
                    $this->gettablePropertiesOfObjects[$className][] = $propertyName;
131
                }
132
            }
133
        }
134
135
        return $this->gettablePropertiesOfObjects[$className];
136
    }
137
138
    /**
139
     * Will check if the property of the given object is gettable. Meaning it
140
     * can be accessed either:
141
     *
142
     * - By the true getter if it does exist;
143
     * - Or by a magic method.
144
     *
145
     * @param object $object
146
     * @param string $propertyName
147
     * @return bool
148
     */
149
    protected function isPropertyGettable($object, $propertyName)
150
    {
151
        $flag = false;
152
153
        if (ObjectAccess::isPropertyGettable($object, $propertyName)) {
154
            $flag = true;
155
            $getterMethodName = 'get' . ucfirst($propertyName);
156
157
            if (false === method_exists($object, $getterMethodName)
158
                && is_callable([$object, $getterMethodName])
159
            ) {
160
                try {
161
                    $object->$getterMethodName();
162
                } catch (MethodNotFoundException $e) {
163
                    $flag = false;
164
                }
165
            }
166
        }
167
168
        return $flag;
169
    }
170
171
    /**
172
     * @return ServiceFactory
173
     */
174
    public function getServiceFactoryInstance()
175
    {
176
        return GeneralUtility::makeInstance(ServiceFactory::class);
177
    }
178
179
    /**
180
     * @return ObjectManagerInterface
181
     */
182
    public function getObjectManager()
183
    {
184
        return $this->objectManager;
185
    }
186
187
    /**
188
     * @param ObjectManagerInterface $objectManager
189
     */
190
    public function injectObjectManager(ObjectManagerInterface $objectManager)
191
    {
192
        $this->objectManager = $objectManager;
193
    }
194
195
    /**
196
     * @return ReflectionService
197
     */
198
    public function getReflectionService()
199
    {
200
        return $this->reflectionService;
201
    }
202
203
    /**
204
     * @param ReflectionService $reflectionService
205
     */
206
    public function injectReflectionService(ReflectionService $reflectionService)
207
    {
208
        $this->reflectionService = $reflectionService;
209
    }
210
211
    /**
212
     * @return ValidatorResolver
213
     */
214
    public function getValidatorResolver()
215
    {
216
        return $this->validatorResolver;
217
    }
218
219
    /**
220
     * @param ValidatorResolver $validatorResolver
221
     */
222
    public function injectValidatorResolver(ValidatorResolver $validatorResolver)
223
    {
224
        $this->validatorResolver = $validatorResolver;
225
    }
226
227
    /**
228
     * @return CacheManager
229
     */
230
    public function getCacheManager()
231
    {
232
        return $this->cacheManager;
233
    }
234
235
    /**
236
     * @param CacheManager $cacheManager
237
     */
238
    public function injectCacheManager(CacheManager $cacheManager)
239
    {
240
        $this->cacheManager = $cacheManager;
241
    }
242
243
    /**
244
     * @return ParentsUtility
245
     */
246
    public function getParentsUtility()
247
    {
248
        return $this->parentsUtility;
249
    }
250
251
    /**
252
     * @param ParentsUtility $parentsUtility
253
     */
254
    public function injectParentsUtility(ParentsUtility $parentsUtility)
255
    {
256
        $this->parentsUtility = $parentsUtility;
257
    }
258
}
259