psysh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of Psy Shell.
  5. *
  6. * (c) 2012-2026 Justin Hileman
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. // Try to find an autoloader for a local psysh version.
  12. // We'll wrap this whole mess in a Closure so it doesn't leak any globals.
  13. call_user_func(function () {
  14. $cwd = null;
  15. $cwdFromArg = false;
  16. $forceTrust = false;
  17. $forceUntrust = false;
  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. $cwdFromArg = true;
  28. continue;
  29. }
  30. if (preg_match('/^--cwd=/', $arg)) {
  31. $cwd = substr($arg, 6);
  32. $cwdFromArg = true;
  33. continue;
  34. }
  35. if ($arg === '--trust-project') {
  36. $forceTrust = true;
  37. $forceUntrust = false;
  38. } elseif ($arg === '--no-trust-project') {
  39. $forceUntrust = true;
  40. $forceTrust = false;
  41. }
  42. }
  43. if ($cwdFromArg) {
  44. if (!@chdir($cwd)) {
  45. fwrite(STDERR, 'Invalid --cwd directory: ' . $cwd . PHP_EOL);
  46. exit(1);
  47. }
  48. }
  49. // Fall back to the actual cwd, or normalize the path after chdir
  50. if (!isset($cwd) || $cwdFromArg) {
  51. $cwd = getcwd();
  52. }
  53. $cwd = str_replace('\\', '/', $cwd);
  54. if ($cwdFromArg) {
  55. $filtered = array();
  56. $skipNext = false;
  57. foreach ($argv as $arg) {
  58. if ($skipNext) {
  59. $skipNext = false;
  60. continue;
  61. }
  62. if ($arg === '--cwd') {
  63. $skipNext = true;
  64. continue;
  65. }
  66. if (preg_match('/^--cwd=/', $arg)) {
  67. continue;
  68. }
  69. $filtered[] = $arg;
  70. }
  71. $_SERVER['argv'] = $filtered;
  72. $_SERVER['argc'] = count($filtered);
  73. $argv = $filtered;
  74. }
  75. if (isset($_SERVER['PSYSH_TRUST_PROJECT']) && $_SERVER['PSYSH_TRUST_PROJECT'] !== '') {
  76. $mode = strtolower(trim($_SERVER['PSYSH_TRUST_PROJECT']));
  77. if (in_array($mode, array('true', '1'))) {
  78. $forceTrust = true;
  79. $forceUntrust = false;
  80. } elseif (in_array($mode, array('false', '0'))) {
  81. $forceUntrust = true;
  82. $forceTrust = false;
  83. } else {
  84. fwrite(STDERR, 'Invalid PSYSH_TRUST_PROJECT value: ' . $_SERVER['PSYSH_TRUST_PROJECT'] . '. Expected: true, 1, false, or 0.' . PHP_EOL);
  85. exit(1);
  86. }
  87. }
  88. // Pass trust decision via env var and strip CLI flags. This allows a local
  89. // psysh version to read the trust state while avoiding errors on older
  90. // versions that don't understand --trust-project flags.
  91. if ($forceTrust) {
  92. $_SERVER['PSYSH_TRUST_PROJECT'] = 'true';
  93. $_ENV['PSYSH_TRUST_PROJECT'] = 'true';
  94. putenv('PSYSH_TRUST_PROJECT=true');
  95. } elseif ($forceUntrust) {
  96. $_SERVER['PSYSH_TRUST_PROJECT'] = 'false';
  97. $_ENV['PSYSH_TRUST_PROJECT'] = 'false';
  98. putenv('PSYSH_TRUST_PROJECT=false');
  99. }
  100. if ($forceTrust || $forceUntrust) {
  101. $filtered = array();
  102. foreach ($argv as $arg) {
  103. if ($arg === '--trust-project' || $arg === '--no-trust-project') {
  104. continue;
  105. }
  106. $filtered[] = $arg;
  107. }
  108. $_SERVER['argv'] = $filtered;
  109. $_SERVER['argc'] = count($filtered);
  110. $argv = $filtered;
  111. }
  112. $trustedRoots = array();
  113. if (!$forceTrust) {
  114. // Find the current config directory (matching ConfigPaths logic)
  115. $currentConfigDir = null;
  116. $fallbackConfigDir = null;
  117. // Windows: %APPDATA%/PsySH takes priority
  118. if ($currentConfigDir === null && defined('PHP_WINDOWS_VERSION_MAJOR')) {
  119. if (isset($_SERVER['APPDATA']) && $_SERVER['APPDATA']) {
  120. $dir = str_replace('\\', '/', $_SERVER['APPDATA']).'/PsySH';
  121. $fallbackConfigDir = $fallbackConfigDir !== null ? $fallbackConfigDir : $dir;
  122. if (@is_dir($dir)) {
  123. $currentConfigDir = $dir;
  124. }
  125. }
  126. }
  127. // XDG_CONFIG_HOME/psysh
  128. if ($currentConfigDir === null && isset($_SERVER['XDG_CONFIG_HOME']) && $_SERVER['XDG_CONFIG_HOME']) {
  129. $dir = rtrim(str_replace('\\', '/', $_SERVER['XDG_CONFIG_HOME']), '/').'/psysh';
  130. $fallbackConfigDir = $fallbackConfigDir !== null ? $fallbackConfigDir : $dir;
  131. if (@is_dir($dir)) {
  132. $currentConfigDir = $dir;
  133. }
  134. }
  135. // HOME/.config/psysh (default XDG location)
  136. if ($currentConfigDir === null && isset($_SERVER['HOME']) && $_SERVER['HOME']) {
  137. $home = rtrim(str_replace('\\', '/', $_SERVER['HOME']), '/');
  138. $dir = $home.'/.config/psysh';
  139. $fallbackConfigDir = $fallbackConfigDir !== null ? $fallbackConfigDir : $dir;
  140. if (@is_dir($dir)) {
  141. $currentConfigDir = $dir;
  142. }
  143. // legacy
  144. if ($currentConfigDir === null) {
  145. $dir = $home.'/.psysh';
  146. if (@is_dir($dir)) {
  147. $currentConfigDir = $dir;
  148. }
  149. }
  150. }
  151. // Windows: HOMEDRIVE/HOMEPATH fallback
  152. if ($currentConfigDir === null && defined('PHP_WINDOWS_VERSION_MAJOR')) {
  153. if (isset($_SERVER['HOMEDRIVE']) && isset($_SERVER['HOMEPATH']) && $_SERVER['HOMEDRIVE'] && $_SERVER['HOMEPATH']) {
  154. $dir = rtrim(str_replace('\\', '/', $_SERVER['HOMEDRIVE'].'/'.$_SERVER['HOMEPATH']), '/').'/.psysh';
  155. if (@is_dir($dir)) {
  156. $currentConfigDir = $dir;
  157. }
  158. }
  159. }
  160. // Fall back to the first candidate directory if none exist yet
  161. if ($currentConfigDir === null) {
  162. $currentConfigDir = $fallbackConfigDir;
  163. }
  164. if ($currentConfigDir !== null) {
  165. $trustFile = $currentConfigDir.'/trusted_projects.json';
  166. if (is_file($trustFile)) {
  167. $contents = file_get_contents($trustFile);
  168. if ($contents !== false && $contents !== '') {
  169. $data = json_decode($contents, true);
  170. if (is_array($data)) {
  171. foreach ($data as $dir) {
  172. if (!is_string($dir) || $dir === '') {
  173. continue;
  174. }
  175. $real = realpath($dir);
  176. if ($real !== false) {
  177. $dir = $real;
  178. }
  179. $trustedRoots[] = str_replace('\\', '/', $dir);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. // Composer-generated bin proxies expose `_composer_autoload_path`, which points
  187. // at the autoloader for the *current* project invoking `vendor/bin/psysh`.
  188. // We use this to distinguish "already running via this project's local psysh"
  189. // from "global psysh trying to hop into some other project's local psysh".
  190. $proxyAutoloadPath = null;
  191. if (isset($GLOBALS['_composer_autoload_path'])
  192. && is_string($GLOBALS['_composer_autoload_path'])
  193. && $GLOBALS['_composer_autoload_path'] !== ''
  194. ) {
  195. $proxyAutoloadPath = realpath($GLOBALS['_composer_autoload_path']);
  196. if ($proxyAutoloadPath === false) {
  197. $proxyAutoloadPath = str_replace('\\', '/', $GLOBALS['_composer_autoload_path']);
  198. } else {
  199. $proxyAutoloadPath = str_replace('\\', '/', $proxyAutoloadPath);
  200. }
  201. }
  202. $isCurrentProjectAutoload = function ($projectPath) use ($proxyAutoloadPath) {
  203. if ($proxyAutoloadPath === null) {
  204. return false;
  205. }
  206. $projectAutoloadPath = realpath($projectPath.'/vendor/autoload.php');
  207. if ($projectAutoloadPath === false) {
  208. return false;
  209. }
  210. return str_replace('\\', '/', $projectAutoloadPath) === $proxyAutoloadPath;
  211. };
  212. $markUntrustedProject = function ($projectPath, $prettyPath) {
  213. fwrite(STDERR, 'Skipping local PsySH at ' . $prettyPath . ' (project is untrusted). Re-run with --trust-project to allow.' . PHP_EOL);
  214. $_SERVER['PSYSH_UNTRUSTED_PROJECT'] = $projectPath;
  215. $_ENV['PSYSH_UNTRUSTED_PROJECT'] = $projectPath;
  216. putenv('PSYSH_UNTRUSTED_PROJECT='.$projectPath);
  217. };
  218. $chunks = explode('/', $cwd);
  219. while (!empty($chunks)) {
  220. $path = implode('/', $chunks);
  221. $prettyPath = $path;
  222. if (isset($_SERVER['HOME']) && $_SERVER['HOME']) {
  223. $prettyPath = preg_replace('/^' . preg_quote($_SERVER['HOME'], '/') . '/', '~', $path);
  224. }
  225. // Find composer.json
  226. if (is_file($path . '/composer.json')) {
  227. if ($cfg = json_decode(file_get_contents($path . '/composer.json'), true)) {
  228. if (isset($cfg['name']) && $cfg['name'] === 'psy/psysh') {
  229. // We're inside the psysh project. Let's use the local Composer autoload.
  230. if (is_file($path . '/vendor/autoload.php')) {
  231. $realPath = realpath($path);
  232. $realPath = $realPath ? str_replace('\\', '/', $realPath) : $path;
  233. $pathReal = realpath($path);
  234. $binReal = realpath(__DIR__ . '/..');
  235. $isCurrentPsysh = ($pathReal !== false && $pathReal === $binReal) || $isCurrentProjectAutoload($path);
  236. if (!$isCurrentPsysh && !$forceTrust && ($forceUntrust || !in_array($realPath, $trustedRoots, true))) {
  237. $markUntrustedProject($realPath, $prettyPath);
  238. return;
  239. }
  240. if (!$isCurrentPsysh) {
  241. fwrite(STDERR, 'Using local PsySH version at ' . $prettyPath . PHP_EOL);
  242. }
  243. require $path . '/vendor/autoload.php';
  244. }
  245. return;
  246. }
  247. }
  248. }
  249. // Or a composer.lock
  250. if (is_file($path . '/composer.lock')) {
  251. if ($cfg = json_decode(file_get_contents($path . '/composer.lock'), true)) {
  252. $packages = array_merge(isset($cfg['packages']) ? $cfg['packages'] : array(), isset($cfg['packages-dev']) ? $cfg['packages-dev'] : array());
  253. foreach ($packages as $pkg) {
  254. if (isset($pkg['name']) && $pkg['name'] === 'psy/psysh') {
  255. // We're inside a project which requires psysh. We'll use the local Composer autoload.
  256. if (is_file($path . '/vendor/autoload.php')) {
  257. $realPath = realpath($path);
  258. $realPath = $realPath ? str_replace('\\', '/', $realPath) : $path;
  259. $vendorReal = realpath($path . '/vendor');
  260. $binVendorReal = realpath(__DIR__ . '/../../..');
  261. $isCurrentPsysh = ($vendorReal !== false && $vendorReal === $binVendorReal) || $isCurrentProjectAutoload($path);
  262. if (!$isCurrentPsysh && !$forceTrust && ($forceUntrust || !in_array($realPath, $trustedRoots, true))) {
  263. $markUntrustedProject($realPath, $prettyPath);
  264. return;
  265. }
  266. if (!$isCurrentPsysh) {
  267. fwrite(STDERR, 'Using local PsySH version at ' . $prettyPath . PHP_EOL);
  268. }
  269. require $path . '/vendor/autoload.php';
  270. }
  271. return;
  272. }
  273. }
  274. }
  275. }
  276. array_pop($chunks);
  277. }
  278. });
  279. // We didn't find an autoloader for a local version, so use the autoloader that
  280. // came with this script.
  281. if (!class_exists('Psy\Shell')) {
  282. /* <<< */
  283. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  284. require __DIR__ . '/../vendor/autoload.php';
  285. } elseif (is_file(__DIR__ . '/../../../autoload.php')) {
  286. require __DIR__ . '/../../../autoload.php';
  287. } else {
  288. fwrite(STDERR, 'PsySH dependencies not found, be sure to run `composer install`.' . PHP_EOL);
  289. fwrite(STDERR, 'See https://getcomposer.org to get Composer.' . PHP_EOL);
  290. exit(1);
  291. }
  292. /* >>> */
  293. }
  294. // If the psysh binary was included directly, assume they just wanted an
  295. // autoloader and bail early.
  296. //
  297. // Keep this PHP 5.3 and 5.4 code around for a while in case someone is using a
  298. // globally installed psysh as a bin launcher for older local versions.
  299. if (PHP_VERSION_ID < 50306) {
  300. $trace = debug_backtrace();
  301. } elseif (PHP_VERSION_ID < 50400) {
  302. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  303. } else {
  304. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
  305. }
  306. if (Psy\Shell::isIncluded($trace)) {
  307. unset($trace);
  308. return;
  309. }
  310. // Clean up after ourselves.
  311. unset($trace);
  312. // If the local version is too old, we can't do this
  313. if (!function_exists('Psy\bin')) {
  314. $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
  315. $first = array_shift($argv);
  316. if (preg_match('/php(\.exe)?$/', $first)) {
  317. array_shift($argv);
  318. }
  319. array_unshift($argv, 'vendor/bin/psysh');
  320. fwrite(STDERR, 'A local PsySH dependency was found, but it cannot be loaded. Please update to' . PHP_EOL);
  321. fwrite(STDERR, 'the latest version, or run the local copy directly, e.g.:' . PHP_EOL);
  322. fwrite(STDERR, PHP_EOL);
  323. fwrite(STDERR, ' ' . implode(' ', $argv) . PHP_EOL);
  324. exit(1);
  325. }
  326. // And go!
  327. call_user_func(Psy\bin());