console.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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(__DIR__));
  26. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  27. define('GNUSOCIAL', true);
  28. define('STATUSNET', true);
  29. require_once INSTALLDIR . '/lib/util/common.php';
  30. // Try to find an autoloader for a local psysh version.
  31. // We'll wrap this whole mess in a Closure so it doesn't leak any globals.
  32. call_user_func(function () {
  33. $cwd = null;
  34. // Find the cwd arg (if present)
  35. $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
  36. foreach ($argv as $i => $arg) {
  37. if ($arg === '--cwd') {
  38. if ($i >= count($argv) - 1) {
  39. echo 'Missing --cwd argument.' . PHP_EOL;
  40. exit(1);
  41. }
  42. $cwd = $argv[$i + 1];
  43. break;
  44. }
  45. if (preg_match('/^--cwd=/', $arg)) {
  46. $cwd = substr($arg, 6);
  47. break;
  48. }
  49. }
  50. // Or fall back to the actual cwd
  51. if (!isset($cwd)) {
  52. $cwd = getcwd();
  53. }
  54. $cwd = str_replace('\\', '/', $cwd);
  55. $chunks = explode('/', $cwd);
  56. while (!empty($chunks)) {
  57. $path = implode('/', $chunks);
  58. // Find composer.json
  59. if (is_file($path . '/composer.json')) {
  60. if (!empty($cfg = json_decode(
  61. file_get_contents($path . '/composer.json'),
  62. true
  63. ))) {
  64. if (isset($cfg['name']) && $cfg['name'] === 'psy/psysh') {
  65. // We're inside the psysh project. Let's use the local
  66. // Composer autoload.
  67. if (is_file($path . '/vendor/autoload.php')) {
  68. require $path . '/vendor/autoload.php';
  69. }
  70. return;
  71. }
  72. }
  73. }
  74. // Or a composer.lock
  75. if (is_file($path . '/composer.lock')) {
  76. if (!empty($cfg = json_decode(
  77. file_get_contents($path . '/composer.lock'),
  78. true
  79. ))) {
  80. foreach (array_merge($cfg['packages'], $cfg['packages-dev']) as $pkg) {
  81. if (isset($pkg['name']) && $pkg['name'] === 'psy/psysh') {
  82. // We're inside a project which requires psysh. We'll
  83. // use the local Composer autoload.
  84. if (is_file($path . '/vendor/autoload.php')) {
  85. require $path . '/vendor/autoload.php';
  86. }
  87. return;
  88. }
  89. }
  90. }
  91. }
  92. array_pop($chunks);
  93. }
  94. });
  95. // We didn't find an autoloader for a local version, so use the autoloader that
  96. // came with this script.
  97. if (!class_exists('Psy\Shell')) {
  98. /* <<< */
  99. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  100. require __DIR__ . '/../vendor/autoload.php';
  101. } elseif (is_file(__DIR__ . '/../../../autoload.php')) {
  102. require __DIR__ . '/../../../autoload.php';
  103. } else {
  104. echo 'PsySH dependencies not found, be sure to run `composer install`.' . PHP_EOL;
  105. echo 'See https://getcomposer.org to get Composer.' . PHP_EOL;
  106. exit(1);
  107. }
  108. /* >>> */
  109. }
  110. // If the psysh binary was included directly, assume they just wanted an
  111. // autoloader and bail early.
  112. if (version_compare(PHP_VERSION, '5.3.6', '<')) {
  113. $trace = debug_backtrace();
  114. } elseif (version_compare(PHP_VERSION, '5.4.0', '<')) {
  115. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  116. } else {
  117. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
  118. }
  119. if (Psy\Shell::isIncluded($trace)) {
  120. unset($trace);
  121. return;
  122. }
  123. // Clean up after ourselves.
  124. unset($trace);
  125. // If the local version is too old, we can't do this
  126. if (!function_exists('Psy\bin')) {
  127. $argv = $_SERVER['argv'];
  128. $first = array_shift($argv);
  129. if (preg_match('/php(\.exe)?$/', $first)) {
  130. array_shift($argv);
  131. }
  132. array_unshift($argv, 'vendor/bin/psysh');
  133. echo 'A local PsySH dependency was found, but it cannot be loaded. Please update to' . PHP_EOL;
  134. echo 'the latest version, or run the local copy directly, e.g.:' . PHP_EOL;
  135. echo PHP_EOL;
  136. echo ' ' . implode(' ', $argv) . PHP_EOL;
  137. exit(1);
  138. }
  139. // And go!
  140. call_user_func(Psy\bin());