Completed
Pull Request — master (#14)
by Romain
02:43 queued 38s
created

Core::getCacheService()   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 0
1
<?php
2
/*
3
 * 2017 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\Core\Service\CacheService;
17
use Romm\ConfigurationObject\Exceptions\MethodNotFoundException;
18
use Romm\ConfigurationObject\Service\Items\Parents\ParentsUtility;
19
use Romm\ConfigurationObject\Service\ServiceFactory;
20
use Romm\ConfigurationObject\Validation\ValidatorResolver;
21
use TYPO3\CMS\Core\Cache\CacheManager;
22
use TYPO3\CMS\Core\SingletonInterface;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Extbase\Object\ObjectManager;
25
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
26
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
27
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
28
29
/**
30
 * General functions.
31
 *
32
 * The structure is here to help unit tests to mock correctly what is needed.
33
 */
34
class Core implements SingletonInterface
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 CacheService
68
     */
69
    protected $cacheService;
70
71
    /**
72
     * @var array
73
     */
74
    protected $existingClassList = [];
75
76
    /**
77
     * @var array[]
78
     */
79
    protected $gettablePropertiesOfObjects = [];
80
81
    /**
82
     * @return Core
83
     */
84
    public static function get()
85
    {
86
        if (null === self::$instance) {
87
            /** @var ObjectManager $objectManager */
88
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
89
90
            self::$instance = $objectManager->get(self::class);
91
        }
92
93
        return self::$instance;
94
    }
95
96
    /**
97
     * Internal function which will check if the given class exists. This is
98
     * useful because of the calls to undefined class, which can lead to a lack
99
     * of performance due to the auto-loader called if the name of the class
100
     * is not registered yet.
101
     *
102
     * This function will store the already checked class name in local cache.
103
     *
104
     * @param string $className
105
     * @return bool
106
     */
107
    public function classExists($className)
108
    {
109
        if (false === isset($this->existingClassList[$className])) {
110
            $this->existingClassList[$className] = class_exists($className);
111
        }
112
113
        return $this->existingClassList[$className];
114
    }
115
116
    /**
117
     * Returns the list of properties which are accessible for this given
118
     * object.
119
     *
120
     * Properties are stored in local cache to improve performance.
121
     *
122
     * @param object $object
123
     * @return array
124
     */
125
    public function getGettablePropertiesOfObject($object)
126
    {
127
        $className = get_class($object);
128
129
        if (false === isset($this->gettablePropertiesOfObjects[$className])) {
130
            $this->gettablePropertiesOfObjects[$className] = [];
131
            $properties = $this->getReflectionService()->getClassPropertyNames($className);
132
133
            foreach ($properties as $propertyName) {
134
                if (true === $this->isPropertyGettable($object, $propertyName)) {
135
                    $this->gettablePropertiesOfObjects[$className][] = $propertyName;
136
                }
137
            }
138
        }
139
140
        return $this->gettablePropertiesOfObjects[$className];
141
    }
142
143
    /**
144
     * Will check if the property of the given object is gettable. Meaning it
145
     * can be accessed either:
146
     *
147
     * - By the true getter if it does exist;
148
     * - Or by a magic method.
149
     *
150
     * @param object $object
151
     * @param string $propertyName
152
     * @return bool
153
     */
154
    protected function isPropertyGettable($object, $propertyName)
155
    {
156
        $flag = false;
157
158
        if (ObjectAccess::isPropertyGettable($object, $propertyName)) {
159
            $flag = true;
160
            $getterMethodName = 'get' . ucfirst($propertyName);
161
162
            if (false === method_exists($object, $getterMethodName)
163
                && is_callable([$object, $getterMethodName])
164
            ) {
165
                try {
166
                    $object->$getterMethodName();
167
                } catch (MethodNotFoundException $e) {
168
                    $flag = false;
169
                }
170
            }
171
        }
172
173
        return $flag;
174
    }
175
176
    /**
177
     * @return ServiceFactory
178
     */
179
    public function getServiceFactoryInstance()
180
    {
181
        /** @var ServiceFactory $serviceFactory */
182
        $serviceFactory = GeneralUtility::makeInstance(ServiceFactory::class);
183
184
        return $serviceFactory;
185
    }
186
187
    /**
188
     * @return ObjectManagerInterface
189
     */
190
    public function getObjectManager()
191
    {
192
        return $this->objectManager;
193
    }
194
195
    /**
196
     * @param ObjectManagerInterface $objectManager
197
     */
198
    public function injectObjectManager(ObjectManagerInterface $objectManager)
199
    {
200
        $this->objectManager = $objectManager;
201
    }
202
203
    /**
204
     * @return ReflectionService
205
     */
206
    public function getReflectionService()
207
    {
208
        return $this->reflectionService;
209
    }
210
211
    /**
212
     * @param ReflectionService $reflectionService
213
     */
214
    public function injectReflectionService(ReflectionService $reflectionService)
215
    {
216
        $this->reflectionService = $reflectionService;
217
    }
218
219
    /**
220
     * @return ValidatorResolver
221
     */
222
    public function getValidatorResolver()
223
    {
224
        return $this->validatorResolver;
225
    }
226
227
    /**
228
     * @param ValidatorResolver $validatorResolver
229
     */
230
    public function injectValidatorResolver(ValidatorResolver $validatorResolver)
231
    {
232
        $this->validatorResolver = $validatorResolver;
233
    }
234
235
    /**
236
     * @return CacheManager
237
     */
238
    public function getCacheManager()
239
    {
240
        return $this->cacheManager;
241
    }
242
243
    /**
244
     * @param CacheManager $cacheManager
245
     */
246
    public function injectCacheManager(CacheManager $cacheManager)
247
    {
248
        $this->cacheManager = $cacheManager;
249
    }
250
251
    /**
252
     * @return ParentsUtility
253
     */
254
    public function getParentsUtility()
255
    {
256
        return $this->parentsUtility;
257
    }
258
259
    /**
260
     * @param ParentsUtility $parentsUtility
261
     */
262
    public function injectParentsUtility(ParentsUtility $parentsUtility)
263
    {
264
        $this->parentsUtility = $parentsUtility;
265
    }
266
267
    /**
268
     * @return CacheService
269
     */
270
    public function getCacheService()
271
    {
272
        return $this->cacheService;
273
    }
274
275
    /**
276
     * @param CacheService $cacheService
277
     */
278
    public function injectCacheService(CacheService $cacheService)
279
    {
280
        $this->cacheService = $cacheService;
281
    }
282
}
283