7dbd203e7ab68f9e2afea9df4c6a410a99804ef9
[yaffs-website] / vendor / psy / psysh / bin / psysh
1 #!/usr/bin/env php
2 <?php
3
4 /*
5  * This file is part of Psy Shell.
6  *
7  * (c) 2012-2017 Justin Hileman
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12
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 () {
16     $cwd = null;
17
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;
24                 exit(1);
25             }
26             $cwd = $argv[$i + 1];
27             break;
28         }
29
30         if (preg_match('/^--cwd=/', $arg)) {
31             $cwd = substr($arg, 6);
32             break;
33         }
34     }
35
36     // Or fall back to the actual cwd
37     if (!isset($cwd)) {
38         $cwd = getcwd();
39     }
40
41     $cwd = str_replace('\\', '/', $cwd);
42
43     $chunks = explode('/', $cwd);
44     while (!empty($chunks)) {
45         $path = implode('/', $chunks);
46
47         // Find composer.json
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
52                     // Composer autoload.
53                     if (is_file($path . '/vendor/autoload.php')) {
54                         require $path . '/vendor/autoload.php';
55                     }
56
57                     return;
58                 }
59             }
60         }
61
62         // Or a composer.lock
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';
71                         }
72
73                         return;
74                     }
75                 }
76             }
77         }
78
79         array_pop($chunks);
80     }
81 });
82
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')) {
86 /* <<< */
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';
91     } else {
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;
94         exit(1);
95     }
96 /* >>> */
97 }
98
99 // If the psysh binary was included directly, assume they just wanted an
100 // autoloader and bail early.
101 //
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);
108 } else {
109     $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
110 }
111
112 if (Psy\Shell::isIncluded($trace)) {
113     unset($trace);
114
115     return;
116 }
117
118 // Clean up after ourselves.
119 unset($trace);
120
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)) {
126         array_shift($argv);
127     }
128     array_unshift($argv, 'vendor/bin/psysh');
129
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;
132     echo PHP_EOL;
133     echo '    ' . implode(' ', $argv) . PHP_EOL;
134     exit(1);
135 }
136
137 // And go!
138 call_user_func(Psy\bin());