DI container

← DI container

Ashterix

docs(articles): Magento and Symfony examples side by side, request lifecycle as a Mermaid diagram

en/articles/magento-to-symfony/core/di.md +24 āˆ’4

24 24
25 25 ## preference → alias
26 26
27 + ::::columns
28 + :::col
27 29 **Magento** Ā· etc/di.xml
28 30
29 31 ```xml
31 33 for="Vendor\Shipping\Api\RateProviderInterface"
32 34 type="Vendor\Shipping\Model\DhlRateProvider"/>
33 35 ```
34 āˆ’
36 + :::
37 + :::col
35 38 **Symfony** Ā· an attribute on the implementation
36 39
37 40 ```php
42 45 {
43 46 }
44 47 ```
48 + :::
49 + ::::
45 50 or in services.yaml:
46 51 ```yaml
47 52 # ...
53 58
54 59 ## virtualType and arguments → named services
55 60
61 + ::::columns
62 + :::col
56 63 **Magento** Ā· a separate log file via virtualType
57 64
58 65 ```xml
77 84 </arguments>
78 85 </type>
79 86 ```
80 āˆ’
87 + :::
88 + :::col
81 89 **Symfony** Ā· a Monolog channel + autowiring by name
82 90
83 91 ```yaml
99 107 ) {}
100 108 }
101 109 ```
110 + :::
111 + ::::
102 112
103 113 The argument name `$faqLogger` selects the `faq` channel. Scoped HTTP clients (`HttpClientInterface $erpClient`), cache pools and lock factories work the same way. The general case, when you need to pass specific values:
104 114
124 134
125 135 ## Plugin → decorator
126 136
137 + ::::columns
138 + :::col
127 139 **Magento** Ā· an after plugin
128 140
129 141 ```xml
145 157 }
146 158 }
147 159 ```
148 āˆ’
160 + :::
161 + :::col
149 162 **Symfony** Ā· a service decorator
150 163
151 164 ```php
167 180 }
168 181 }
169 182 ```
183 + :::
184 + ::::
170 185
171 186 Everyone who asks for `PriceCalculatorInterface` gets the decorator, and it calls the original. Differences from plugins:
172 187
185 200
186 201 ## Arrays in di.xml → tags
187 202
203 + ::::columns
204 + :::col
188 205 **Magento** Ā· a pool of implementations
189 206
190 207 ```xml
197 214 </arguments>
198 215 </type>
199 216 ```
200 āˆ’
217 + :::
218 + :::col
201 219 **Symfony** Ā· a tag + a locator
202 220
203 221 ```php
223 241 }
224 242 }
225 243 ```
244 + :::
245 + ::::
226 246
227 247 A new format is just a new class implementing the interface. `#[AutowireIterator]` gives you an iterator over all implementations (with priorities), `#[AutowireLocator]` gives a lazy map by key.
228 248