简体   繁体   English

如何使用Composer确定项目的根目录

[英]How to determine root directory for a project using Composer

I have an app ("foo") created in PHP that uses composer to bring it its dependancies. 我有一个用PHP创建的应用程序(“foo”),它使用composer来实现它的依赖性。 Once of the dependancies is another project ("bar") that we created. 一直是依赖性是我们创建的另一个项目(“酒吧”)。

When bar is added, by composer, to the foo app, bar needs access to the MySQL database of the foo app. 当添加 ,由作曲家,给foo的应用, 需要访问FOO应用的MySQL数据库。 The database credentials are stored in a config directory in the foo app. 数据库凭据存储在foo应用程序的config目录中。

My file structure for foo looks somewhat like this: 我对foo的文件结构看起来有点像这样:

app/ # files for the foo app config/ # config information to access MySQL vendor/ # composer-managed files, including our bar app composer.json # information to load bar into the vendor/ directory

When I'm developing the bar app on its own, I need to be able to place a config/ directory at the root of my bar development app and bar needs to be able to access it. 当我自己开发酒吧应用程序时,我需要能够在我的酒吧开发应用程序的根目录下放置一个配置/目录,并且需要能够访问它。 If bar is part of another composer project ( foo , in this case), I need bar to know to go to the root of foo for the config/ directory, not look in the root of bar . 如果酒吧是另一个作曲家项目的一部分(FOO,在这种情况下),我需要知道去foo的根在config /目录,在酒吧的根源不看。

How can bar determine the correct directory for config/? 如何禁止确定的config /正确的目录? I think I need to find the root directory of the composer install, but I don't know how to do that. 我想我需要找到作曲家安装的根目录,但我不知道该怎么做。

In case a real-world example would help... 万一真实世界的例子会有所帮助......

I'm deploying this foo app to Amazon OpsWorks. 我正在将这个foo应用程序部署到Amazon OpsWorks。 OpsWorks places an opsworks.php file in the root of the deployed app. OpsWorks将opsworks.php文件放在已部署应用程序的根目录中。 I need my submodules ( bar in my example above) to know where to access the opsworks.php file at the root of the composer project. 我需要我的子模块(上面例子中的 )知道在作曲家项目的根目录下访问opsworks.php文件的位置。 I need to account for the fact that composer may be set to not place bar in /vendor/, but might have a different file structure setup. 我需要考虑这样一个事实,即composer可能被设置为不在/ vendor /中放置bar ,但可能有不同的文件结构设置。

In other words, I want to ask Composer where the root for the app is. 换句话说,我想问一下Composer应用程序的根目录。

You can use composer's very own \\Composer\\Factory::getComposerFile(); 你可以使用作曲家自己的\\Composer\\Factory::getComposerFile(); to get to the project root directory: 到达项目根目录:

$projectRootPath = dirname(\\Composer\\Factory::getComposerFile());

And with your case, you can access your config/ directory by: 根据您的情况,您可以通过以下方式访问您的config/目录:

$configPath = $projectRootPath . '/config'

Don't forget to add composer to your dependencies for the \\Composer\\Factory::class to be available: 不要忘记将composer添加到依赖项中,以使\\Composer\\Factory::class可用:

$ composer require composer/composer

If you look at https://getcomposer.org/doc/01-basic-usage.md 如果你看一下https://getcomposer.org/doc/01-basic-usage.md

The section for autoload describes the behaviour you appear to be looking for. 自动加载部分描述了您正在寻找的行为。

$loader = require 'vendor/autoload.php';
$loader->add('Acme\\Test\\', __DIR__);

You can define the root of foo and the root of bar in this way and only load the items you need. 您可以通过这种方式定义foo的根和bar的根,只加载您需要的项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM