我的应用程序有这样的结构
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/"
}
}
}
您可以看到,我在。json中有这个"module/*/source/"
。我这样做的原因是因为我想避免写出每个模块,因为它将是一个长列表。当我有新的模块时,它也不灵活,然后我必须对。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/"
....
有可能吗?
目前不能在自动加载定义的目录路径中使用通配符。
你可以调整你的项目,允许任何类的模块被自动加载通过改变你的目录结构。你必须去掉在完全限定类名中没有提到的所有级别,即没有"source"
"Article/source/Article/Model/ArticleModel.php"托管FooArticleModelArticleModel
将被要求驻留在"Article/Model/ArticleModel.php"
总是可能的替代方法是在自动加载中提到所有模块,或者将它们单独拆分为Composer包。
关于性能的注意事项:始终尝试使用最长的自动加载前缀。"psr-0": { "" : "library" }
将强制Composer查看该库目录并搜索您加载的所有类的匹配文件。尽管Composer试图通过记住失败来优化这一点,但它仍然有影响。
您应该总是添加唯一标识类位置的前缀,并且可以为PSR-0添加指向相同位置的多个前缀。对于PSR-4,位置因性质而异。
考虑编写一个脚本来生成自动加载定义。
你可以要求这个composer插件。它允许使用通配符:
psr4-advanced-wildcard-composer-plugin
这里,应该工作
module/.*?/source/
注意模式"module/*/source/"
,这里的*
也可以在bash
shell上很好地工作,它被称为globbing(路径名扩展)。
要在JSON
中完成路径扩展,您可以使用JSONPath
,这里有一个简单的例子
XPath vs JSONPath
XPath: /store/*
JSONPath: $.store.*
裁判:http://goessner.net/articles/JsonPath/