4

I'm trying to find the base directory of my application from within a composer package. Is there an composer API or proper way to accomplish this.

To clarify, if my package install looks like this and I've got files here (using psr-4):

/home/project/vendor/Acme/my-package/src/

how can I find /home/project dynamically?

Updated Info:

I'm trying to load a .env file from the root which contains an API URL endpoint via Dotenv package from within the package.

4
  • 1
    I wonder why you'd need the physical location of the project you are contained inside. This somehow states a dependency on the outside world, and probably should be handled by requiring the user of your package to provide that path instead of autodetecting it.
    – Sven
    Mar 31, 2015 at 18:25
  • Updated question with more info.
    – dcbarans
    Mar 31, 2015 at 18:34
  • 1
    Based in your input I'm seeing that you're suggesting I should just load the data on demand via constructor.. this is an easier solution
    – dcbarans
    Mar 31, 2015 at 18:39
  • That's likely the better solution. If you want to reuse your code (if you don't, why do you put it into a composer package then?), the next project that uses it won't have .env files somewhere, but a completely different configuration system. Ask for the information you need to do your work: Strings, integer, floats, booleans from "the configuration" should be read from wherever and passed as a parameter either into a factory or the constructor.
    – Sven
    Mar 31, 2015 at 22:07

5 Answers 5

3

This should do:

<?php

$root = null;

// Set the current directory.
// Make sure you set this up so
// that you get out of your own root.
// Assuming this php file is at the root
// of this composer package, this should suffice.
$directory = dirname(__FILE__);

// Go up until you find a composer.json file
// which should exist in the ancestors
// because its a composer package.
do {
    $directory = dirname($directory);
    $composer = $directory . '/composer.json';
    if(file_exists($composer)) $root = $directory;
} while(is_null($root) && $directory != '/');

// We either are at the root or we got lost.
// i.e. a composer.json was nowhere to be found.
if(!is_null($root))
{
    // Yay! we are at the root.
    // and $root contains the path.
    // Do whatever you seem fit!
    bootstrapOrSomething();
}
else
{
    // Oh no! Can we default to something?
    // Or just bail out?
    throw new Exception('Oops, did you require this package via composer?');
}

@sven There may be situations where this strategy might help. Like a general console app (phar) for any project and to avoid global installation but still load a bootsrap file. Composer allows bin files to be installed at the root https://getcomposer.org/doc/articles/vendor-binaries.md, but allowing users to place the phar file wherever they see fit is a plus.

One could argue about inversion of control, but imo, simplicity should come into play sometimes.

0

Ah.. I think I've figured it out..

Since I used include_once './vendor/autoload.php'; in my index.php I can just use getcwd() from the package

1
  • 3
    No, you can't, because that value may be changed anytime during application execution.
    – Sven
    Mar 31, 2015 at 18:23
0

Based in input from @Sven best and cleanest solution was to collect input on class initialization and leave configuration out of the package.

0

Try saboohy/basepath package. Gives directory of the project.

1
0

Try this:

$basePath = explode('/vendor', __FILE__)[0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.