(二) Laravel学习笔记之执行流程//待完善V1.0

来源:互联网 发布:淘宝卖家假货退货 编辑:程序博客网 时间:2024/06/05 07:26

1、Composer自动加载第三方类库
/public/index.php:

/*|--------------------------------------------------------------------------| 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';

然后跳转到(/bootstrap/autoload.php):

/*|----------------------------------------------------------------------| Register The Composer Auto Loader|----------------------------------------------------------------------| Composer provides a convenient, automatically generated class loader| for our application. We just need to utilize it! We'll require it| into the script here so that we do not have to worry about the| loading of any our classes "manually". Feels great to relax.*/require __DIR__.'/../vendor/autoload.php';

继续跳转到(/vendor/autoload.php):

// autoload.php @generated by Composerrequire_once __DIR__ . '/composer' . '/autoload_real.php';return ComposerAutoloaderInitb2c8fef7d9fb7d1be1655640262b4aa3::getLoader();

关注getLoader()方法:

public static function loadClassLoader($class){    if ('Composer\Autoload\ClassLoader' === $class) {        require __DIR__ . '/ClassLoader.php';    }}public static function getLoader(){    if (null !== self::$loader) {        return self::$loader;    }  //spl_autoload_register()是注册一个function作为__autoloader()的implementation,可见PHP官方文档。注册本类的//loadClassLoader()方法,在该方法中判断$class是否等于\Composer\Autoload\ClassLoader,为下一语句准备    spl_autoload_register(array('ComposerAutoloaderInitb2c8fef7d9fb7d1be1655640262b4aa3','loadClassLoader'), true, true);    //self::$loader已经得到了类\Composer\Autoload\ClassLoader的实例,卸载loadClassLoader()吧    self::$loader = $loader = new \Composer\Autoload\ClassLoader();//Composer的类加载器,为程序自动加载类    spl_autoload_unregister(array('ComposerAutoloaderInitb2c8fef7d9fb7d1be1655640262b4aa3', 'loadClassLoader'));    //在加载器$loader中设置一些第三方库的源代码路径        $map = require __DIR__ . '/autoload_namespaces.php';        foreach ($map as $namespace => $path) {            $loader->set($namespace, $path);        }        $map = require __DIR__ . '/autoload_psr4.php';        foreach ($map as $namespace => $path) {            $loader->setPsr4($namespace, $path);        }    //这里也是在加载第三方东西,特别多的第三方东西        $classMap = require __DIR__ . '/autoload_classmap.php';        if ($classMap) {            $loader->addClassMap($classMap);        }    //注册加载器作为autoloader的实例        $loader->register(true);    //还需要包含一些辅助文件,如autoload_files.php中有:    /*    return array(    $vendorDir . '/hamcrest/hamcrest-php/hamcrest/Hamcrest.php',    $vendorDir . '/symfony/var-dumper/Resources/functions/dump.php',    $vendorDir . '/nikic/php-parser/lib/bootstrap.php',    $vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php',    $vendorDir . '/psy/psysh/src/Psy/functions.php',    $vendorDir . '/danielstjules/stringy/src/Create.php',    $vendorDir . '/illuminate/html/helpers.php',    $vendorDir . '/laravel/framework/src/Illuminate/Foundation/helpers.php',    $vendorDir . '/laravel/framework/src/Illuminate/Support/helpers.php',    $vendorDir . '/dingo/api/src/helpers.php',);*/        $includeFiles = require __DIR__ . '/autoload_files.php';        foreach ($includeFiles as $file) {            composerRequireb2c8fef7d9fb7d1be1655640262b4aa3($file);        }//var_dump($loader);die();        return $loader;    }

打印$loader,发现是一坨第三方框架的源码和库,有点类似于系统的初始化似的。先把第三方的类加载进来后,然后就可以在程序中new一个对象了,截图只是截取了部分:
这里写图片描述
重新回到/public/index.php后,第二行代码:

$app = require_once __DIR__.'/../bootstrap/app.php';

在/bootstrap/app.php中第一行就开始new一个对象了,这样就可以new了:

$app = new Illuminate\Foundation\Application(    realpath(__DIR__.'/../'));

2、Container:装Service Provider的容器
////////////////插曲:PHP的反射/////////////////
Reflection:PHP 5 具有完整的反射 API,添加了对类、接口、函数、方法和扩展进行反向工程的能力。 此外,反射 API 提供了方法来取出函数、类和方法中的文档注释。
“反射”类似于”镜像”,把一个类、函数、成员方法、成员变量、对象、属性,”镜像”给一个”反射对象”如$ref,该”反射对象”可以利用反射类提供的”检查工具”(就是一堆类方法),依次对类、函数、成员方法、成员变量、对象、属性,做相对应的”检查”,比如is_Abstract()、is_Final()等等。
反射的设计应该是为了对”类、函数、成员方法、成员变量、对象、属性”做”探针检查”吧,并根据检查结果来做些别的事情。
////////////////PHP的反射END//////////////////

/bootstrap/app.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__.'/../'));

new了一个装载Laravel components的的Container,该Container装了各个部分的system binding,不相信就看Application类:

class Application extends Container implements ApplicationContract, HttpKernelInterface{//Application继承了Container容器    /**     * Create a new Illuminate application instance.     *构造一个实例     * @param  string|null  $basePath     * @return void     */public function __construct($basePath = null){//$basePath就是上面的realpath(__DIR__.'/../'),等于laravel文件的根目录,如MAC版的/Application/MAMP/htdocs/laravel/        $this->registerBaseBindings();//把基本的绑定注册到Application容器内        $this->registerBaseServiceProviders();//把基本的Service Provider注册到Application容器内//把核心的Container容器也注册到Application容器内,Application是最大容器,是整个程序容器。核心容器一坨,见下面代码。        $this->registerCoreContainerAliases();        if ($basePath) {            $this->setBasePath($basePath);        }    } /**     * Register the basic bindings into the container.     *     * @return void     *///基本绑定包括app和Container实例的绑定    protected function registerBaseBindings()    {        static::setInstance($this);        $this->instance('app', $this);        $this->instance('Illuminate\Container\Container', $this);    }  /**     * Register all of the base service providers.     *     * @return void     *///基本的Service Provider包括Event和Route    protected function registerBaseServiceProviders()    {        $this->register(new EventServiceProvider($this));        $this->register(new RoutingServiceProvider($this));    }/**     * Register the core class aliases in the container.     *     * @return void     *///把容器注册进来,当然,加载的是它的别名    public function registerCoreContainerAliases(){    $aliases = ['app'=> ['Illuminate\Foundation\Application', 'Illuminate\Contracts\Container\Container','Illuminate\Contracts\Foundation\Application'],'auth'=> 'Illuminate\Auth\AuthManager','auth.driver'=> ['Illuminate\Auth\Guard', 'Illuminate\Contracts\Auth\Guard'],'auth.password.tokens' => 'Illuminate\Auth\Passwords\TokenRepositoryInterface','blade.compiler'=> 'Illuminate\View\Compilers\BladeCompiler','cache'=> ['Illuminate\Cache\CacheManager', 'Illuminate\Contracts\Cache\Factory'],'cache.store'=> ['Illuminate\Cache\Repository', 'Illuminate\Contracts\Cache\Repository'],'config'=> ['Illuminate\Config\Repository', 'Illuminate\Contracts\Config\Repository'],'cookie'=> ['Illuminate\Cookie\CookieJar', 'Illuminate\Contracts\Cookie\Factory','Illuminate\Contracts\Cookie\QueueingFactory'],'encrypter'=> ['Illuminate\Encryption\Encrypter', 'Illuminate\Contracts\Encryption\Encrypter'],'db'=> 'Illuminate\Database\DatabaseManager','events'=> ['Illuminate\Events\Dispatcher', 'Illuminate\Contracts\Events\Dispatcher'],'files'=> 'Illuminate\Filesystem\Filesystem','filesystem'=>['Illuminate\Filesystem\FilesystemManager', 'Illuminate\Contracts\Filesystem\Factory'],'filesystem.disk'=> 'Illuminate\Contracts\Filesystem\Filesystem','filesystem.cloud'=> 'Illuminate\Contracts\Filesystem\Cloud','hash'=> 'Illuminate\Contracts\Hashing\Hasher','translator'=>['Illuminate\Translation\Translator','Symfony\Component\Translation\TranslatorInterface'],'log'=> ['Illuminate\Log\Writer', 'Illuminate\Contracts\Logging\Log', 'Psr\Log\LoggerInterface'],'mailer'=> ['Illuminate\Mail\Mailer', 'Illuminate\Contracts\Mail\Mailer', 'Illuminate\Contracts\Mail\MailQueue'],'auth.password'=> ['Illuminate\Auth\Passwords\PasswordBroker', 'Illuminate\Contracts\Auth\PasswordBroker'],'queue'=> ['Illuminate\Queue\QueueManager', 'Illuminate\Contracts\Queue\Factory', 'Illuminate\Contracts\Queue\Monitor'],'queue.connection'=> 'Illuminate\Contracts\Queue\Queue','redirect'=> 'Illuminate\Routing\Redirector','redis'=> ['Illuminate\Redis\Database', 'Illuminate\Contracts\Redis\Database'],'request'=> 'Illuminate\Http\Request','router'=> ['Illuminate\Routing\Router', 'Illuminate\Contracts\Routing\Registrar'],'session'=> 'Illuminate\Session\SessionManager','session.store'=> ['Illuminate\Session\Store', 'Symfony\Component\HttpFoundation\Session\SessionInterface'],'url'=> ['Illuminate\Routing\UrlGenerator', 'Illuminate\Contracts\Routing\UrlGenerator'],'validator'=> ['Illuminate\Validation\Factory', 'Illuminate\Contracts\Validation\Factory'],'view'=> ['Illuminate\View\Factory', 'Illuminate\Contracts\View\Factory'],        ];        foreach ($aliases as $key => $aliases) {            foreach ((array) $aliases as $alias) {                $this->alias($key, $alias);            }        }}}

3、

0 0
原创粉丝点击