1

我的应用程序有这个结构,

myapp/
  config/ (config and setup)
  module/ (tons of modules!)
     Article/ (one of the modules)
        source/ (<----- note that all php psr4 classes are stored in this folder)
           Article
              Model/
                 ArticleModel.php
                 ArticlesModel.php
              Controller/
              View/
     Book/ (one of the modules)
       ....(the structure is the same as Article's)
  public/  (web site doc root)
  vendor/
    composer/
    ...(other packages)

这是我的composer.json,

{
    "require": {
        "slim/slim": "~2.0",
        "slim/views": "0.1.*"
    },
    "autoload": {
        "psr-4": {
            "Foo\\": [
                "module/*/source/"
            ]
        },
        "psr-0": {
            "": "library/"
        }
    }
}

如您所见,我"module/*/source/"在 .json 中有这个。我这样做的原因是因为我想避免写出每个模块,因为这将是一个很长的列表。而且当我有新模块时它也不灵活,然后我必须对.json进行更改。

"module/*/source/"不工作。那么我可以在这个 .json 中做正则表达式或其他东西,以便它足够“智能”来获取所有模块吗?如,

"module/Artcle/source/"
"module/Book/source/"
"module/Contact/source/"
"module/Admin/Article/source/"
"module/Admin/Book/source/"
"module/Admin/Contact/source/"
....

可能吗?

4

3 回答 3

2

目前,您不能在自动加载定义的目录路径中使用通配符。

但是,您可以调整项目以允许通过更改目录结构来自动加载模块的任何类。您必须摆脱完全限定类名中未提及的所有级别,即没有“源”

“Article/source/Article/Model/ArticleModel.php”托管\Foo\Article\Model\ArticleModel需要住在“Article/Model/ArticleModel.php”中

始终可能的替代方法是在自动加载中提及所有模块,或者将它们自己拆分为 Composer 包。

关于性能的备注:始终尝试使用可能的最长前缀进行自动加载。"psr-0": { "" : "library" }将强制 Composer 查看该库目录并搜索您加载的所有类的匹配文件。尽管 Composer 试图通过记住失败来优化这一点,但它仍然会产生影响。

您应该始终添加唯一标识类位置的前缀,并且可以为 PSR-0 添加多个指向同一位置的前缀。对于 PSR-4,位置因性质而异。

考虑编写一个生成自动加载定义的脚本。

于 2015-08-29T10:25:07.277 回答
2

你可以要求这个作曲家插件。它允许使用通配符:

psr4-advanced-wildcard-composer-plugin

于 2019-11-02T21:53:31.233 回答
0

在这里,应该工作

module\/.*?\/source\/

从您的模式中注意"module/*/source/"*这里也可以在bashshell 上很好地工作,它被称为globbing(路径名扩展)。

附加信息

要完成路径扩展,JSON您可以使用JSONPath这里有一个简单的示例

XPath 与 JSONPath

XPath:/store/*

JSON路径:$.store.*

参考: http: //goessner.net/articles/JsonPath/

于 2015-08-29T09:52:49.177 回答