console.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * Description of this file.
  19. *
  20. * @package samples
  21. * @author Diogo Cordeiro <diogo@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. define('INSTALLDIR', dirname(__FILE__));
  26. define('GNUSOCIAL', true);
  27. define('STATUSNET', true);
  28. require_once INSTALLDIR . '/lib/common.php';
  29. // Try to find an autoloader for a local psysh version.
  30. // We'll wrap this whole mess in a Closure so it doesn't leak any globals.
  31. call_user_func(function () {
  32. $cwd = null;
  33. // Find the cwd arg (if present)
  34. $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
  35. foreach ($argv as $i => $arg) {
  36. if ($arg === '--cwd') {
  37. if ($i >= count($argv) - 1) {
  38. echo 'Missing --cwd argument.' . PHP_EOL;
  39. exit(1);
  40. }
  41. $cwd = $argv[$i + 1];
  42. break;
  43. }
  44. if (preg_match('/^--cwd=/', $arg)) {
  45. $cwd = substr($arg, 6);
  46. break;
  47. }
  48. }
  49. // Or fall back to the actual cwd
  50. if (!isset($cwd)) {
  51. $cwd = getcwd();
  52. }
  53. $cwd = str_replace('\\', '/', $cwd);
  54. $chunks = explode('/', $cwd);
  55. while (!empty($chunks)) {
  56. $path = implode('/', $chunks);
  57. // Find composer.json
  58. if (is_file($path . '/composer.json')) {
  59. if ($cfg = json_decode(file_get_contents($path . '/composer.json'), true)) {
  60. if (isset($cfg['name']) && $cfg['name'] === 'psy/psysh') {
  61. // We're inside the psysh project. Let's use the local
  62. // Composer autoload.
  63. if (is_file($path . '/vendor/autoload.php')) {
  64. require $path . '/vendor/autoload.php';
  65. }
  66. return;
  67. }
  68. }
  69. }
  70. // Or a composer.lock
  71. if (is_file($path . '/composer.lock')) {
  72. if ($cfg = json_decode(file_get_contents($path . '/composer.lock'), true)) {
  73. foreach (array_merge($cfg['packages'], $cfg['packages-dev']) as $pkg) {
  74. if (isset($pkg['name']) && $pkg['name'] === 'psy/psysh') {
  75. // We're inside a project which requires psysh. We'll
  76. // use the local Composer autoload.
  77. if (is_file($path . '/vendor/autoload.php')) {
  78. require $path . '/vendor/autoload.php';
  79. }
  80. return;
  81. }
  82. }
  83. }
  84. }
  85. array_pop($chunks);
  86. }
  87. });
  88. // We didn't find an autoloader for a local version, so use the autoloader that
  89. // came with this script.
  90. if (!class_exists('Psy\Shell')) {
  91. /* <<< */
  92. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  93. require __DIR__ . '/../vendor/autoload.php';
  94. } elseif (is_file(__DIR__ . '/../../../autoload.php')) {
  95. require __DIR__ . '/../../../autoload.php';
  96. } else {
  97. echo 'PsySH dependencies not found, be sure to run `composer install`.' . PHP_EOL;
  98. echo 'See https://getcomposer.org to get Composer.' . PHP_EOL;
  99. exit(1);
  100. }
  101. /* >>> */
  102. }
  103. // If the psysh binary was included directly, assume they just wanted an
  104. // autoloader and bail early.
  105. if (version_compare(PHP_VERSION, '5.3.6', '<')) {
  106. $trace = debug_backtrace();
  107. } elseif (version_compare(PHP_VERSION, '5.4.0', '<')) {
  108. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  109. } else {
  110. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
  111. }
  112. if (Psy\Shell::isIncluded($trace)) {
  113. unset($trace);
  114. return;
  115. }
  116. // Clean up after ourselves.
  117. unset($trace);
  118. // If the local version is too old, we can't do this
  119. if (!function_exists('Psy\bin')) {
  120. $argv = $_SERVER['argv'];
  121. $first = array_shift($argv);
  122. if (preg_match('/php(\.exe)?$/', $first)) {
  123. array_shift($argv);
  124. }
  125. array_unshift($argv, 'vendor/bin/psysh');
  126. echo 'A local PsySH dependency was found, but it cannot be loaded. Please update to' . PHP_EOL;
  127. echo 'the latest version, or run the local copy directly, e.g.:' . PHP_EOL;
  128. echo PHP_EOL;
  129. echo ' ' . implode(' ', $argv) . PHP_EOL;
  130. exit(1);
  131. }
  132. // And go!
  133. call_user_func(Psy\bin());