Reputation: 439
I have an app ("foo") created in PHP that uses composer to bring it its dependancies. 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. The database credentials are stored in a config directory in the foo app.
My file structure for foo looks somewhat like this:
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.
How can bar determine the correct directory for 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. OpsWorks places an opsworks.php file in the root of the deployed app. 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. 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.
In other words, I want to ask Composer where the root for the app is.
Upvotes: 4
Views: 5176
Reputation: 4341
You can use composer's very own \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:
$configPath = $projectRootPath . '/config'
Don't forget to add composer
to your dependencies for the \Composer\Factory::class
to be available:
$ composer require composer/composer
Upvotes: 5
Reputation: 1941
If you look at 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.
Upvotes: 0