5 * This file is part of Psy Shell.
7 * (c) 2012-2017 Justin Hileman
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
13 // Try to find an autoloader for a local psysh version.
14 // We'll wrap this whole mess in a Closure so it doesn't leak any globals.
15 call_user_func(function () {
18 // Find the cwd arg (if present)
19 $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
20 foreach ($argv as $i => $arg) {
21 if ($arg === '--cwd') {
22 if ($i >= count($argv) - 1) {
23 echo 'Missing --cwd argument.' . PHP_EOL;
30 if (preg_match('/^--cwd=/', $arg)) {
31 $cwd = substr($arg, 6);
36 // Or fall back to the actual cwd
41 $cwd = str_replace('\\', '/', $cwd);
43 $chunks = explode('/', $cwd);
44 while (!empty($chunks)) {
45 $path = implode('/', $chunks);
48 if (is_file($path . '/composer.json')) {
49 if ($cfg = json_decode(file_get_contents($path . '/composer.json'), true)) {
50 if (isset($cfg['name']) && $cfg['name'] === 'psy/psysh') {
51 // We're inside the psysh project. Let's use the local
53 if (is_file($path . '/vendor/autoload.php')) {
54 require $path . '/vendor/autoload.php';
63 if (is_file($path . '/composer.lock')) {
64 if ($cfg = json_decode(file_get_contents($path . '/composer.lock'), true)) {
65 foreach (array_merge($cfg['packages'], $cfg['packages-dev']) as $pkg) {
66 if (isset($pkg['name']) && $pkg['name'] === 'psy/psysh') {
67 // We're inside a project which requires psysh. We'll
68 // use the local Composer autoload.
69 if (is_file($path . '/vendor/autoload.php')) {
70 require $path . '/vendor/autoload.php';
83 // We didn't find an autoloader for a local version, so use the autoloader that
84 // came with this script.
85 if (!class_exists('Psy\Shell')) {
87 if (is_file(__DIR__ . '/../vendor/autoload.php')) {
88 require __DIR__ . '/../vendor/autoload.php';
89 } elseif (is_file(__DIR__ . '/../../../autoload.php')) {
90 require __DIR__ . '/../../../autoload.php';
92 echo 'PsySH dependencies not found, be sure to run `composer install`.' . PHP_EOL;
93 echo 'See https://getcomposer.org to get Composer.' . PHP_EOL;
99 // If the psysh binary was included directly, assume they just wanted an
100 // autoloader and bail early.
102 // Keep this PHP 5.3 code around for a while in case someone is using a globally
103 // installed psysh as a bin launcher for older local versions.
104 if (version_compare(PHP_VERSION, '5.3.6', '<')) {
105 $trace = debug_backtrace();
106 } elseif (version_compare(PHP_VERSION, '5.4.0', '<')) {
107 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
109 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
112 if (Psy\Shell::isIncluded($trace)) {
118 // Clean up after ourselves.
121 // If the local version is too old, we can't do this
122 if (!function_exists('Psy\bin')) {
123 $argv = $_SERVER['argv'];
124 $first = array_shift($argv);
125 if (preg_match('/php(\.exe)?$/', $first)) {
128 array_unshift($argv, 'vendor/bin/psysh');
130 echo 'A local PsySH dependency was found, but it cannot be loaded. Please update to' . PHP_EOL;
131 echo 'the latest version, or run the local copy directly, e.g.:' . PHP_EOL;
133 echo ' ' . implode(' ', $argv) . PHP_EOL;
138 call_user_func(Psy\bin());