These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Service; |
||
15 | |||
16 | use Romm\ConfigurationObject\Exceptions\PropertyNotAccessibleException; |
||
17 | use TYPO3\CMS\Core\SingletonInterface; |
||
18 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||
19 | use TYPO3\CMS\Extbase\Reflection\ClassReflection; |
||
20 | use TYPO3\CMS\Extbase\Reflection\PropertyReflection; |
||
21 | |||
22 | /** |
||
23 | * An abstraction for class reflection, which is used a lot by this API, to |
||
24 | * reduce performance impact. |
||
25 | */ |
||
26 | class ReflectionService implements SingletonInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var ReflectionService |
||
30 | */ |
||
31 | private static $instance; |
||
32 | |||
33 | /** |
||
34 | * @var ClassReflection[] |
||
35 | */ |
||
36 | protected $classReflection = []; |
||
37 | |||
38 | /** |
||
39 | * @var PropertyReflection[] |
||
40 | */ |
||
41 | protected $classAccessibleProperties = []; |
||
42 | |||
43 | /** |
||
44 | * @return ReflectionService |
||
45 | */ |
||
46 | public static function get() |
||
47 | { |
||
48 | if (null === self::$instance) { |
||
49 | self::$instance = GeneralUtility::makeInstance(self::class); |
||
50 | } |
||
51 | |||
52 | return self::$instance; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param string $className |
||
57 | * @return ClassReflection |
||
58 | */ |
||
59 | public function getClassReflection($className) |
||
60 | { |
||
61 | if (false === isset($this->classReflection[$className])) { |
||
62 | $this->classReflection[$className] = GeneralUtility::makeInstance(ClassReflection::class, $className); |
||
63 | } |
||
64 | |||
65 | return $this->classReflection[$className]; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $className |
||
70 | * @return PropertyReflection |
||
71 | */ |
||
72 | public function getAccessibleProperties($className) |
||
73 | { |
||
74 | if (false === isset($this->classAccessibleProperties[$className])) { |
||
75 | $this->classAccessibleProperties[$className] = []; |
||
76 | |||
77 | $classReflection = $this->getClassReflection($className); |
||
78 | $properties = $classReflection->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED); |
||
79 | |||
80 | foreach ($properties as $property) { |
||
81 | $this->classAccessibleProperties[$className][$property->getName()] = $property; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return $this->classAccessibleProperties[$className]; |
||
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param string $className |
||
90 | * @param string $propertyName |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function isClassPropertyAccessible($className, $propertyName) |
||
94 | { |
||
95 | $accessibleProperties = $this->getAccessibleProperties($className); |
||
96 | |||
97 | return isset($accessibleProperties[$propertyName]); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $className |
||
102 | * @param string $propertyName |
||
103 | * @return PropertyReflection |
||
104 | * @throws PropertyNotAccessibleException |
||
105 | */ |
||
106 | public function getClassAccessibleProperty($className, $propertyName) |
||
107 | { |
||
108 | if (false === $this->isClassPropertyAccessible($className, $propertyName)) { |
||
109 | throw new PropertyNotAccessibleException( |
||
110 | "Property $className::$propertyName is not accessible!", |
||
111 | 1489149795 |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | $accessibleProperties = $this->getAccessibleProperties($className); |
||
116 | |||
117 | return $accessibleProperties[$propertyName]; |
||
118 | } |
||
119 | } |
||
120 |