La colonne tutorielle suivante de laravel vous présentera d'où viennent $this->app et app() de Laravel. J'espère que cela sera utile aux amis qui en ont besoin !
Le débogage du point d'arrêt recherche le fichier correspondant, ignore les étapes mineures, décrit uniquement les actions principales, '/' indique le répertoire où se trouve index.php
Adresse : index.php
$app = require_once __DIR__.'/../bootstrap/app.php';
$initialisation de l'application
path/… /bootstrap/app.php
$app = new Illuminate\Foundation\Application( realpath(__DIR__.'/../') );//$app初始化工作
Classe d'initialisation et constructeur $app
class Application extends Container implements ApplicationContract,HttpKernelInterface { //继承Container类,Container类实现应用契约接口与请求接口 public function __construct($basePath = null){ if ($basePath) {//$basePath = '/../' $this->setBasePath($basePath);//路径绑定 } $this->registerBaseBindings();//基础绑定 $this->registerBaseServiceProviders();//注册基础服务提供者 $this->registerCoreContainerAliases();//注册别名 } }
Liaison de chemin
Application的setBasePath(’/…/’)方法调用Container的instance(a b s t r a c t , abstract,abstract,instance)方法赋值Contaienr类instances instances: array:9 [▼ “path” => “\var\www\app” “path.base” => “\var\www” “path.lang” => “\var\www\resources\lang” “path.config” => “\var\www\config” “path.public” => “\var\www\public” “path.storage” => “\var\www\storage” “path.database” => “\var\www\database” “path.resources” => “\var\www\resources” “path.bootstrap” => “\var\www\bootstrap” ]
Liaison de base
Ajouter une liaison aux instances du conteneur
"app" => Application {#2} "Illuminate\Container\Container" => Application {#2} "Illuminate\Foundation\PackageManifest" => PackageManifest {#4 ▼ +files: Filesystem {#5} +basePath: "\var\www" +vendorPath: "\var\www\vendor" +manifestPath: "\var\www\bootstrap\cache\packages.php" +manifest: null }
L'application et le conteneur pointent vers l'instance actuelle
PackageMainfest est l'instance de gestion des packages. Dans la méthode de construction, les fichiers enregistrent l'instance du système de fichiers
Enregistrez le fournisseur de services de base
//class Application protected function registerBaseServiceProviders() { $this->register(new EventServiceProvider($this)); $this->register(new LogServiceProvider($this)); $this->register(new RoutingServiceProvider($this)); }
Les services de base Event, Log et Routing héritent de ServiceProvider et attribuent l'attribut app à l'instance d'application pendant initialisation
//class ServiceProvider public function __construct($app) { $this->app = $app; }
Execute Application La méthode de registre de classe
//Application public function register($provider, $options = [], $force = false) { if (is_string($provider)) { $provider = $this->resolveProvider($provider); } if (method_exists($provider, 'register')) { $provider->register();//赋值bindings } //将服务提供者添加到serviceProviders属性队列 //将服务提供者类名添加到loaderProviders属性 $this->markAsRegistered($provider); return $provider; }
fournit deux méthodes pour l'enregistrement du fournisseur
L'une est une instance directe du nom de classe de chaîne,
L'autre consiste à exécuter $$provider->register() ; méthode à enregistrer,
L'exécution actuelle des deux méthodes,
Regardez la méthode de registre de l'instance EventServiceProvider actuelle
public function register() { $this->app->singleton('events', function ($app) { return (new Dispatcher($app))->setQueueResolver(function () use ($app) { return $app->make(QueueFactoryContract::class); }); }); }
Appelez la méthode singleton de l'instance d'application
//class Continer public function singleton($abstract, $concrete = null) { $this->bind($abstract, $concrete, true); }
Appelez la méthode de liaison, attribuez des liaisons
//class Container public function bind($abstract, $concrete = null, $shared = false) { if (is_null($concrete)) { $concrete = $abstract; } if (! $concrete instanceof Closure) { $concrete = $this->getClosure($abstract, $concrete); } $this->bindings[$abstract] = compact('concrete', 'shared'); }
Retournez à la méthode Register de la classe Application et exécutez markAsRegistered($provider)
//class Application protected function markAsRegistered($provider) { $this->serviceProviders[] = $provider; $this->loadedProviders[get_class($provider)] = true; }
LogServerProvider et RoutingServiceProvider sont similaires
liaison d'alias
//class Application public function registerCoreContainerAliases() { foreach ([ 'app' => [self::class, \Illuminate\Contracts\Container\Container::class, \Illuminate\Contracts\Foundation\Application::class, \Psr\Container\ContainerInterface::class], 'auth' => [\Illuminate\Auth\AuthManager::class, \Illuminate\Contracts\Auth\Factory::class], 'auth.driver' => [\Illuminate\Contracts\Auth\Guard::class], 'blade.compiler' => [\Illuminate\View\Compilers\BladeCompiler::class], 'cache' => [\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class], 'cache.store' => [\Illuminate\Cache\Repository::class, \Illuminate\Contracts\Cache\Repository::class], 'config' => [\Illuminate\Config\Repository::class, \Illuminate\Contracts\Config\Repository::class], 'cookie' => [\Illuminate\Cookie\CookieJar::class, \Illuminate\Contracts\Cookie\Factory::class, \Illuminate\Contracts\Cookie\QueueingFactory::class], 'encrypter' => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class], 'db' => [\Illuminate\Database\DatabaseManager::class], 'db.connection' => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class], 'events' => [\Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class], 'files' => [\Illuminate\Filesystem\Filesystem::class], 'filesystem' => [\Illuminate\Filesystem\FilesystemManager::class, \Illuminate\Contracts\Filesystem\Factory::class], 'filesystem.disk' => [\Illuminate\Contracts\Filesystem\Filesystem::class], 'filesystem.cloud' => [\Illuminate\Contracts\Filesystem\Cloud::class], 'hash' => [\Illuminate\Contracts\Hashing\Hasher::class], 'translator' => [\Illuminate\Translation\Translator::class, \Illuminate\Contracts\Translation\Translator::class], 'log' => [\Illuminate\Log\Writer::class, \Illuminate\Contracts\Logging\Log::class, \Psr\Log\LoggerInterface::class], 'mailer' => [\Illuminate\Mail\Mailer::class, \Illuminate\Contracts\Mail\Mailer::class, \Illuminate\Contracts\Mail\MailQueue::class], 'auth.password' => [\Illuminate\Auth\Passwords\PasswordBrokerManager::class, \Illuminate\Contracts\Auth\PasswordBrokerFactory::class], 'auth.password.broker' => [\Illuminate\Auth\Passwords\PasswordBroker::class, \Illuminate\Contracts\Auth\PasswordBroker::class], 'queue' => [\Illuminate\Queue\QueueManager::class, \Illuminate\Contracts\Queue\Factory::class, \Illuminate\Contracts\Queue\Monitor::class], 'queue.connection' => [\Illuminate\Contracts\Queue\Queue::class], 'queue.failer' => [\Illuminate\Queue\Failed\FailedJobProviderInterface::class], 'redirect' => [\Illuminate\Routing\Redirector::class], 'redis' => [\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class], 'request' => [\Illuminate\Http\Request::class, \Symfony\Component\HttpFoundation\Request::class], 'router' => [\Illuminate\Routing\Router::class, \Illuminate\Contracts\Routing\Registrar::class, \Illuminate\Contracts\Routing\BindingRegistrar::class], 'session' => [\Illuminate\Session\SessionManager::class], 'session.store' => [\Illuminate\Session\Store::class, \Illuminate\Contracts\Session\Session::class], 'url' => [\Illuminate\Routing\UrlGenerator::class, \Illuminate\Contracts\Routing\UrlGenerator::class], 'validator' => [\Illuminate\Validation\Factory::class, \Illuminate\Contracts\Validation\Factory::class], 'view' => [\Illuminate\View\Factory::class, \Illuminate\Contracts\View\Factory::class], ] as $key => $aliases) { foreach ($aliases as $alias) { $this->alias($key, $alias); } } }
attribuez une valeur à l'attribut d'alias
liaison de classe $app core
/…/bootstrap/app.php /** *核心类绑定 */ $app->singleton( Illuminate\Contracts\Http\Kernel::class, App\Http\Kernel::class ); $app->singleton( Illuminate\Contracts\Console\Kernel::class, App\Console\Kernel::class ); $app->singleton( Illuminate\Contracts\Debug\ExceptionHandler::class, App\Exceptions\Handler::class );
appelle la fonction de liaison
//class Container public function singleton($abstract, $concrete = null) { $this->bind($abstract, $concrete, true); }
associe la fonction de rappel par défaut au nom de la classe de chaîne
//class Container public function bind($abstract, $concrete = null, $shared = false) { // If the factory is not a Closure, it means it is just a class name which is // bound into this container to the abstract type and we will just wrap it // up inside its own Closure to give us more convenience when extending. if (! $concrete instanceof Closure) { $concrete = $this->getClosure($abstract, $concrete); } $this->bindings[$abstract] = compact('concrete', 'shared'); }
getClosure renvoie la fonction de rappel par défaut
//class Container protected function getClosure($abstract, $concrete) { return function ($container, $parameters = []) use ($abstract, $concrete) { if ($abstract == $concrete) { return $container->build($concrete); } return $container->make($concrete, $parameters); }; }
Enfin : renvoie $app
/…/bootstrap/app.php return $app;
Recommandations associées : Les cinq derniers didacticiels vidéo Laravel
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!