No Description

psysh 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of Psy Shell.
  5. *
  6. * (c) 2012-2015 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. // Find the cwd arg (if present)
  16. $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
  17. foreach ($argv as $i => $arg) {
  18. if ($arg === '--cwd') {
  19. if ($i >= count($argv) - 1) {
  20. echo 'Missing --cwd argument.' . PHP_EOL;
  21. exit(1);
  22. }
  23. $cwd = $argv[$i + 1];
  24. break;
  25. }
  26. if (preg_match('/^--cwd=/', $arg)) {
  27. $cwd = substr($arg, 6);
  28. break;
  29. }
  30. }
  31. // Or fall back to the actual cwd
  32. if (!isset($cwd)) {
  33. $cwd = getcwd();
  34. }
  35. $cwd = str_replace('\\', '/', $cwd);
  36. $chunks = explode('/', $cwd);
  37. while (!empty($chunks)) {
  38. $path = implode('/', $chunks);
  39. // Find composer.json
  40. if (is_file($path . '/composer.json')) {
  41. if ($cfg = json_decode(file_get_contents($path . '/composer.json'), true)) {
  42. if (isset($cfg['name']) && $cfg['name'] === 'psy/psysh') {
  43. // We're inside the psysh project. Let's use the local
  44. // Composer autoload.
  45. if (is_file($path . '/vendor/autoload.php')) {
  46. require $path . '/vendor/autoload.php';
  47. }
  48. return;
  49. }
  50. }
  51. }
  52. // Or a composer.lock
  53. if (is_file($path . '/composer.lock')) {
  54. if ($cfg = json_decode(file_get_contents($path . '/composer.lock'), true)) {
  55. foreach (array_merge($cfg['packages'], $cfg['packages-dev']) as $pkg) {
  56. if (isset($pkg['name']) && $pkg['name'] === 'psy/psysh') {
  57. // We're inside a project which requires psysh. We'll
  58. // use the local Composer autoload.
  59. if (is_file($path . '/vendor/autoload.php')) {
  60. require $path . '/vendor/autoload.php';
  61. }
  62. return;
  63. }
  64. }
  65. }
  66. }
  67. array_pop($chunks);
  68. }
  69. });
  70. // We didn't find an autoloader for a local version, so use the autoloader that
  71. // came with this script.
  72. if (!class_exists('Psy\Shell')) {
  73. /* <<< */
  74. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  75. require __DIR__ . '/../vendor/autoload.php';
  76. } elseif (is_file(__DIR__ . '/../../../autoload.php')) {
  77. require __DIR__ . '/../../../autoload.php';
  78. } else {
  79. echo 'PsySH dependencies not found, be sure to run `composer install`.' . PHP_EOL;
  80. echo 'See https://getcomposer.org to get Composer.' . PHP_EOL;
  81. exit(1);
  82. }
  83. /* >>> */
  84. }
  85. // If the psysh binary was included directly, assume they just wanted an
  86. // autoloader and bail early.
  87. if (version_compare(PHP_VERSION, '5.3.6', '<')) {
  88. $trace = debug_backtrace();
  89. } elseif (version_compare(PHP_VERSION, '5.4.0', '<')) {
  90. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  91. } else {
  92. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
  93. }
  94. if (Psy\Shell::isIncluded($trace)) {
  95. unset($trace);
  96. return;
  97. }
  98. // Clean up after ourselves.
  99. unset($trace);
  100. // If the local version is too old, we can't do this
  101. if (!function_exists('Psy\bin')) {
  102. $argv = $_SERVER['argv'];
  103. $first = array_shift($argv);
  104. if (preg_match('/php(\.exe)?$/', $first)) {
  105. array_shift($argv);
  106. }
  107. array_unshift($argv, 'vendor/bin/psysh');
  108. echo 'A local PsySH dependency was found, but it cannot be loaded. Please update to' . PHP_EOL;
  109. echo 'the latest version, or run the local copy directly, e.g.:' . PHP_EOL;
  110. echo PHP_EOL;
  111. echo ' ' . implode(' ', $argv) . PHP_EOL;
  112. exit(1);
  113. }
  114. // And go!
  115. call_user_func(Psy\bin());