D
D
dllweb2018-07-19 21:46:59
symfony
dllweb, 2018-07-19 21:46:59

Where does symfony DI collect all dependencies?

Hello everyone, just wanted to clarify. Where does symfony DI collect all dependencies?
And through what (which component) does it pass dependencies into methods, properties or a constructor?
For example controller

public function actionIndex(Request $r){
Переменная $r переданная через аргумент метода в контроллере
уже содержит в себе текущий инстанс Request
}

Where in the application did this forwarding (injection) of the instance into the actionIndex method occur?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2018-07-21
@dllweb

Hello everyone, just wanted to clarify. Where does symfony DI collect all dependencies?

https://symfony.com/doc/current/components/depend...
https://symfony.com/doc/current/service_container.html
symfony.com/doc/current/components/dependency_inje...
I think you are asking the wrong question. "Where", "Through what", "In what place" is a direct link to the symfony sources https://github.com/symfony/symfony
It would be more reasonable to answer the questions "How" and "Why".
So D.I. What does this component do.
The configuration of services arrives at his input, no matter in what format and how. yaml, php, xml, etc. (at least implement your own format)
Then it resolves the dependencies and builds the container. At the output, it gives ContainerInterface.
That's all, this is where the main logic of this component ends. All the magic, before and after his main job.
Configuration - the guys added sugar and now the configuration for the component is collected using reflection, which for you collects an analogue of the same yaml file with all the prescribed arguments, parameters and other settings for a particular service.
(Which solves one very important problem - favorite parameters, non-existent dependencies are found right there, and not in runtime)
Cache - after the main work of DI - the container is dumped into a file, it usually lies in var/cache/{env}/{container_name} and connects in the Kernel class.
It is there that all your arguments for your classes, specific classes that implement interfaces, abstractions, etc. are already located.
In general, for a general understanding.
A small note.
Do not put calls to the base, to third-party api and other 3rd services in class methods if this method is involved in the compilation of the container. Otherwise, you will not be able to lift the application.
Banal example. If you make a request to the database in some command in the configure method, then without a configured connection to the database or an available database, you will not be able to clear the cache normally.
The above link - https://github.com/symfony/http-kernel/blob/master... to get controller arguments, is not even responsible for "inclusion" in the service controller, but for "inclusion" of request parameters, session and others .
In addition, I will give this link https://github.com/symfony/http-kernel/blob/master... and this onehttps://github.com/symfony/http-kernel/blob/master...
This is how the http-kernel and di components are related to each other in this case.
A full-fledged answer draws on a series of articles on Habré with only one DI in symfony, but here I tried to describe as briefly as possible "how it works", and not where and in what place. I hope my answer is of some help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question