- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在一个项目中有一个包,它是使用 composer 和 composer.json 条目自动加载的,如下所示:
"autoload": {
"psr-4": {
"CompanyName\\PackageName\\": "packages/package-folder/src/"
}
}
现在我将其复制到另一个未使用 Composer 的项目中。我怎样才能在那里自动加载同一个包?
最佳答案
您必须阅读 composer 并自行为 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 CompanyName\PackageName\Test();
当然,我不知道你在PackageName中有哪些类。/vendor/project
是克隆或下载外部库的地方。这是您拥有 composer.json
文件的地方。
注意:这仅适用于 psr4 自动加载。
编辑:为一个命名空间添加对多个类路径的支持
EDIT2 :我创建了一个 Github repo处理这段代码,如果有人想改进它。
关于php - 没有 Composer 的 PSR 4 自动加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39571391/
TD;LR:我试图掌握中间件实现背后的理念。它似乎可以正常工作,但我该如何正确处理响应,以便它向浏览器显示基本授权登录提示? -- 我正在使用: equip/dispatch用于 PSR-15 中间件
我将我的一些 bundle 的 psr-0 自动加载更改为 psr-4。现在,当我尝试使用 app/console 命令生成实体时,它给了我 Can't find base path for "MbD
最近我读到了关于命名空间以及它们的好处的文章。我目前正在 Laravel 中创建一个项目,并尝试从类映射自动加载转移到命名空间。但是,我似乎无法理解 PSR-0 和 PSR-4 之间的实际区别。 我读
我开始研究 Composer 并正在开发一个系统,我将文件核心应用程序文件分开,如下所示: /root |-- /src |-- /App |-
我有一个在内部项目中使用的 composer 包。历史上,此包中的所有类都是通过 "autoload": { "classmap": ... } 自动加载的,并且尚未结构化。 现在我想迁移到 PSR-
我正在尝试 PHP 编码标准包:https://github.com/inpsyde/php-coding-standards ,但是当我开始运行 phpcs 时,我得到: nickan@nickan
我正在尝试 PHP 编码标准包:https://github.com/inpsyde/php-coding-standards ,但是当我开始运行 phpcs 时,我得到: nickan@nickan
我尝试使用 PSR-0 自动加载文件,但它不会自动加载该文件。我用 PSR-4 自动加载尝试了相同的文件。使用 PSR-4,它可以完美运行。 PSR-0 所需的文件夹结构有什么不同吗? 即使保留 Wh
我正在使用 Laravel 5.0,一切正常。 但是,我不断收到这些警告消息。我已经查看了 JetBrains 文档,但我仍然有点不清楚该怎么办。 本质上,我担心如果我允许 PhpStorm 自动运行
我是 C++ 的新手,想了解一些东西。我的 main.cpp 中有这段代码: Radio r = Radio("PSR", 100.8); 或者那个代码: Radio r("PSR", 100.8);
我是 C++ 的新手,想了解一些东西。我的 main.cpp 中有这段代码: Radio r = Radio("PSR", 100.8); 或者那个代码: Radio r("PSR", 100.8);
我知道您可以使用 PSR 标准来定位文件,或者告诉 composer 一个目录来扫描类。 documentation recommends using the PSR-4标准。 Composer 还可
Composer 是否有来自 CLI 的任何命令添加到 composer.json 这样的入口? { "autoload": { "psr-4": {
根据 PSR-2 Visibility MUST be declared on all properties and methods 但是 __construct 呢,它是特定的功能,我想知道当我们使
PSR建议,方法名称必须在camelCase中声明,而类名称必须在StudlyCaps中声明。 最佳答案 StudlyCaps,也称为PascalCase,表示每个子单词的首字母大写。 camelCa
任何人都可以建议 PSR 标准文档中的哪个位置记录了类成员变量中的变量类型后面不应有任何内容吗? 我用了PHP CodeStyle Fixer通过执行以下操作: php-cs-fixer 修复 MyC
我希望我的 IDE (PhpStorm) 对所有自己的文件强制执行 PSR-2 兼容格式。然而,有时我必须提交遗留代码的补丁,我只想提交功能更改,而不是代码样式更改。 我如何使用 PhpStorm 仅
我有一个在本地运行良好的 Laravel 项目(Mavericks),但是 psr-4 下的类没有加载到我们的舞台服务器(CentOS)上。每次尝试 composer update 或运行 artis
我创建了公司供应商库,但 namespace 有问题。 通常我会忽略这一点,但现在想要解决问题。 我现在测试示例供应商 Symfony\Bundle\FrameworkBundle但这有同样的错误。
我想从 lib 加载 HomeController 类目录: root/ -lib/ --/HomeController.php -vendor/ -composer.json -index.php
我是一名优秀的程序员,十分优秀!