| 148 |
lars |
1 |
#!/usr/bin/env php
|
|
|
2 |
<?php
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
* This file is part of Psy Shell.
|
|
|
6 |
*
|
|
|
7 |
* (c) 2012-2022 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 |
fwrite(STDERR, '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 |
$prettyPath = $path;
|
|
|
47 |
if (isset($_SERVER['HOME']) && $_SERVER['HOME']) {
|
|
|
48 |
$prettyPath = preg_replace('/^' . preg_quote($_SERVER['HOME'], '/') . '/', '~', $path);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
// Find composer.json
|
|
|
52 |
if (is_file($path . '/composer.json')) {
|
|
|
53 |
if ($cfg = json_decode(file_get_contents($path . '/composer.json'), true)) {
|
|
|
54 |
if (isset($cfg['name']) && $cfg['name'] === 'psy/psysh') {
|
|
|
55 |
// We're inside the psysh project. Let's use the local Composer autoload.
|
|
|
56 |
if (is_file($path . '/vendor/autoload.php')) {
|
|
|
57 |
if (realpath($path) !== realpath(__DIR__ . '/..')) {
|
|
|
58 |
fwrite(STDERR, 'Using local PsySH version at ' . $prettyPath . PHP_EOL);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
require $path . '/vendor/autoload.php';
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
return;
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
// Or a composer.lock
|
|
|
70 |
if (is_file($path . '/composer.lock')) {
|
|
|
71 |
if ($cfg = json_decode(file_get_contents($path . '/composer.lock'), true)) {
|
|
|
72 |
foreach (array_merge($cfg['packages'], $cfg['packages-dev']) as $pkg) {
|
|
|
73 |
if (isset($pkg['name']) && $pkg['name'] === 'psy/psysh') {
|
|
|
74 |
// We're inside a project which requires psysh. We'll use the local Composer autoload.
|
|
|
75 |
if (is_file($path . '/vendor/autoload.php')) {
|
|
|
76 |
if (realpath($path . '/vendor') !== realpath(__DIR__ . '/../../..')) {
|
|
|
77 |
fwrite(STDERR, 'Using local PsySH version at ' . $prettyPath . PHP_EOL);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
require $path . '/vendor/autoload.php';
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
return;
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
array_pop($chunks);
|
|
|
90 |
}
|
|
|
91 |
});
|
|
|
92 |
|
|
|
93 |
// We didn't find an autoloader for a local version, so use the autoloader that
|
|
|
94 |
// came with this script.
|
|
|
95 |
if (!class_exists('Psy\Shell')) {
|
|
|
96 |
/* <<< */
|
|
|
97 |
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
|
|
|
98 |
require __DIR__ . '/../vendor/autoload.php';
|
|
|
99 |
} elseif (is_file(__DIR__ . '/../../../autoload.php')) {
|
|
|
100 |
require __DIR__ . '/../../../autoload.php';
|
|
|
101 |
} else {
|
|
|
102 |
fwrite(STDERR, 'PsySH dependencies not found, be sure to run `composer install`.' . PHP_EOL);
|
|
|
103 |
fwrite(STDERR, 'See https://getcomposer.org to get Composer.' . PHP_EOL);
|
|
|
104 |
exit(1);
|
|
|
105 |
}
|
|
|
106 |
/* >>> */
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
// If the psysh binary was included directly, assume they just wanted an
|
|
|
110 |
// autoloader and bail early.
|
|
|
111 |
//
|
|
|
112 |
// Keep this PHP 5.3 and 5.4 code around for a while in case someone is using a
|
|
|
113 |
// globally installed psysh as a bin launcher for older local versions.
|
|
|
114 |
if (version_compare(PHP_VERSION, '5.3.6', '<')) {
|
|
|
115 |
$trace = debug_backtrace();
|
|
|
116 |
} elseif (version_compare(PHP_VERSION, '5.4.0', '<')) {
|
|
|
117 |
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
|
|
118 |
} else {
|
|
|
119 |
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
if (Psy\Shell::isIncluded($trace)) {
|
|
|
123 |
unset($trace);
|
|
|
124 |
|
|
|
125 |
return;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// Clean up after ourselves.
|
|
|
129 |
unset($trace);
|
|
|
130 |
|
|
|
131 |
// If the local version is too old, we can't do this
|
|
|
132 |
if (!function_exists('Psy\bin')) {
|
|
|
133 |
$argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
|
|
|
134 |
$first = array_shift($argv);
|
|
|
135 |
if (preg_match('/php(\.exe)?$/', $first)) {
|
|
|
136 |
array_shift($argv);
|
|
|
137 |
}
|
|
|
138 |
array_unshift($argv, 'vendor/bin/psysh');
|
|
|
139 |
|
|
|
140 |
fwrite(STDERR, 'A local PsySH dependency was found, but it cannot be loaded. Please update to' . PHP_EOL);
|
|
|
141 |
fwrite(STDERR, 'the latest version, or run the local copy directly, e.g.:' . PHP_EOL);
|
|
|
142 |
fwrite(STDERR, PHP_EOL);
|
|
|
143 |
fwrite(STDERR, ' ' . implode(' ', $argv) . PHP_EOL);
|
|
|
144 |
exit(1);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
// And go!
|
|
|
148 |
call_user_func(Psy\bin());
|