تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Introducing EnvironmentStore Demo0:00

We are still talking about the facade magic, but in this video I'm going to show you a magic trick that can really confuse you, even if you are already familiar with Laravel and the facades that ship with it. Let me show you. To demonstrate this magic, I've created a class named EnvironmentStore. It's placed inside the app directory, so the namespace here is App. The class depends on the Laravel application instance, so it requires it in the constructor. It also has a few methods, one to store a value in an array, and one to return the size of the array, and also one to return the name of the environment the application is running within.

Using Dependency Injection0:41

of the array, and also one to return the name of the environment the application is running within. It extracts the name of the environment from the Laravel application instance. Now let's go to our console command and use this method. We will require it as a dependency here, EnvironmentStore, and name the variable store. Then we call the store method on the variable and provide some value. Let's call it again so we have two values, and now we dump the number of items in the store. And let's also dump the application environment name, all pretty straightforward. Let's run the command, and here we go.

Attempting Static Calls1:24

And let's also dump the application environment name, all pretty straightforward. Let's run the command, and here we go. We have two items in store, and the environment name is Local. Now let's do some magic, we'll remove this, no $store variable anymore, and let's replace the reference to the old $store variable to EnvironmentStore, and call the methods statically. PHPStorm can't find that class because there is no class with that name in the global namespace, our EnvironmentStore class is in the app namespace. Anyway let's run the command, and we get an error, class not found, that's expected. So let's go up here, and use Facades, App, EnvironmentStore, PHPStorm can't find that class still, but it doesn't exist, so that makes sense.

Switching to Real-Time Facade2:17

So let's go up here, and use Facades, App, EnvironmentStore, phpStorm can't find that class still, but it doesn't exist, so that makes sense. Let's run the command, and it works, how? This class doesn't exist, we just verified. If we go back to the code, and we find that phpStorm can find that class now, how? That's this magic, let me show you. The Laravel framework ships with a class named AliasLoader. Let's check it out, and that class had a method called prependToLoaderStack. Inside this method, Laravel calls PHP's SPL autoload register function. This function registers autoloading logic that helps PHP find the class file by its

Tracing AliasLoader Autoloading3:02

Inside this method, Laravel calls PHP's SPL autoload register function. This function registers autoloading logic that helps PHP find the class file by its namespace. Laravel provides a load method to this PHP function. Let's go check it out. And here we can see that inside the method, Laravel checks if the class you are trying to load starts with the value of a facade namespace static property, and if so, it calls a loadFacade method. Let's check this method. And in here, Laravel requires the file which path is returned by this EnsureFacadeExists

Let's check this method. And in here, Laravel requires the file which path is returned by this EnsureFacadeExists method. Let's check it out. And here, Laravel checks if there is a file with a certain name inside the Storage/Framework/cache directory, and if so, it returns its path. If not, it creates it and returns the path. So Laravel created a facade for us in real time and required that file in the autoloader. Now if we go to the command, check the file phpStorm found. And here we go, that's the file Laravel created for us in real time, a proper facade with

Explaining Real-Time Facade Behavior4:12

Now if we go to the command, check the file phpStorm found. And here we go, that's the file Laravel created for us in real time, a proper facade with the getFacadeAccessor method and everything. So when we call static methods on the EnvironmentStore class, Laravel treats this call as a facade call and proxies the calls to the facade accessor, which is the EnvironmentStore class in our case. This is some black magic, I can assure you. But now you know. If you see a class that has a namespace that starts with facades, it means this class is treated as a real-time facade by the framework.

If you see a class that has a namespace that starts with Facades, it means this class is treated as a real-time facade by the framework. It resolves a service from the dependency container and makes it available to you. Should you use real-time facades or not, that's your call. I will see you in the next video.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟