如何使用composer获取根包路径 [英] How to get the root package path using composer
问题描述
我正在开发一个名为 php-app-config 的 PHP 组件,使用 composer install
安装,应该在 根包,类似于root_package/config/config.yml
.
I'm developing a PHP component called php-app-config using composer.
This component, once required by another project, and installed using composer install
, should look for config files inside the config folder of the root package, something like root_package/config/config.yml
.
./config/config.yml
应该只存在于根包中,而不存在于 composer.json
中require:"导入的组件中,如下面:
The ./config/config.yml
should exists only in the root package and not inside the component imported by the "require:" in composer.json
, as below:
▾ root-package/
▸ bin/
▸ build/
▾ config/
▸ locales/
config.yml
▸ src/
▸ tests/
▾ vendor/
▸ composer/
▸ phpdocumentor/
▸ phpspec/
▸ phpunit/
▾ robotdance/
▾ php-app-config/
▾ src/
Config.php -> how to get the root "config/config.yml" path from here?
▸ tests/
composer.json
composer.lock
phpunit.xml
README.md
根包可以是 Web 应用程序或命令行实用程序.有没有办法使用composer获取根包路径?如果没有,更好的方法是什么?
The root package can be a web app or command line utility. Is there any way to get the root package path using composer? If not, what is the better way?
推荐答案
使用 ReflectionClass:
Using ReflectionClass:
$reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class);
$vendorDir = dirname(dirname($reflection->getFileName()));
这篇关于如何使用composer获取根包路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!