dmz社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 662|回复: 0

[php框架] laravel5源码讲解整理

[复制链接]
  • TA的每日心情
    奋斗
    5 天前
  • 签到天数: 237 天

    [LV.7]常住居民III

    4425

    主题

    1432

    帖子

    9869

    积分

    会|员

    Rank: 9Rank: 9Rank: 9

    积分
    9869
    发表于 2019-11-5 23:57:50 | 显示全部楼层 |阅读模式

    本站资源全部免费,回复即可查看下载地址!

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    入口文件 index.php
    一个基于Laravel的应用,当WEB服务器接受到来自外部的请求后,会将这个这个请求解析到 应用根目录的 public/index.php 中。
    [PHP] 纯文本查看 复制代码
    <?php
    /**
     * Laravel - A PHP Framework For Web Artisans
     *
     * @package  Laravel
     * @author   Taylor Otwell <[url=mailto:taylorotwell@gmail.com]taylorotwell@gmail.com[/url]>
     */
    
    /*
    |--------------------------------------------------------------------------
    | Register The Auto Loader
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader for
    | our application. We just need to utilize it! We'll simply require it
    | into the script here so that we don't have to worry about manual
    | loading any of our classes later on. It feels nice to relax.
    |
    */
    
    require __DIR__.'/../bootstrap/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Turn On The Lights
    |--------------------------------------------------------------------------
    |
    | We need to illuminate PHP development, so let us turn on the lights.
    | This bootstraps the framework and gets it ready for use, then it
    | will load up this application so that we can run it and send
    | the responses back to the browser and delight our users.
    |
    */
    
    $app = require_once __DIR__.'/../bootstrap/app.php';
    
    /*
    |--------------------------------------------------------------------------
    | Run The Application
    |--------------------------------------------------------------------------
    |
    | Once we have the application, we can handle the incoming request
    | through the kernel, and send the associated response back to
    | the client's browser allowing them to enjoy the creative
    | and wonderful application we have prepared for them.
    |
    */
    
    $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
    
    $response = $kernel->handle(
        $request = Illuminate\Http\Request::capture()
    );
    
    $response->send();
    
    $kernel->terminate($request, $response);
    第二十一行代码
    [PHP] 纯文本查看 复制代码
    require __DIR__.'/../bootstrap/autoload.php';
    为Laravel应用引入了由Composer提供的类加载器,这样Laravel应用便无需再手动加载任 何的类。其加载原理不是此次探究的目标,所以仅仅这样使用就好了。接下的代码,便是重 点。

    Illuminate\Foundation\Application 类
    该类的继承结构如下:
    第三十五行代码
    [PHP] 纯文本查看 复制代码
    $app = require_once __DIR__.'/../bootstrap/app.php';
    它将我的视线引入到了另外一个文件中,去看看到底发生了什么吧。
    Laravel源码解读-app.php (laravel_app.php)
    [PHP] 纯文本查看 复制代码
    <?php
    
    /*
    |--------------------------------------------------------------------------
    | Create The Application
    |--------------------------------------------------------------------------
    |
    | The first thing we will do is create a new Laravel application instance
    | which serves as the "glue" for all the components of Laravel, and is
    | the IoC container for the system binding all of the various parts.
    |
    */
    
    $app = new Illuminate\Foundation\Application(
        realpath(__DIR__.'/../')
    );
    
    /*
    |--------------------------------------------------------------------------
    | Bind Important Interfaces
    |--------------------------------------------------------------------------
    |
    | Next, we need to bind some important interfaces into the container so
    | we will be able to resolve them when needed. The kernels serve the
    | incoming requests to this application from both the web and CLI.
    |
    */
    
    $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
    );
    
    /*
    |--------------------------------------------------------------------------
    | Return The Application
    |--------------------------------------------------------------------------
    |
    | This script returns the application instance. The instance is given to
    | the calling script so we can separate the building of the instances
    | from the actual running of the application and sending responses.
    |
    */
    
    return $app;

    看第十四行,原来$app是一个 Illuminate\Foundation\Application 对象,那么在创 建这个对象的时候又发生了什么呢?
    从它的构造方法看起:
    Illuminate\Foundation\Application 构造方法
    [PHP] 纯文本查看 复制代码
    /**
     * Create a new Illuminate application instance.
     *
     * @param  string|null  $basePath
     * @return void
     */
    public function __construct($basePath = null)
    {
        $this->registerBaseBindings();
    
        $this->registerBaseServiceProviders();
    
        $this->registerCoreContainerAliases();
    
        if ($basePath) {
            $this->setBasePath($basePath);
        }
    }

    顺着函数调用,往下看。在这个构造函数中,首先调用了registerBaseBindings方法。
    Illuminate\Foundation\Application#registerBaseBindings
    [PHP] 纯文本查看 复制代码
    /**
      * Register the basic bindings into the container.
      *
      * @return void
      */
    protected function registerBaseBindings()
    {
        static::setInstance($this);
    
        $this->instance('app', $this);
    
        $this->instance('Illuminate\Container\Container', $this);
    }

    这段代码,是将实例对象注入到容器中。那么,这个容器是什么呢?答案还是要从这段调用 中去寻找。
    static::setInstance($this) 所做的就是将 $this 赋值给自身的 instance 静态变 量。重点看 $this->instance('app', $this)。
    instance 函数的作用是绑定一个已有对象到容器中,这个对象在容器中共享并且可以通 过键获取。
    Illuminate\Container\Container#instance
    [PHP] 纯文本查看 复制代码
    /**
      * Register an existing instance as shared in the container.
      *
      * @param  string  $abstract
      * @param  mixed   $instance
      * @return void
      */
    public function instance($abstract, $instance)
    {
        if (is_array($abstract)) {
            // $abstract 是这样的一个数组 ['actual key' => 'alias']
            list($abstract, $alias) = $this->extractAlias($abstract);
    
            // 实际上的行为是 $this->aliases[$alias] = $abstract;
            $this->alias($abstract, $alias);
        }
    
        unset($this->aliases[$abstract]);
    
        // 检查是否有这个键是否已经注册到容器中
        // $bound 是一个boolean值
        $bound = $this->bound($abstract);
    
        $this->instances[$abstract] = $instance;
    
        if ($bound) {
            $this->rebound($abstract);
        }
    }

    视线重新回到Application类中,接下来调用了这个方法 $this->registerBaseServiceProviders(),
    Illuminate\Foundation\Application#registerBaseServiceProviders
    [PHP] 纯文本查看 复制代码
    /**
      * Register all of the base service providers.
      *
      * @return void
      */
    protected function registerBaseServiceProviders()
    {
        $this->register(new EventServiceProvider($this));
    
        $this->register(new RoutingServiceProvider($this));
    }
    
    /**
      * Register a service provider with the application.
      *
      * @param  \Illuminate\Support\ServiceProvider|string  $provider
      * @param  array  $options
      * @param  bool   $force
      * @return \Illuminate\Support\ServiceProvider
      */
    public function register($provider, $options = [], $force = false)
    {
        if ($registered = $this->getProvider($provider) && !$force) {
            return $registered;
        }
    
        // If the given "provider" is a string, we will resolve it, passing in the
        // application instance automatically for the developer. This is simply
        // a more convenient way of specifying your service provider classes.
        if (is_string($provider)) {
            $provider = $this->resolveProviderClass($provider);
        }
    
        $provider->register();
    
        // Once we have registered the service we will iterate through the options
        // and set each of them on the application so they will be available on
        // the actual loading of the service objects and for developer usage.
        foreach ($options as $key => $value) {
            $this[$key] = $value;
        }
    
        $this->markAsRegistered($provider);
    
        // If the application has already booted, we will call this boot method on
        // the provider class so it has an opportunity to do its boot logic and
        // will be ready for any usage by the developer's application logics.
        if ($this->booted) {
            $this->bootProvider($provider);
        }
    
        return $provider;
    }

    其中,EventServiceProvider和RoutingServiceProvider分别是
    • Illuminate\Events\EventServiceProvider
    • Illuminate\Routing\RoutingServiceProvider
    这些ServiceProvider是 Illuminate\Support\ServiceProvider 的子类,它接受一个 Application 对象作为构造函数参数,存储在实例变量 $app 中。

    注入所有基础 Service Provider
    在 register 方法中,每个ServiceProvider被调用了自身的 register 方法。首先看 看 EventServiceProvider 中的吧。
    Illuminate\Events\EventServiceProvider#register
    [PHP] 纯文本查看 复制代码
    public function register()
    {
        $this->app->singleton('events', function ($app) {
            return (new Dispatcher($app))->setQueueResolver(function () use ($app) {
                return $app->make('Illuminate\Contracts\Queue\Factory');
            });
        });
    }

    上面方法体将一个 Illuminate\Events\Dispatcher 对象以键 events 绑定到了容器 中,它负责实现事件的调度。
    再看看 Illuminate\Routing\RoutingServiceProvider:
    Illuminate\Routing\RoutingServiceProvider#register
    [PHP] 纯文本查看 复制代码
    public function register()
    {
        $this->registerRouter();
    
        $this->registerUrlGenerator();
    
        $this->registerRedirector();
    
        $this->registerPsrRequest();
    
        $this->registerPsrResponse();
    
        $this->registerResponseFactory();
    }

    首页是在Laravel中接触的最多的 route 被注册,它是 Illuminate\Routing\Router 对象
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|小黑屋|本站代理|dmz社区

    GMT+8, 2024-4-28 01:54 , Processed in 0.403327 second(s), 31 queries .

    Powered by Discuz! X3.4 Licensed

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表