1 | <?php |
||
28 | abstract class AbstractEditableDiscovery implements EditableDiscovery |
||
29 | { |
||
30 | /** |
||
31 | * @var BindingInitializer[] |
||
32 | */ |
||
33 | private $initializers; |
||
34 | |||
35 | /** |
||
36 | * @var BindingInitializer[][] |
||
37 | */ |
||
38 | private $initializersByBindingClass = array(); |
||
39 | |||
40 | /** |
||
41 | * Creates a new discovery. |
||
42 | * |
||
43 | * @param BindingInitializer[] $initializers The binding initializers to |
||
44 | * apply to newly created or |
||
45 | * unserialized bindings. |
||
46 | */ |
||
47 | 228 | public function __construct(array $initializers = array()) |
|
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 56 | public function removeBindings($typeName = null, Expression $expr = null) |
|
56 | { |
||
57 | 56 | Assert::nullOrStringNotEmpty($typeName, 'The type name must be a non-empty string. Got: %s'); |
|
58 | |||
59 | 51 | if (null !== $typeName) { |
|
60 | 34 | if (null !== $expr) { |
|
61 | 17 | $this->removeBindingsWithTypeNameThatMatch($typeName, $expr); |
|
62 | } else { |
||
63 | 34 | $this->removeBindingsWithTypeName($typeName); |
|
64 | } |
||
65 | 17 | } elseif (null !== $expr) { |
|
66 | 5 | $this->removeBindingsThatMatch($expr); |
|
67 | } else { |
||
68 | 12 | $this->removeAllBindings(); |
|
69 | } |
||
70 | 51 | } |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 40 | public function hasBindings($typeName = null, Expression $expr = null) |
|
76 | { |
||
77 | 40 | Assert::nullOrStringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s'); |
|
78 | |||
79 | 30 | if (null !== $typeName) { |
|
80 | 20 | if (null !== $expr) { |
|
81 | 10 | return $this->hasBindingsWithTypeNameThatMatch($typeName, $expr); |
|
82 | } |
||
83 | |||
84 | 10 | return $this->hasBindingsWithTypeName($typeName); |
|
85 | } |
||
86 | |||
87 | 10 | if (null !== $expr) { |
|
88 | return $this->hasBindingsThatMatch($expr); |
||
89 | } |
||
90 | |||
91 | 10 | return $this->hasAnyBinding(); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Removes all bindings from the discovery. |
||
96 | */ |
||
97 | abstract protected function removeAllBindings(); |
||
98 | |||
99 | /** |
||
100 | * Removes all bindings from the discovery that match an expression. |
||
101 | * |
||
102 | * @param Expression $expr The expression to filter by. |
||
103 | */ |
||
104 | abstract protected function removeBindingsThatMatch(Expression $expr); |
||
105 | |||
106 | /** |
||
107 | * Removes all bindings bound to the given binding type. |
||
108 | * |
||
109 | * @param string $typeName The name of the binding type. |
||
110 | */ |
||
111 | abstract protected function removeBindingsWithTypeName($typeName); |
||
112 | |||
113 | /** |
||
114 | * Removes all bindings bound to the given binding type that match an expression. |
||
115 | * |
||
116 | * @param string $typeName The name of the binding type. |
||
117 | * @param Expression $expr The expression to filter by. |
||
118 | */ |
||
119 | abstract protected function removeBindingsWithTypeNameThatMatch($typeName, Expression $expr); |
||
120 | |||
121 | /** |
||
122 | * Returns whether the discovery contains bindings. |
||
123 | * |
||
124 | * @return bool Returns `true` if the discovery has bindings and `false` |
||
125 | * otherwise. |
||
126 | */ |
||
127 | abstract protected function hasAnyBinding(); |
||
128 | |||
129 | /** |
||
130 | * Returns whether the discovery contains bindings that match an expression. |
||
131 | * |
||
132 | * @param Expression $expr The expression to filter by. |
||
133 | * |
||
134 | * @return bool Returns `true` if the discovery has bindings and `false` |
||
135 | * otherwise. |
||
136 | */ |
||
137 | abstract protected function hasBindingsThatMatch(Expression $expr); |
||
138 | |||
139 | /** |
||
140 | * Returns whether the discovery contains bindings for the given type. |
||
141 | * |
||
142 | * @param string $typeName The name of the binding type. |
||
143 | * |
||
144 | * @return bool Returns `true` if bindings bound to the given binding type |
||
145 | * are found and `false` otherwise. |
||
146 | */ |
||
147 | abstract protected function hasBindingsWithTypeName($typeName); |
||
148 | |||
149 | /** |
||
150 | * Returns whether the discovery contains bindings for the given type that |
||
151 | * match an expression. |
||
152 | * |
||
153 | * @param string $typeName The name of the binding type. |
||
154 | * @param Expression $expr The expression to filter by. |
||
155 | * |
||
156 | * @return bool Returns `true` if bindings bound to the given binding type |
||
157 | * are found and `false` otherwise. |
||
158 | */ |
||
159 | abstract protected function hasBindingsWithTypeNameThatMatch($typeName, Expression $expr); |
||
160 | |||
161 | /** |
||
162 | * Initializes a binding. |
||
163 | * |
||
164 | * @param Binding $binding The binding to initialize. |
||
165 | * |
||
166 | * @throws BindingNotAcceptedException If the loaded type does not accept |
||
167 | * the binding. |
||
168 | */ |
||
169 | 89 | protected function initializeBinding(Binding $binding) |
|
191 | |||
192 | /** |
||
193 | * Initializes multiple bindings. |
||
194 | * |
||
195 | * @param Binding[] $bindings The bindings to initialize. |
||
196 | */ |
||
197 | 81 | protected function initializeBindings(array $bindings) |
|
203 | } |
||
204 |