I need to deeply understand laravel. I have to explain everything to my dev team since we are starting to use laravel.
Please correct this if it is wrong: When laravel start, due to increasing performance, it split services provider to 'eager' and 'deferred', then it register all 'eager' provider, but not 'deferred'.
My question: Each time we use deferred services, e.g:
$validator = Validator::make(
//..
);
How does laravel load and register that class/services ? I just find this probably related line 70 on Illuminate\Foundation\ProviderRepository
Class
$app->setDeferredServices($manifest['deferred']);
But then I stuck. Sorry for bad English, hope you all understands. Thanks.
I wanted to know the answer to this as well, but I decided to look for myself instead.
If you go to
app/storage/meta
there is aservices.json
that lists theeager
anddeferred
classes. This is so we don't load all classes for every request as we may never need theForm
class if we are building an API.The
ProviderRepository
class scans all the providers listed inapp/config/app.php
and instantiates the providers for each of them (These are not the actual libraries).These providers eg
CacheServiceProvider
inIlluminate/Cache
have a variable calleddefer
that determines whether the library will be eagerly loaded of deferred until needed.Here is a quote from
ProviderRepository
For the
CacheServiceProvider
this is set totrue
.So lets say
Validator::make()
is called here is what I have gathered; EachFacade
such as theValidator
facade extends the baseFacade
class inIlluminate\Support\Facades\Facade
.This has a magic method
__callStatic($method, $args)
which is quite self explanatory, the main point of this method isThe facade accessor in this case returns a string of the actual class that performs the validation (
Illuminate\Validation\Validator
):The main method
resolveFacadeInstance
checks if the returned accessor is an object. If it is it simply returns that back as it is probably a custom Laravel package.The second part of this method checks if it has already been resolved, if it has then return that. The final part:
Calls the
Illuminate\Foundation\Application
(which extends theIlluminate\Container\Container
class) via the array access interface located in the previously mentioned parent class.So
static::$app[$name]
calls:This is located in the
Container
classWhich leads us to the
make()
method in theApplication
class:I'll let you delve into the
parent::make()
method as this would start getting very lengthy, but as it stands this is where it loads the deferred provider and instantiates it via theloadDeferredProvider()
The comments in the code block should explain the rest of what is happening.
From here, the last part of
__callStatic($method, $args)
fires the actual method calledValidator::make()
.So the facade calls
Illuminate\Validation\Validator
's non-static methodvalidate()
.I'm pretty sure this is accurate for 4.0 but please correct me if there any mistakes.