Laravel 4 - Container class: distribution and closure function

I have the following question for the question discussed here: Path to the Laravel kernel path

I, in the same situation as driechel (the author of the question above), was before that, now I'm used to Laravel 4 FW and studying the kernel. Although the exact answer is given, I still do not understand the logic and what is happening under the hood. Therefore, I will greatly appreciate further explanation. I know this may be a duplicate, but since I cannot leave comments, I will give him a chance with a new question. I hope so.

I looked at it from a different angle, starting with this article: http://blog.joynag.net/2013/05/facades-in-laravel-4-and-static-methods-resolution/

When considering the call to File:get() I finally find myself in the container class function <Container> of the container>, which is called with this actual parameter share(function() { return new Filesystem; } .

What I just can't figure out is using $container . Especially with the second appearance in closing:

$object = $closure($container);

Could you clarify this again? Why is $container passed as a parameter here and what is actually contained in it? As far as I understand, $closure at this point executes and executes function() { return new Filesystem; } function() { return new Filesystem; } , which has no input parameter.

I'm lost. I have studied this and the anonymous PHP / close functions now for two consecutive days and still cannot figure it out. I don't understand the syntax of $closure($container) here and not the logic.

+6
source share
1 answer

For reference, this is the share method @ v4.0.5 .

So what is going on here. I will explain this in a couple of steps.

Share method call

As you indicated, this method is called from service providers. Thus, FilesystemServiceProvider calls this method, which looks something like this:

 $this->app['files'] = $this->app->share(function() { return new Filesystem; }); 

It assigns the return value of this share method to the binding in the container. In short, this return value will be a new instance of Filesystem , which is returned in close.

So what does Share do?

The share method is another way of defining a singleton in an IoC container. All of this can be a little intimidating at first. Basically, Laravel itself is an IoC container. All classes are connected as container instances. Sometimes these instances must be the same for each call.

If you look at the link method above on GitHub, you will notice that a static variable is defined inside the closure. He then checks to see if this variable is null, and if it allows closure (this is the closure that our new Filesystem instance returns). Then it just returns the variable.

Now, the next time you use File::get() , you do not need to instantiate the Filesystem class again, as it has already been created and is stored in the static variable $object . That way, it just returns the same object to you.

So! Indeed, you could replace the string $this->app['files'] this, and it will still work.

 $this->app->instance('files', new Filesystem); 

99% of the services actually use the share method, though, since working inside the closure allows you to create objects with more complex dependencies.

Hope this helps.

+11
source

Source: https://habr.com/ru/post/948054/


All Articles