Skip to content

Instantly share code, notes, and snippets.

@kamermans
Created August 29, 2014 18:40
Show Gist options
  • Save kamermans/f31f6ae82411253cf112 to your computer and use it in GitHub Desktop.
Save kamermans/f31f6ae82411253cf112 to your computer and use it in GitHub Desktop.
A better psysh/rc.php that automatically loads your composer autoloader
<?php
/**
* This is a better Psysh rc.php
*/
$add_composer_autoloader = function(array $config) {
static $was_run = false;
if ($was_run) {
return $config;
} else {
$was_run = true;
}
$dir = realpath(getcwd());
while (true) {
$composer_json = "$dir/composer.json";
if (is_readable($composer_json)) {
$autoloader = "$dir/vendor/autoload.php";
// Check for alternate vendor dir
$composer = json_decode(file_get_contents($composer_json), true);
if (isset($composer['config']) && isset($composer['config']['vendor-dir'])) {
$vendor_dir = $composer['config']['vendor-dir'];
$autoloader = "$dir/$vendor_dir/autoload.php";
}
if (is_readable($autoloader)) {
if (!isset($config["defaultIncludes"])) {
$config["defaultIncludes"] = [];
}
$config["defaultIncludes"][] = $autoloader;
echo "Added Composer autoloader: $autoloader\n";
return $config;
}
}
$last_dir = $dir;
$dir = realpath("$dir/../");
if ($dir === false || $dir === $last_dir) break;
}
return $config;
};
$config = [
// "Default includes" will be included once at the beginning of every PsySH
// session. This is a good place to add autoloaders for your favorite
// libraries.
'defaultIncludes' => [
// Use this line to add your user's global composer autoloader
// __DIR__.'/../.composer/vendor/autoload.php',
],
// While PsySH ships with a bunch of great commands, it's possible to add
// your own for even more awesome. Any Psy command added here will be
// available in your Psy shell sessions.
'commands' => [],
// PsySH ships with presenters for scalars, resources, arrays, and objects.
// But you're not limited to those presenters. You can enable additional
// presenters (like the included MongoCursorPresenter), or write your own!
'presenters' => [
// new \Psy\Presenter\MongoCursorPresenter,
],
];
return $add_composer_autoloader($config);
@rockymontana
Copy link

awesome gist! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment