1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ 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\Formz\Core; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Service\Traits\SelfInstantiateTrait; |
17
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
18
|
|
|
use TYPO3\CMS\Extbase\Object\Container\ClassInfo; |
19
|
|
|
use TYPO3\CMS\Extbase\Object\Container\Container as ExtbaseContainer; |
20
|
|
|
use TYPO3\CMS\Extbase\Reflection\PropertyReflection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This container allows injecting dependencies in objects. It is used on |
24
|
|
|
* objects that were fetched from cache. |
25
|
|
|
* |
26
|
|
|
* Unfortunately, Extbase container does not have a public method for this, so |
27
|
|
|
* this is a pure copy of the original container functions. |
28
|
|
|
*/ |
29
|
|
|
class Container extends ExtbaseContainer |
30
|
|
|
{ |
31
|
|
|
use SelfInstantiateTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ExtbaseContainer |
35
|
|
|
*/ |
36
|
|
|
protected $extbaseContainer; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Injects extbase real container. |
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
$this->extbaseContainer = GeneralUtility::makeInstance(ExtbaseContainer::class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @see \Romm\Formz\Core\Container |
48
|
|
|
* |
49
|
|
|
* @param object $instance |
50
|
|
|
*/ |
51
|
|
|
public function injectDependenciesInInstance($instance) |
52
|
|
|
{ |
53
|
|
|
$classInfo = $this->getClassInfo(get_class($instance)); |
54
|
|
|
|
55
|
|
|
if (!$classInfo->hasInjectMethods() && !$classInfo->hasInjectProperties()) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($classInfo->getInjectMethods() as $injectMethodName => $classNameToInject) { |
60
|
|
|
$instanceToInject = $this->extbaseContainer->getInstance($classNameToInject); |
61
|
|
|
if (is_callable([$instance, $injectMethodName])) { |
62
|
|
|
$instance->{$injectMethodName}($instanceToInject); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
foreach ($classInfo->getInjectProperties() as $injectPropertyName => $classNameToInject) { |
66
|
|
|
$instanceToInject = $this->extbaseContainer->getInstance($classNameToInject); |
67
|
|
|
$propertyReflection = GeneralUtility::makeInstance(PropertyReflection::class, $instance, $injectPropertyName); |
68
|
|
|
|
69
|
|
|
$propertyReflection->setAccessible(true); |
70
|
|
|
$propertyReflection->setValue($instance, $instanceToInject); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @see \Romm\Formz\Core\Container |
76
|
|
|
* |
77
|
|
|
* @param string $className |
78
|
|
|
* @return ClassInfo |
79
|
|
|
*/ |
80
|
|
|
private function getClassInfo($className) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$classNameHash = md5($className); |
83
|
|
|
$classInfo = $this->getClassInfoCache()->get($classNameHash); |
84
|
|
|
|
85
|
|
|
if (!$classInfo instanceof ClassInfo) { |
86
|
|
|
$classInfo = $this->getClassInfoFactory()->buildClassInfoFromClassName($className); |
87
|
|
|
$this->getClassInfoCache()->set($classNameHash, $classInfo); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $classInfo; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.