没有作曲家的 PSR4 自动加载

PSR4 auto load without composer(没有作曲家的 PSR4 自动加载)
本文介绍了没有作曲家的 PSR4 自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中有一个使用composer自动加载的包,composer.json条目如下:

I have one package in a project which is autoloaded using composer and composer.json entry is as follows :

 "autoload": {
      "psr-4": {
        "CompanyName\PackageName\": "packages/package-folder/src/"
    }
  }

现在我将其复制到另一个未使用 composer 的项目中.我怎样才能在那里自动加载相同的包?

Now I am copying this over to another project which is not using composer. How can I autoload this same package there ?

推荐答案

您必须阅读 composer 并为 composer.json 中定义的每个命名空间自行加载类.

You have to read the composer and load the classes yourself for each namespaces defined into the composer.json.

方法如下:

function loadPackage($dir)
{
    $composer = json_decode(file_get_contents("$dir/composer.json"), 1);
    $namespaces = $composer['autoload']['psr-4'];

    // Foreach namespace specified in the composer, load the given classes
    foreach ($namespaces as $namespace => $classpaths) {
        if (!is_array($classpaths)) {
            $classpaths = array($classpaths);
        }
        spl_autoload_register(function ($classname) use ($namespace, $classpaths, $dir) {
            // Check if the namespace matches the class we are looking for
            if (preg_match("#^".preg_quote($namespace)."#", $classname)) {
                // Remove the namespace from the file path since it's psr4
                $classname = str_replace($namespace, "", $classname);
                $filename = preg_replace("#\\#", "/", $classname).".php";
                foreach ($classpaths as $classpath) {
                    $fullpath = $dir."/".$classpath."/$filename";
                    if (file_exists($fullpath)) {
                        include_once $fullpath;
                    }
                }
            }
        });
    }
}

loadPackage(__DIR__."/vendor/project");

new CompanyNamePackageNameTest();

当然,我不知道你在 PackageName 中有哪些类./vendor/project 是克隆或下载外部库的位置.这是您拥有 composer.json 文件的位置.

Of course, I don't know the classes you have in the PackageName. The /vendor/project is where your external library was cloned or downloaded. This is where you have the composer.json file.

注意:这仅适用于 psr4 自动加载.

Note: this works only for psr4 autoload.

编辑:为一个命名空间添加对多个类路径的支持

EDIT : Adding support for multiple classpaths for one namespace

EDIT2 :我创建了一个 Github repo 来处理这段代码,如果有人想改进它.

EDIT2 : I created a Github repo to handle this code, if anyone wants to improve it.

这篇关于没有作曲家的 PSR4 自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)