从symfony框架到一个完整的项目需要几步-二-死磕composer

1次阅读

共计 10063 个字符,预计需要花费 26 分钟才能阅读完成。

前言

对于 php 的框架,无论是 yiisymfony 或者是 laravel,大家都在工作中有涉猎。对于在框架中的存放着资源包vendor 文件夹,入口文件(index.php 或者 app.php),大家也都与他们每天碰面。但是你真的熟悉这些文件 / 文件夹吗?一个完整的项目是如何从一个纯净框架发展而来?各个部分又在框架这个大厦中又起到了怎么样的作用?

在上一章我们说到了依赖注入,也不知道大伙都理解了没有?不理解也没问题,今天的这一章和上一章完全没有关系。

二、Composer

现在我们来到了下一个话题来,说说 composer 这个工具。大家对于这个工具都不陌生,用它安装插件真的是非常方便。但是他的原理大家是否清楚?本来就是一个普普通通的类,怎么就被加载进来了呢?composer说了,我们钦定了,就由 autoload 进行操作。

2.1 __autoload

这是一个特别重要的知识点。我们经常会在框架的入口文件中看到它 (__autoloadspl_autoload_register。当然现在你只能看到spl_auto_register)。但是真的被问及这两个方法的作用和方法的时候,大部分人还是会一脸懵逼。

这两个函数到底是什么?自动加载有又什么方便之处?

includerequire 是 PHP 中引入文件的两个基本方法。在小规模开发中直接使用 includerequire但在大型项目中会造成大量的 include 和 require 堆积。 (你想想,一个文件里面我写几百个 include 你累不累?)

这样的代码既不优雅,执行效率也很低,而且维护起来也相当困难。

为了解决这个问题,部分框架会给出一个引入文件的配置清单,在对象初始化的时候把需要的文件引入。但这只是让代码变得更简洁了一些,引入的效果仍然是差强人意。PHP5 之后,随着 PHP 面向对象支持的完善,__autoload 函数才真正使得自动加载成为可能。

在这里我补充和当前章节无关的两个知识点:

  • include 和 require 功能是一样的,它们的不同在于 include 出错时只会产生警告,而 require 会抛出错误终止脚本。
  • include_once 和 include 唯一的区别在于 include_once 会检查文件是否已经引入,如果是则不会重复引入。

实现自动加载最简单的方式就是使用 __autoload 魔术方法。当你引用不存在的类时,__autoload就会被调用,并且你的类名会被作为参数传送过去。至于函数具体的逻辑,这需要用户自己去实现。利用该性质,创建一个自动加载的机制。
首先创建一个 autoload.php 来做一个简单的测试:

// 类未定义时,系统自动调用
function __autoload($class)
{
    /* 具体处理逻辑 */
    echo $class;// 简单的输出未定义的类名
}

new HelloWorld();

/**
 * 输出 HelloWorld 与报错信息
 * Fatal error: Class 'HelloWorld' not found
 */


通过这个简单的例子可以发现,在类的实例化过程中,系统所做的工作大致是这样的:/* 模拟系统实例化过程 */
function instance($class)
{
    // 如果类存在则返回其实例
    if (class_exists($class, false)) {return new $class();
    }
    // 查看 autoload 函数是否被用户定义
    if (function_exists('__autoload')) {__autoload($class); // 最后一次引入的机会
    }
    // 再次检查类是否存在
    if (class_exists($class, false)) {return new $class();
    } else { // 系统:我实在没辙了
        throw new Exception('Class Not Found');
    }
}

明白了 __autoload 函数的工作原理之后,那就让我们来用它去实现自动加载。

首先创建一个类文件(建议文件名与类名一致),代码如下:

class [ClassName] 
{
    // 对象实例化时输出当前类名
    function __construct()
    {echo '<h1>' . __CLASS__ . '</h1>';}
}

(我这里创建了一个 HelloWorld 类用作演示)接下来我们就要定义 __autoload 的具体逻辑,使它能够实现自动加载:

function __autoload($class)
{
    // 根据类名确定文件名
    $file = $class . '.php';

    if (file_exists($file)) {include $file; // 引入 PHP 文件}
}

new HelloWorld();

/**
 * 输出 <h1>HelloWorld</h1>
 */

看上去很美好对吧?利用这个 __autoload 就能写一个自动加载类的机制。但是你有没有试过在一个文件里面写两个__autoload? 不用想,结果报错。在一个大型框架中,你敢保障你只有一个__autoload?这样不就很麻烦吗?

不用着急,spl_autoload_register()该出场了。不过再解释之前,我们得说另外一个重要的概念 – 命名空间。

2.3 命名空间

其实命名空间并不是什么新生事物,很多语言(例如 C ++)早都支持这个特性了。只不过 PHP 起步比较晚,直到 PHP 5.3 之后才支持。命名空间简而言之就是一种标识,它的主要目的是解决命名冲突的问题。
就像在日常生活中,有很多姓名相同的人,如何区分这些人呢?那就需要加上一些额外的标识。把工作单位当成标识似乎不错,这样就不用担心“撞名”的尴尬了。

这里我们来做一个小任务,去介绍百度的 CEO 李彦宏:

namespace 百度;

class 李彦宏
{function __construct()
    {echo '百度创始人';}
}

这就是李彦宏的基本资料了,namespace 是他的单位标识,class 是他的姓名。命名空间通过关键字 namespace 来声明。如果一个文件中包含命名空间,它必须在其它所有代码之前声明命名空间。

new 百度 \ 李彦宏(); // 限定类名
new \ 百度 \ 李彦宏(); // 完全限定类名

在一般情况下,无论是向别人介绍 "百度 李彦宏" 还是 "百度公司 李彦宏",他们都能够明白。在当前命名空间没有声明的情况下,限定类名和完全限定类名是等价的。因为如果不指定空间,则默认为全局()。

namespace 谷歌;

new 百度 \ 李彦宏(); // 谷歌 \ 百度 \ 李彦宏(实际结果)new \ 百度 \ 李彦宏(); // 百度 \ 李彦宏(实际结果)

如果你在谷歌公司向他们的员工介绍李彦宏,一定要指明是 “ 百度公司的李彦宏 ”。否则他会认为百度是谷歌的一个部门,而李彦宏只是其中的一位员工而已。这个例子展示了在命名空间下,使用限定类名和完全限定类名的区别。(完全限定类名 = 当前命名空间 + 限定类名)

/* 导入命名空间 */
use 百度 \ 李彦宏;
new 李彦宏(); // 百度 \ 李彦宏(实际结果)/* 设置别名 */
use 百度 \ 李彦宏 AS CEO;
new CEO(); // 百度 \ 李彦宏(实际结果)/* 任何情况 */
new \ 百度 \ 李彦宏();// 百度 \ 李彦宏(实际结果)

第一种情况是别人已经认识李彦宏了,你只需要直接说名字,他就能知道你指的是谁。第二种情况是李彦宏就是他们的 CEO,你直接说 CEO,他可以立刻反应过来。使用命名空间只是让类名有了前缀,不容易发生冲突,系统仍然不会进行自动导入。
如果不引入文件,系统会在抛出 “Class Not Found” 错误之前触发 __autoload 函数,并将限定类名传入作为参数。
所以上面的例子都是基于你已经将相关文件手动引入的情况下实现的,否则系统会抛出 ” Class ‘ 百度李彦宏 ’ not found”。

2.4 spl_autoload_register

接下来让我们要在含有命名空间的情况下去实现自动加载。这里我们使用 spl_autoload_register() 函数来实现,这需要你的 PHP 版本号大于 5.12。
spl_autoload_register函数的功能就是把传入的函数(参数可以为回调函数或函数名称形式)注册到 SPL __autoload 函数队列中,并移除系统默认的 __autoload() 函数。一旦调用 spl_autoload_register() 函数,当调用未定义类时,系统就会按顺序调用注册到 spl_autoload_register() 函数的所有函数,而不是自动调用 __autoload() 函数。

现在,我们来创建一个 Linux 类,它使用 os 作为它的命名空间(建议文件名与类名保持一致):

namespace os; // 命名空间

class Linux // 类名
{function __construct()
    {echo '<h1>' . __CLASS__ . '</h1>';}
}

接着,在同一个目录下新建一个 PHP 文件,使用 spl_autoload_register 以函数回调的方式实现自动加载:

spl_autoload_register(function ($class) { // class = os\Linux

    /* 限定类名路径映射 */
    $class_map = array(
        // 限定类名 => 文件路径
        'os\\Linux' => './Linux.php',
    );

    /* 根据类名确定文件名 */
    $file = $class_map[$class];

    /* 引入相关文件 */
    if (file_exists($file)) {include $file;}
});

new \os\Linux();

这里我们使用了一个数组去保存类名与文件路径的关系,这样当类名传入时,自动加载器就知道该引入哪个文件去加载这个类了。

但是一旦文件多起来的话,映射数组会变得很长,这样的话维护起来会相当麻烦。如果命名能遵守统一的约定,就可以让自动加载器自动解析判断类文件所在的路径。接下来要介绍的PSR-4 就是一种被广泛采用的约定方式。

2.4 PSR- 4 规范

PSR-4 是关于由文件路径自动载入对应类的相关规范,规范规定了一个完全限定类名需要具有以下结构:

\< 顶级命名空间 >(\< 子命名空间 >)*\< 类名 >

如果继续拿上面的例子打比方的话,顶级命名空间相当于公司,子命名空间相当于职位,类名相当于人名。那么李彦宏标准的称呼为 “ 百度公司 CEO 李彦宏 ”。

PSR-4 规范中必须要有一个顶级命名空间,它的意义在于表示某一个特殊的目录(文件基目录)。子命名空间代表的是类文件相对于文件基目录的这一段路径(相对路径),类名则与文件名保持一致(注意大小写的区别)。

举个例子:在全限定类名 \app\view\news\Index 中,如果 app 代表 C:\Baidu,那么这个类的路径则是 C:\Baidu\view\news\Index.php

我们就以解析 \app\view\news\Index 为例,编写一个简单的 Demo

$class = 'app\view\news\Index';

/* 顶级命名空间路径映射 */
$vendor_map = array('app' => 'C:\Baidu',);

/* 解析类名为文件路径 */
$vendor = substr($class, 0, strpos($class, '\\')); // 取出顶级命名空间[app]
$vendor_dir = $vendor_map[$vendor]; // 文件基目录[C:\Baidu]
$rel_path = dirname(substr($class, strlen($vendor))); // 相对路径[/view/news]
$file_name = basename($class) . '.php'; // 文件名[Index.php]

/* 输出文件所在路径 */
echo $vendor_dir . $rel_path . DIRECTORY_SEPARATOR . $file_name;

通过这个 Demo 可以看出限定类名转换为路径的过程。那么现在就让我们用规范的面向对象方式去实现自动加载器吧。

首先我们创建一个文件 Index.php,它处于 \app\mvc\view\home 目录中:

namespace app\mvc\view\home;

class Index
{function __construct()
    {echo '<h1> Welcome To Home </h1>';}
}

接着我们在创建一个加载类(不需要命名空间),它处于 目录中:

class Loader
{
    /* 路径映射 */
    public static $vendorMap = array('app' => __DIR__ . DIRECTORY_SEPARATOR . 'app',);

    /**
     * 自动加载器
     */
    public static function autoload($class)
    {$file = self::findFile($class);
        if (file_exists($file)) {self::includeFile($file);
        }
    }

    /**
     * 解析文件路径
     */
    private static function findFile($class)
    {$vendor = substr($class, 0, strpos($class, '\\')); // 顶级命名空间
        $vendorDir = self::$vendorMap[$vendor]; // 文件基目录
        $filePath = substr($class, strlen($vendor)) . '.php'; // 文件相对路径
        return strtr($vendorDir . $filePath, '\\', DIRECTORY_SEPARATOR); // 文件标准路径
    }

    /**
     * 引入文件
     */
    private static function includeFile($file)
    {if (is_file($file)) {include $file;}
    }
}

最后,将 Loader 类中的 autoload 注册到 spl_autoload_register 函数中:

include 'Loader.php'; // 引入加载器
spl_autoload_register('Loader::autoload'); // 注册自动加载

new \app\mvc\view\home\Index(); // 实例化未引用的类

/**
 * 输出: <h1> Welcome To Home </h1>
 */

2.4 composer

说了这么多,终于该 composer 登场啦。关于安装之类的我在这里就不在赘述了。下面来看看 vendor/composer 的文件详情

vendor
----autoload_classmap.php
----autoload_files.php
----autoload_namespace.php
----autoload_psr4.php
----autoload_real.php
----autoload_static.php
----ClassLoader.php
----install.json 
autoload.php

那么我先看看vendor/autoload.php

<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326::getLoader();


其执行了一个自动生成的类 ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326 中的 getLoader 方法。我们跟进到 autoload_real.php 上。public static function getLoader()
    {if (null !== self::$loader) {return autoload_real.phpself::$loader;}

        spl_autoload_register(array('ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326', 'loadClassLoader'), true, true);
        self::$loader = $loader = new \Composer\Autoload\ClassLoader();
        spl_autoload_unregister(array('ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326', 'loadClassLoader'));

        $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);
        }

        $loader->register(true);

        $includeFiles = require __DIR__ . '/autoload_files.php';
        foreach ($includeFiles as $file) {composerRequireff1d77c91141523097b07ee2acc23326($file);
        }

        return $loader;
    }

可以明显看到,他将 autoload_namespaces.phpautoload_psr4.phpautoload_classmap.phpautoload_files.php 等几个配置文件包含了进来,并进行了相关处理(setPsr4),最后注册(register)。
那么我们跟进 register 方法:

    public function register($prepend = false)
    {spl_autoload_register(array($this, 'loadClass'), true, $prepend);
    }

这函数就一行,但简单明了,直接调用 php 自带的 spl_autoload_register 函数,注册处理 __autoload 的方法,也就是 loadClass 方法。再跟进 loadClass 方法:

    public function loadClass($class)
    {if ($file = $this->findFile($class)) {includeFile($file);

            return true;
        }
    }

从函数名字就可以大概知道流程:如果存在 $class 对应的这个 $file,则 include 进来。
那么进 findFile 方法里看看吧:

    public function findFile($class)
    {
        // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
        if ('\\' == $class[0]) {$class = substr($class, 1);
        }

        // class map lookup
        if (isset($this->classMap[$class])) {return $this->classMap[$class];
        }
        if ($this->classMapAuthoritative) {return false;}

        $file = $this->findFileWithExtension($class, '.php');

        // Search for Hack files if we are running on HHVM
        if ($file === null && defined('HHVM_VERSION')) {$file = $this->findFileWithExtension($class, '.hh');
        }

        if ($file === null) {
            // Remember that this class does not exist.
            return $this->classMap[$class] = false;
        }

        return $file;
    }

通过类名找文件,最终锁定在 findFileWithExtension 方法中。
还是跟进 findFileWithExtension 方法:

    private function findFileWithExtension($class, $ext)
    {
        // PSR-4 lookup
        $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;

        $first = $class[0];
        if (isset($this->prefixLengthsPsr4[$first])) {foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {if (0 === strpos($class, $prefix)) {foreach ($this->prefixDirsPsr4[$prefix] as $dir) {if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {return $file;}
                    }
                }
            }
        }

        // PSR-4 fallback dirs
        foreach ($this->fallbackDirsPsr4 as $dir) {if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {return $file;}
        }

        // PSR-0 lookup
        if (false !== $pos = strrpos($class, '\\')) {
            // namespaced class name
            $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
                . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
        } else {
            // PEAR-like class name
            $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
        }

        if (isset($this->prefixesPsr0[$first])) {foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {if (0 === strpos($class, $prefix)) {foreach ($dirs as $dir) {if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {return $file;}
                    }
                }
            }
        }

        // PSR-0 fallback dirs
        foreach ($this->fallbackDirsPsr0 as $dir) {if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {return $file;}
        }

        // PSR-0 include paths.
        if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {return $file;}
    }

最终实现将 命名空间 \ 类 这样的类名,给转换成 目录名 / 类名.php这样的路径,并返回完整路径。

我发现 composerautoloadphp 自带的 spl_autoload,在包含文件时有一点小区别。那就是,spl_autoload 会查找 .inc 类型的文件名,但 composer 不会。

另外也可以发现,虽然配置文件的名字是 autoload_psr4.php,但实际上psr0 格式的自动加载也是支持的。二者最大的不同就是 psr0 中用”_”来代替目录间的””。

以上说了这么多,也该总结一下了。从 __autoloadspl_autoload_register再到 composerpsr4方法。php 官方和社区设计了这么多都是为了什么?它们就是为了解决 include 文件不方便的问题。说一千道一万,原来一个个的 include 不方便,我现在使用 spl_autoload_register 直接自动 include 了。但是我们不能瞎写,还要有规则,于是就有了psr4

正文完
 0