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.