You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+61Lines changed: 61 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -133,3 +133,64 @@ class Acme_Service
133
133
```
134
134
135
135
If the specified key does not exist, or is empty, the argument will be null or empty.
136
+
137
+
### Explicitly Providing Dependencies via __dependencies
138
+
139
+
Certain Magento classes, such as controllers, observers and blocks are instantiated by the system and therefore cannot be instantiated by the DI Container, nevertheless an additional service tag can make it easier to explicitly provide dependencies to these classes. Adding the mage.injectable tag to a service will allow you to provide this services dependencies *after* the service has been instantiated. The services arguments will be provided to the __dependencies method provided you call ServiceInjector::setupDependencies($class) after service instantiation, and your service has a __dependencies method that contains typehints that match the arguments type an order.
140
+
141
+
For convinience, setupDependencies is called on controllers by hooking into the pre-dispatch event, for other classes such as observers and blocks you will have to call it yourself by overriding the class constructor.
142
+
143
+
For controllers, a service definition might look like this
And the class itself will implement __dependencies() thus:
163
+
```php
164
+
/**
165
+
* @var Mage_Catalog_Model_Product
166
+
*/
167
+
private $catalog;
168
+
169
+
/**
170
+
* @var Mage_Sales_Model_Order
171
+
*/
172
+
private $order;
173
+
174
+
/**
175
+
* @param Mage_Catalog_Model_Product $catalog
176
+
* @param Mage_Sales_Model_Order $order
177
+
*/
178
+
public function __dependencies(
179
+
Mage_Catalog_Model_Product $catalog,
180
+
Mage_Sales_Model_Order $order
181
+
) {
182
+
$this->catalog = $catalog;
183
+
$this->order = $order;
184
+
}
185
+
```
186
+
187
+
Your controller will still be coupled to Mage due to extending Controller, and thus - untestable, but it will be clear what the controllers dependencies are and they will be type-hinted.
188
+
189
+
To provide dependencies to other classes after they are instantiated, in addition to using the mage.injectable tag and implementing __dependencies, you will have to override your class' constructor, for example:
0 commit comments