paulyg/autoloader

简单轻量级的PSR-0和PSR-4类自动加载器

v1.0.0 2014-01-01 21:35 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:22:30 UTC


README

##概述

Paulyg\Autoloader 是您能想到的最小最简单的 PHP 类自动加载器。它遵循已建立和流行的 PSR-0 标准 和最近批准的 PSR-4 标准 加载类。它使用 PHP 的内部 SPL 栈来跟踪前缀目录映射,而不是在用户空间代码中执行。

##动机

PHP 已经有很多自动加载库和组件。那么我为什么要写自己的呢?我最初的动机是消除由 PSR-0 目录结构产生的许多空目录。如果您使用 Composer 来管理库,这会变得更糟。这里有一个例子,假设我们使用 Zend\Feed,它将引入 Zend\StdlibZend\Escaper

/path/to/project/vendor/zendframework/zend-feed/Zend/Feed/
/path/to/project/vendor/zendframework/zend-stdlib/Zend/Stdlib/
/path/to/project/vendor/zendframework/zend-escaper/Zend/Escaper/
                              |             |       |     |
                              |             |       |     `-- Finally code here!
                              |             |       |
                              |             |       `-- No code in this dir
                              |             |
                              |             `-- No code in this dir
                              |
                              `-- No code in this dir

我一直在自己的代码和库代码中穿越这些空目录。Composer 强制执行前两个目录,例如 zendframework/zend-blah,作为其自己的 命名空间 方案的组成部分。但至少我可以缩小其他两个。我将此称为 别名,而这个自动加载器的第一个版本有一个名为 addAlias() 的方法。后来我看到了来自 PHP-FIGPSR-4 建议,这基本上是同样的想法。因此,该方法被重命名为 addPsr4(),尽管 PSR-4 尚未批准。《免责声明:我与 PHP-FIG 没有联系。

PSR-4 自动加载标准允许您以如下方式组织库代码。

/path/to/project/vendor/zf/zend-feed/Reader.php <- Class: Zend\Feed\Reader
                                     Writer.php <- Class: Zend\Feed\Writer
                                     AbstractEntry.php <- Class: Zend\Feed\AbstractEntry

并如下自动加载它们。

<?php
use Paulyg\Autoloader;
Autoloader::addPsr4('Zend\Feed', '/path/to/project/zf/zend-feed');
$reader = new \Zend\Feed\Reader();

次要动机是我看到的一些有关 Github 上 PHPUnit 问题的帖子,Sebastian Bergmann 不希望在 PHPUnit 中包含 PSR-0 自动加载器。他 主要的问题 是,所有当前的 PSR-0 自动加载器都在用户空间 PHP 数组中维护命名空间/类前缀到目录的映射,而不是在由更有效的编译 C 代码管理的 spl_autoloader 栈中。我相信 Sebastian 实际上更喜欢类映射,但我接受了将 spl_autoloader 栈用于此库的建议。

安装

自动加载器只是一个小的类文件,我不期望它会有很大的变化,所以我建议您直接从 Github 使用 raw 功能下载它。

$ wget https://github.com/paulyg/autoloader/raw/master/src/Paulyg/Autoloader.php

或者,您可以使用 Composer。将以下内容添加到您项目中的 composer.json 文件中

{
    "require": {
        "paulyg/autoloader": "dev-master"
    }
}

或者克隆 Git 仓库。

$ git clone https://github.com/paulyg/autoloader

API

3 个简单的方法。所有方法都可以静态调用或从实例调用。

  1. addPsr0($prefix, $dir, $prepend = false)
  2. addPsr4($prefix, $dir, $prepend = false)
  3. remove($prefix, $dir)

示例

PSR-0

基本加载具有命名空间和 PSR-0 目录结构的类。

<?php
use Paulyg\Autoloader;
Autoloader::addPsr0('Symfony', '/path/to/project/vendor/symfony/src');
$collection = new Symfony\Component\Routing\RouteCollection();

/path/to/project/vendor/symfony/src/Symfony/Component/Routing/RouteCollection.php 加载

去除首部(根)命名空间分隔符。这两个是等效的。

<?php
use Paulyg\Autoloader;
Autoloader::addPsr0('Silex', '/path/to/project/vendor/silex/silex/src');
Autoloader::addPsr0('\Silex', '/path/to/project/vendor/silex/silex/src');

尊重尾部命名空间分隔符。这对于您有命名空间代码和非命名空间代码且共享相同前缀的情况非常有用。此示例还说明了使用实例方法调用与静态方法调用的区别。

<?php
$autoloader = new Paulyg\Autoloader();
$autoloader->addPsr0('Zend\\', '/path/to/project/vendor/zf2/library');
$reader = new Zend\Feed\Reader(); // <- will load class
$writer = new Zend_Feed_Writer(); // <- will not load class

加载PEAR/Zend/Horde风格的类。注意,即使许多其他库为命名空间和非命名空间类提供了单独的方法,它们仍然符合PSR-0规范。

<?php
use Paulyg\Autoloader;
Autoloader::addPsr0('Zend', '/path/to/project/vendor/zf1/library');
$writer = new Zend_Feed_Writer(); // <- Now works

也支持尾随下划线。

<?php
use Paulyg\Autoloader;
Autoloader::addPsr0('Zend_', '/path/to/project/vendor/zf1/library');
$writer = new Zend_Feed_Writer(); // <- Still works
$fooBar = new ZendFooBar(); // <- Nothing found

PSR-4

基本加载具有命名空间和PSR-4目录结构的类。

<?php
use Paulyg\Autoloader;
Autoloader::addPsr4('Symfony\Component\Routing', '/path/to/project/vendor/symfony/routing/');
$collection = new Symfony\Component\Routing\RouteCollection();

/path/to/project/vendor/symfony/routing/RouteCollection.php加载

再次去除开头的(根)命名空间分隔符。这两个是等价的。

<?php
use Paulyg\Autoloader;
Autoloader::addPsr4('Zend\Db', '/path/to/project/vendor/zendframework/zend-db/');
Autoloader::addPsr4('\Zend\Db', '/path/to/project/vendor/zendframework/zend-db/');

以与PSR-0相同的方式尊重尾随命名空间分隔符。此示例还说明了从实例使用与静态方法调用。

<?php
$autoloader = new Paulyg\Autoloader();
$autoloader->addPsr4('Zend\Feed', '/path/to/project/vendor/zendframework/zend-feed/');
$reader = new Zend\Feed\Reader(); // <- will load class
$writer = new Zend_Feed_Writer(); // <- will not load class

测试

Paulyg\Autoloader附带了一套完整的测试,使用PHPUnit编写。要从test目录运行测试,请执行以下命令。

$ phpunit "Paulyg\AutoloaderTest"

许可证

Paulyg\Autoloader使用MIT许可证授权。