console.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/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 ($cfg = json_decode(file_get_contents($path . '/composer.json'), true)) {
  61. if (isset($cfg['name']) && $cfg['name'] === 'psy/psysh') {
  62. // We're inside the psysh project. Let's use the local
  63. // Composer autoload.
  64. if (is_file($path . '/vendor/autoload.php')) {
  65. require $path . '/vendor/autoload.php';
  66. }
  67. return;
  68. }
  69. }
  70. }
  71. // Or a composer.lock
  72. if (is_file($path . '/composer.lock')) {
  73. if ($cfg = json_decode(file_get_contents($path . '/composer.lock'), true)) {
  74. foreach (array_merge($cfg['packages'], $cfg['packages-dev']) as $pkg) {
  75. if (isset($pkg['name']) && $pkg['name'] === 'psy/psysh') {
  76. // We're inside a project which requires psysh. We'll
  77. // use the local Composer autoload.
  78. if (is_file($path . '/vendor/autoload.php')) {
  79. require $path . '/vendor/autoload.php';
  80. }
  81. return;
  82. }
  83. }
  84. }
  85. }
  86. array_pop($chunks);
  87. }
  88. });
  89. // We didn't find an autoloader for a local version, so use the autoloader that
  90. // came with this script.
  91. if (!class_exists('Psy\Shell')) {
  92. /* <<< */
  93. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  94. require __DIR__ . '/../vendor/autoload.php';
  95. } elseif (is_file(__DIR__ . '/../../../autoload.php')) {
  96. require __DIR__ . '/../../../autoload.php';
  97. } else {
  98. echo 'PsySH dependencies not found, be sure to run `composer install`.' . PHP_EOL;
  99. echo 'See https://getcomposer.org to get Composer.' . PHP_EOL;
  100. exit(1);
  101. }
  102. /* >>> */
  103. }
  104. // If the psysh binary was included directly, assume they just wanted an
  105. // autoloader and bail early.
  106. if (version_compare(PHP_VERSION, '5.3.6', '<')) {
  107. $trace = debug_backtrace();
  108. } elseif (version_compare(PHP_VERSION, '5.4.0', '<')) {
  109. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  110. } else {
  111. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
  112. }
  113. if (Psy\Shell::isIncluded($trace)) {
  114. unset($trace);
  115. return;
  116. }
  117. // Clean up after ourselves.
  118. unset($trace);
  119. // If the local version is too old, we can't do this
  120. if (!function_exists('Psy\bin')) {
  121. $argv = $_SERVER['argv'];
  122. $first = array_shift($argv);
  123. if (preg_match('/php(\.exe)?$/', $first)) {
  124. array_shift($argv);
  125. }
  126. array_unshift($argv, 'vendor/bin/psysh');
  127. echo 'A local PsySH dependency was found, but it cannot be loaded. Please update to' . PHP_EOL;
  128. echo 'the latest version, or run the local copy directly, e.g.:' . PHP_EOL;
  129. echo PHP_EOL;
  130. echo ' ' . implode(' ', $argv) . PHP_EOL;
  131. exit(1);
  132. }
  133. // And go!
  134. call_user_func(Psy\bin());