3 // This file is part of GNU social - https://www.gnu.org/software/social
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.
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.
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/>.
19 * Description of this file.
22 * @author Diogo Cordeiro <diogo@fc.up.pt>
23 * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
24 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
27 define('INSTALLDIR', dirname(__DIR__));
28 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
29 define('GNUSOCIAL', true);
30 define('STATUSNET', true);
32 require_once INSTALLDIR . '/lib/common.php';
34 // Try to find an autoloader for a local psysh version.
35 // We'll wrap this whole mess in a Closure so it doesn't leak any globals.
36 call_user_func(function () {
39 // Find the cwd arg (if present)
40 $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
41 foreach ($argv as $i => $arg) {
42 if ($arg === '--cwd') {
43 if ($i >= count($argv) - 1) {
44 echo 'Missing --cwd argument.' . PHP_EOL;
51 if (preg_match('/^--cwd=/', $arg)) {
52 $cwd = substr($arg, 6);
57 // Or fall back to the actual cwd
62 $cwd = str_replace('\\', '/', $cwd);
64 $chunks = explode('/', $cwd);
65 while (!empty($chunks)) {
66 $path = implode('/', $chunks);
69 if (is_file($path . '/composer.json')) {
70 if ($cfg = json_decode(file_get_contents($path . '/composer.json'), true)) {
71 if (isset($cfg['name']) && $cfg['name'] === 'psy/psysh') {
72 // We're inside the psysh project. Let's use the local
74 if (is_file($path . '/vendor/autoload.php')) {
75 require $path . '/vendor/autoload.php';
84 if (is_file($path . '/composer.lock')) {
85 if ($cfg = json_decode(file_get_contents($path . '/composer.lock'), true)) {
86 foreach (array_merge($cfg['packages'], $cfg['packages-dev']) as $pkg) {
87 if (isset($pkg['name']) && $pkg['name'] === 'psy/psysh') {
88 // We're inside a project which requires psysh. We'll
89 // use the local Composer autoload.
90 if (is_file($path . '/vendor/autoload.php')) {
91 require $path . '/vendor/autoload.php';
104 // We didn't find an autoloader for a local version, so use the autoloader that
105 // came with this script.
106 if (!class_exists('Psy\Shell')) {
108 if (is_file(__DIR__ . '/../vendor/autoload.php')) {
109 require __DIR__ . '/../vendor/autoload.php';
110 } elseif (is_file(__DIR__ . '/../../../autoload.php')) {
111 require __DIR__ . '/../../../autoload.php';
113 echo 'PsySH dependencies not found, be sure to run `composer install`.' . PHP_EOL;
114 echo 'See https://getcomposer.org to get Composer.' . PHP_EOL;
120 // If the psysh binary was included directly, assume they just wanted an
121 // autoloader and bail early.
122 if (version_compare(PHP_VERSION, '5.3.6', '<')) {
123 $trace = debug_backtrace();
124 } elseif (version_compare(PHP_VERSION, '5.4.0', '<')) {
125 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
127 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
130 if (Psy\Shell::isIncluded($trace)) {
136 // Clean up after ourselves.
139 // If the local version is too old, we can't do this
140 if (!function_exists('Psy\bin')) {
141 $argv = $_SERVER['argv'];
142 $first = array_shift($argv);
143 if (preg_match('/php(\.exe)?$/', $first)) {
146 array_unshift($argv, 'vendor/bin/psysh');
148 echo 'A local PsySH dependency was found, but it cannot be loaded. Please update to' . PHP_EOL;
149 echo 'the latest version, or run the local copy directly, e.g.:' . PHP_EOL;
151 echo ' ' . implode(' ', $argv) . PHP_EOL;
156 call_user_func(Psy\bin());