]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/console.php
Make attachment fit better in notice: drop text and link
[quix0rs-gnu-social.git] / scripts / console.php
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 /**
19  * Description of this file.
20  *
21  * @package   samples
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
25  */
26
27 define('INSTALLDIR', dirname(__DIR__));
28 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
29 define('GNUSOCIAL', true);
30 define('STATUSNET', true);
31
32 require_once INSTALLDIR . '/lib/common.php';
33
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 () {
37     $cwd = null;
38
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;
45                 exit(1);
46             }
47             $cwd = $argv[$i + 1];
48             break;
49         }
50
51         if (preg_match('/^--cwd=/', $arg)) {
52             $cwd = substr($arg, 6);
53             break;
54         }
55     }
56
57     // Or fall back to the actual cwd
58     if (!isset($cwd)) {
59         $cwd = getcwd();
60     }
61
62     $cwd = str_replace('\\', '/', $cwd);
63
64     $chunks = explode('/', $cwd);
65     while (!empty($chunks)) {
66         $path = implode('/', $chunks);
67
68         // Find composer.json
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
73                     // Composer autoload.
74                     if (is_file($path . '/vendor/autoload.php')) {
75                         require $path . '/vendor/autoload.php';
76                     }
77
78                     return;
79                 }
80             }
81         }
82
83         // Or a composer.lock
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';
92                         }
93
94                         return;
95                     }
96                 }
97             }
98         }
99
100         array_pop($chunks);
101     }
102 });
103
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')) {
107     /* <<< */
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';
112     } else {
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;
115         exit(1);
116     }
117     /* >>> */
118 }
119
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);
126 } else {
127     $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
128 }
129
130 if (Psy\Shell::isIncluded($trace)) {
131     unset($trace);
132
133     return;
134 }
135
136 // Clean up after ourselves.
137 unset($trace);
138
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)) {
144         array_shift($argv);
145     }
146     array_unshift($argv, 'vendor/bin/psysh');
147
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;
150     echo PHP_EOL;
151     echo '    ' . implode(' ', $argv) . PHP_EOL;
152     exit(1);
153 }
154
155 // And go!
156 call_user_func(Psy\bin());