资讯专栏INFORMATION COLUMN

PSR-4:自动加载

ZweiZhao / 1025人阅读

摘要:概述这份声明了关于从文件路径自动加载类的规范。当根据完全限定类名加载对应的文件时由最开始的命名空间开始,连续的一个或多个命名空间组成的序列,不包括最前面的命名空间分隔符,在这个完全限定类名中这个序列称为命名空间前缀,对应了至少一个基础目录。

PSR-4:自动加载

翻译:薛粲
授权许可:CC BY-NC 4.0

这份文档是《PSR-4: Autoloader》的非官方译文。

英文原文使用的关键词 "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", 以及 "OPTIONAL" 遵循 RFC 2119 的描述。译文中根据上下文可能会使用不同的词汇来对应这些关键词,并加粗显示。

1. 概述

这份 PSR 声明了关于从文件路径自动加载(autoloading)类的规范。它具有完整的互用性,可以结合任何其它关于自动加载机制的规范使用,包括 PSR-0。这份 PSR 还描述了在何处保存文件以便可以根据这份规范实现自动加载。

2. 规范

术语“类”指代类、接口、trait 以及其它类似的结构。

一个完全限定(fully qualified)类名形如:()*

完全限定类名必须包含一个顶级命名空间名,这也被称为“开发商命名空间”。

完全限定类名可以包含一个或多个子命名空间名称。

完全限定类名必须以一个类名结束。

完全限定类名各部分中的下划线没有任何特殊的含义。

完全限定类名中的字母可以使用任意大小写组合。

所有类名必须以大小写敏感的方式被引用。

当根据完全限定类名加载对应的文件时:

由最开始的命名空间开始,连续的一个或多个命名空间组成的序列,不包括最前面的命名空间分隔符,在这个完全限定类名中(这个序列称为“命名空间前缀,namespace prefix”)对应了至少一个“基础目录”。

“命名空间前缀”之后相邻的子命名空间,对应“基础目录”中的子目录,命名空间分隔符对应目录分隔符。子目录名必须与子命名空间名大小写匹配。

结尾的类名对应一个以 .php 最为后缀的文件名。文件名必须与类名大小写匹配。

自动加载机制的具体实现不得抛出异常,不得产生任何等级的错误,且不应该有返回值。

3. 示例

下表列出了一些文件路径及其相对应的完全限定类名、命名空间前缀和基础目录。

完全限定类名 命名空间前缀 基础目录 文件路径
AcmeLogWriterFile_Writer AcmeLogWriter ./acme-log-writer/lib/ ./acme-log-writer/lib/File_Writer.php
AuraWebResponseStatus AuraWeb /path/to/aura-web/src/ /path/to/aura-web/src/Response/Status.php
SymfonyCoreRequest SymfonyCore ./vendor/Symfony/Core/ ./vendor/Symfony/Core/Request.php
endAcl Zend /usr/includes/Zend/ /usr/includes/Zend/Acl.php

PSR-4 提供遵循本规范的自动加载机制实现范例。这些范例不得被当做本规范的一部分,并可能随时修改。


实现范例

下面的示例提供了遵循 PSR-4 的参考实现。

使用闭包的范例

使用类的范例

下面基于类的实现范例可以处理多个命名空间:

register();
 *      
 *      // register the base directories for the namespace prefix
 *      $loader->addNamespace("FooBar", "/path/to/packages/foo-bar/src");
 *      $loader->addNamespace("FooBar", "/path/to/packages/foo-bar/tests");
 * 
 * 下面的代码将会尝试从文件
 * /path/to/packages/foo-bar/src/Qux/Quux.php
 * 加载类 FooBarQuxQuux:
 * 
 *      prefixes[$prefix]) === false) {
            $this->prefixes[$prefix] = array();
        }

        // retain the base directory for the namespace prefix
        if ($prepend) {
            array_unshift($this->prefixes[$prefix], $base_dir);
        } else {
            array_push($this->prefixes[$prefix], $base_dir);
        }
    }

    /**
     * Loads the class file for a given class name.
     *
     * @param string $class The fully-qualified class name.
     * @return mixed The mapped file name on success, or boolean false on
     * failure.
     */
    public function loadClass($class)
    {
        // the current namespace prefix
        $prefix = $class;

        // work backwards through the namespace names of the fully-qualified
        // class name to find a mapped file name
        while (false !== $pos = strrpos($prefix, "")) {

            // retain the trailing namespace separator in the prefix
            $prefix = substr($class, 0, $pos + 1);

            // the rest is the relative class name
            $relative_class = substr($class, $pos + 1);

            // try to load a mapped file for the prefix and relative class
            $mapped_file = $this->loadMappedFile($prefix, $relative_class);
            if ($mapped_file) {
                return $mapped_file;
            }

            // remove the trailing namespace separator for the next iteration
            // of strrpos()
            $prefix = rtrim($prefix, "");   
        }

        // never found a mapped file
        return false;
    }

    /**
     * Load the mapped file for a namespace prefix and relative class.
     * 
     * @param string $prefix The namespace prefix.
     * @param string $relative_class The relative class name.
     * @return mixed Boolean false if no mapped file can be loaded, or the
     * name of the mapped file that was loaded.
     */
    protected function loadMappedFile($prefix, $relative_class)
    {
        // are there any base directories for this namespace prefix?
        if (isset($this->prefixes[$prefix]) === false) {
            return false;
        }

        // look through base directories for this namespace prefix
        foreach ($this->prefixes[$prefix] as $base_dir) {

            // replace the namespace prefix with the base directory,
            // replace namespace separators with directory separators
            // in the relative class name, append with .php
            $file = $base_dir
                  . str_replace("", "/", $relative_class)
                  . ".php";

            // if the mapped file exists, require it
            if ($this->requireFile($file)) {
                // yes, we"re done
                return $file;
            }
        }

        // never found it
        return false;
    }

    /**
     * If a file exists, require it from the file system.
     * 
     * @param string $file The file to require.
     * @return bool True if the file exists, false if not.
     */
    protected function requireFile($file)
    {
        if (file_exists($file)) {
            require $file;
            return true;
        }
        return false;
    }
}
单元测试

下面的范例是对上面的类加载机制的一种单元测试实现方案:

files = $files;
    }

    protected function requireFile($file)
    {
        return in_array($file, $this->files);
    }
}

class Psr4AutoloaderClassTest extends PHPUnit_Framework_TestCase
{
    protected $loader;

    protected function setUp()
    {
        $this->loader = new MockPsr4AutoloaderClass;

        $this->loader->setFiles(array(
            "/vendor/foo.bar/src/ClassName.php",
            "/vendor/foo.bar/src/DoomClassName.php",
            "/vendor/foo.bar/tests/ClassNameTest.php",
            "/vendor/foo.bardoom/src/ClassName.php",
            "/vendor/foo.bar.baz.dib/src/ClassName.php",
            "/vendor/foo.bar.baz.dib.zim.gir/src/ClassName.php",
        ));

        $this->loader->addNamespace(
            "FooBar",
            "/vendor/foo.bar/src"
        );

        $this->loader->addNamespace(
            "FooBar",
            "/vendor/foo.bar/tests"
        );

        $this->loader->addNamespace(
            "FooBarDoom",
            "/vendor/foo.bardoom/src"
        );

        $this->loader->addNamespace(
            "FooBarBazDib",
            "/vendor/foo.bar.baz.dib/src"
        );

        $this->loader->addNamespace(
            "FooBarBazDibimGir",
            "/vendor/foo.bar.baz.dib.zim.gir/src"
        );
    }

    public function testExistingFile()
    {
        $actual = $this->loader->loadClass("FooBarClassName");
        $expect = "/vendor/foo.bar/src/ClassName.php";
        $this->assertSame($expect, $actual);

        $actual = $this->loader->loadClass("FooBarClassNameTest");
        $expect = "/vendor/foo.bar/tests/ClassNameTest.php";
        $this->assertSame($expect, $actual);
    }

    public function testMissingFile()
    {
        $actual = $this->loader->loadClass("No_VendorNo_PackageNoClass");
        $this->assertFalse($actual);
    }

    public function testDeepFile()
    {
        $actual = $this->loader->loadClass("FooBarBazDibimGirClassName");
        $expect = "/vendor/foo.bar.baz.dib.zim.gir/src/ClassName.php";
        $this->assertSame($expect, $actual);
    }

    public function testConfusion()
    {
        $actual = $this->loader->loadClass("FooBarDoomClassName");
        $expect = "/vendor/foo.bar/src/DoomClassName.php";
        $this->assertSame($expect, $actual);

        $actual = $this->loader->loadClass("FooBarDoomClassName");
        $expect = "/vendor/foo.bardoom/src/ClassName.php";
        $this->assertSame($expect, $actual);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/21466.html

相关文章

  • PHP自动加载功能原理解析

    摘要:前言在开始之前,欢迎关注我自己的博客这篇文章是对自动加载功能的一个总结,内容涉及的自动加载功能的命名空间的与标准等内容。要实现第一步,第二步的功能,必须在开发时约定类名与磁盘文件的映射方法,只有这样我们才能根据类名找到它对应的磁盘文件。 前言 在开始之前,欢迎关注我自己的博客:www.leoyang90.cn 这篇文章是对PHP自动加载功能的一个总结,内容涉及PHP的自动加载功能、P...

    Imfan 评论0 收藏0
  • composer 自动加载

    摘要:自动加载是指在代码中,不需要显式地使用文件路径将类库文件包含进来,便可使用该文件中定义的类库。在里是这样进行配置的按照的规则,当试图自动加载这个时,会去寻找这个文件。最后,只要在项目中你所需要的所有类库都会在适当的时候自动载入。 Composer是PHP中用来管理依赖(dependency)关系的工具。你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer...

    alighters 评论0 收藏0
  • PHP包管理工具--Composer自动加载

    摘要:接触过的同学都知道使用作为项目的包管理工具但是并不是独有的是的包管理工具这两者的关系就像于于一样但是发现真正项目中使用还是比较少的所以这里这里写一遍文章介绍的使用帮助那些对于还是有点模糊的同学此文跟没有任何联系安装的方式就不讲了具体安装方式 接触过Laravel的同学都知道,Laravel使用Composer作为项目的包管理工具.但是Composer并不是Laravel独有的,Comp...

    xiaoqibTn 评论0 收藏0
  • Yii2中的代码自动加载机制

    摘要:中是如何实现代码的自动加载的入口脚本的以下两行代码其中的作用注册为自动加载函数。这个负责引入了一个类中的,随后立即解除注册。注册中的为自动加载函数,并利用配置文件即目录下的文件对这个自动加载函数进行了初始化。 1.基本知识 Include与require 的作用: 当一个文件被包含时,其中所包含的代码继承了 include 所在行的变量范围。从该处开始,调用文件在该行处可用的任何...

    Jaden 评论0 收藏0
  • PSR-4——新鲜出炉的PHP规范

    摘要:制定的规范,简称,是开发的事实标准。原本有四个规范,分别是自动加载基本代码规范代码样式日志接口年底,新出了第个规范。区别在于的规范比较干净,去除了兼容以前版本的内容,有一点升级版的感觉。 FIG制定的PHP规范,简称PSR,是PHP开发的事实标准。 PSR原本有四个规范,分别是: PSR-0 自动加载 PSR-1 基本代码规范 PSR-2 代码样式 PSR-3 日志接口 20...

    Fundebug 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<