Home > Development Tools > composer > How to make Composer's autoload support custom file suffixes

How to make Composer's autoload support custom file suffixes

藏色散人
Release: 2019-09-26 18:32:23
forward
1975 people have browsed it

下面由composer使用教程栏目为大家介绍让Composer的autoload支持自定义文件后缀名的方法,希望对需要的朋友有所帮助!

How to make Composer's autoload support custom file suffixes

PHP的Composer工具规范了我们对系统各种资源库的加载格式,借助于PHP的自动加载机制,可以很大程度上简化在应用开发过程中的类库文件引用场景。但到目前为止,它有个不是问题的问题,就是文件后缀名只支持.php,而基于某些框架开发的旧资产,类文件的后缀名是.class.php,想使用Composer的自动加载规范,就不太纯粹了,一般要两者混着用,或者修改其他框架下的加载规则。

有没有省事点的解决办法呢?

首先只要能产生这么一个疑问,就赢了。而答案呢,多半能找到的。

Composer实现自动加载机制的代码非常简练,稍微看一下就能看懂。

当看到ClassLoader.php文件中的findFileWithExtension方法时参数里出现了一个$ext,也就看到希望。只要在适当的时机,能覆盖这个$ext参数就搞定。

其原始代码如下:

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 {
Copy after login

稍微修改一下:

How to make Composers autoload support custom file suffixes

autload_psr4.php 配置文件中,对应的格式变化:

return array(
    'Qiniu\\' => array($vendorDir . '/qiniu/php-sdk/src/Qiniu’),
    // 字符串格式改为二维数组格式
    ‘Liniu\\' => array([$vendorDir . ‘/Liniu/php-sdk/src/Liniu’, ‘.class.php']),
);
Copy after login

贴出代码:

private function findFileWithExtension($class, $ext)
    {
        // PSR-4 lookup
        $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR);
        $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) {
                        $_ext = $ext;
                        $_dir = $dir;
                        if (is_array($dir) && count($dir) == 2) {
                            $_ext = $dir[1];
                            $_dir = $dir[0];
                        }
                        if (file_exists($file = $_dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4 . $_ext, $length))) {
                            return $file;
                        }
                    }
                }
            }
        }
        // PSR-4 fallback dirs
        foreach ($this->fallbackDirsPsr4 as $dir) {
            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4 . $ext)) {
                return $file;
            }
        }
        // PSR-0 lookup
        if (false !== $pos = strrpos($class, '\\')) {
            // namespaced class name
            $logicalPathPsr0 = substr($logicalPathPsr4 . $ext, 0, $pos + 1)
                . strtr(substr($logicalPathPsr4 . $ext, $pos + 1), '_', DIRECTORY_SEPARATOR);
        } else {
Copy after login

编码,有一种纯粹的乐趣。

The above is the detailed content of How to make Composer's autoload support custom file suffixes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:aliyun.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template