/
InterfaceInjector.php
executable file
·172 lines (149 loc) · 4.3 KB
/
InterfaceInjector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* InterfaceInjector is used for Interface Injection.
*
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
class InterfaceInjector
{
protected $class;
protected $calls = array();
protected $processedDefinitions = array();
/**
* Contructs interface injector by specifying the target class name
*
* @param string $class
*/
public function __construct($class)
{
$this->class = $class;
}
/**
* Returns the interface name
*
* @return string
*/
public function getClass()
{
return $this->class;
}
/**
* Adds method calls if Definition is of required interface
*
* @param Definition $definition
* @return void
*/
public function processDefinition(Definition $definition, $class = null)
{
if (in_array($definition, $this->processedDefinitions, true)) {
return;
}
$class = $class ?: $definition->getClass();
if (!$this->supports($class)) {
return;
}
foreach ($this->calls as $callback) {
list($method, $arguments) = $callback;
$definition->addMethodCall($method, $arguments);
}
$this->processedDefinitions[] = $definition;
}
/**
* Inspects if current interface injector is to be used with a given class
*
* @param string $object
* @return boolean
*/
public function supports($object)
{
if (is_string($object)) {
$reflection = new \ReflectionClass($object);
return $reflection->implementsInterface($this->class)
|| $reflection->isSubClassOf($this->class)
|| $object === $this->class;
}
if ( ! is_object($object)) {
throw new InvalidArgumentException(sprintf("%s expects class or object, %s given", __METHOD__, substr(str_replace("\n", '', var_export($object, true)), 0, 10)));
}
return is_a($object, $this->class);
}
/**
* Adds a method to call to be injected on any service implementing the interface.
*
* @param string $method The method name to call
* @param array $arguments An array of arguments to pass to the method call
*
* @return InterfaceInjector The current instance
*/
public function addMethodCall($method, array $arguments = array())
{
$this->calls[] = array($method, $arguments);
return $this;
}
/**
* Removes a method to call after service initialization.
*
* @param string $method The method name to remove
*
* @return Definition The current instance
*/
public function removeMethodCall($method)
{
foreach ($this->calls as $i => $call) {
if ($call[0] === $method) {
unset($this->calls[$i]);
break;
}
}
return $this;
}
/**
* Check if the current definition has a given method to call after service initialization.
*
* @param string $method The method name to search for
*
* @return boolean
*/
public function hasMethodCall($method)
{
foreach ($this->calls as $i => $call) {
if ($call[0] === $method) {
return true;
}
}
return false;
}
/**
* Gets the methods to call after service initialization.
*
* @return array An array of method calls
*/
public function getMethodCalls()
{
return $this->calls;
}
/**
* Merges another InterfaceInjector
*
* @param InterfaceInjector $injector
*/
public function merge(InterfaceInjector $injector)
{
if ($this->class === $injector->getClass()) {
foreach ($injector->getMethodCalls() as $call) {
list ($method, $arguments) = $call;
$this->addMethodCall($method, $arguments);
}
}
}
}