Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy;
|
Chris@13
|
13
|
Chris@13
|
14 use Psy\VersionUpdater\GitHubChecker;
|
Chris@13
|
15 use Symfony\Component\Console\Input\ArgvInput;
|
Chris@13
|
16 use Symfony\Component\Console\Input\InputArgument;
|
Chris@13
|
17 use Symfony\Component\Console\Input\InputDefinition;
|
Chris@13
|
18 use Symfony\Component\Console\Input\InputOption;
|
Chris@13
|
19 use XdgBaseDir\Xdg;
|
Chris@13
|
20
|
Chris@13
|
21 if (!function_exists('Psy\sh')) {
|
Chris@13
|
22 /**
|
Chris@13
|
23 * Command to return the eval-able code to startup PsySH.
|
Chris@13
|
24 *
|
Chris@13
|
25 * eval(\Psy\sh());
|
Chris@13
|
26 *
|
Chris@13
|
27 * @return string
|
Chris@13
|
28 */
|
Chris@13
|
29 function sh()
|
Chris@13
|
30 {
|
Chris@13
|
31 return 'extract(\Psy\debug(get_defined_vars(), isset($this) ? $this : null));';
|
Chris@13
|
32 }
|
Chris@13
|
33 }
|
Chris@13
|
34
|
Chris@13
|
35 if (!function_exists('Psy\debug')) {
|
Chris@13
|
36 /**
|
Chris@13
|
37 * Invoke a Psy Shell from the current context.
|
Chris@13
|
38 *
|
Chris@13
|
39 * For example:
|
Chris@13
|
40 *
|
Chris@13
|
41 * foreach ($items as $item) {
|
Chris@13
|
42 * \Psy\debug(get_defined_vars());
|
Chris@13
|
43 * }
|
Chris@13
|
44 *
|
Chris@13
|
45 * If you would like your shell interaction to affect the state of the
|
Chris@13
|
46 * current context, you can extract() the values returned from this call:
|
Chris@13
|
47 *
|
Chris@13
|
48 * foreach ($items as $item) {
|
Chris@13
|
49 * extract(\Psy\debug(get_defined_vars()));
|
Chris@13
|
50 * var_dump($item); // will be whatever you set $item to in Psy Shell
|
Chris@13
|
51 * }
|
Chris@13
|
52 *
|
Chris@13
|
53 * Optionally, supply an object as the `$boundObject` parameter. This
|
Chris@13
|
54 * determines the value `$this` will have in the shell, and sets up class
|
Chris@13
|
55 * scope so that private and protected members are accessible:
|
Chris@13
|
56 *
|
Chris@13
|
57 * class Foo {
|
Chris@13
|
58 * function bar() {
|
Chris@13
|
59 * \Psy\debug(get_defined_vars(), $this);
|
Chris@13
|
60 * }
|
Chris@13
|
61 * }
|
Chris@13
|
62 *
|
Chris@13
|
63 * @param array $vars Scope variables from the calling context (default: array())
|
Chris@13
|
64 * @param object $boundObject Bound object ($this) value for the shell
|
Chris@13
|
65 *
|
Chris@13
|
66 * @return array Scope variables from the debugger session
|
Chris@13
|
67 */
|
Chris@13
|
68 function debug(array $vars = [], $boundObject = null)
|
Chris@13
|
69 {
|
Chris@13
|
70 echo PHP_EOL;
|
Chris@13
|
71
|
Chris@13
|
72 $sh = new Shell();
|
Chris@13
|
73 $sh->setScopeVariables($vars);
|
Chris@13
|
74
|
Chris@13
|
75 // Show a couple of lines of call context for the debug session.
|
Chris@13
|
76 //
|
Chris@13
|
77 // @todo come up with a better way of doing this which doesn't involve injecting input :-P
|
Chris@13
|
78 if ($sh->has('whereami')) {
|
Chris@13
|
79 $sh->addInput('whereami -n2', true);
|
Chris@13
|
80 }
|
Chris@13
|
81
|
Chris@13
|
82 if ($boundObject !== null) {
|
Chris@13
|
83 $sh->setBoundObject($boundObject);
|
Chris@13
|
84 }
|
Chris@13
|
85
|
Chris@13
|
86 $sh->run();
|
Chris@13
|
87
|
Chris@13
|
88 return $sh->getScopeVariables(false);
|
Chris@13
|
89 }
|
Chris@13
|
90 }
|
Chris@13
|
91
|
Chris@13
|
92 if (!function_exists('Psy\info')) {
|
Chris@13
|
93 /**
|
Chris@13
|
94 * Get a bunch of debugging info about the current PsySH environment and
|
Chris@13
|
95 * configuration.
|
Chris@13
|
96 *
|
Chris@13
|
97 * If a Configuration param is passed, that configuration is stored and
|
Chris@13
|
98 * used for the current shell session, and no debugging info is returned.
|
Chris@13
|
99 *
|
Chris@13
|
100 * @param Configuration|null $config
|
Chris@13
|
101 *
|
Chris@13
|
102 * @return array|null
|
Chris@13
|
103 */
|
Chris@13
|
104 function info(Configuration $config = null)
|
Chris@13
|
105 {
|
Chris@13
|
106 static $lastConfig;
|
Chris@13
|
107 if ($config !== null) {
|
Chris@13
|
108 $lastConfig = $config;
|
Chris@13
|
109
|
Chris@13
|
110 return;
|
Chris@13
|
111 }
|
Chris@13
|
112
|
Chris@13
|
113 $xdg = new Xdg();
|
Chris@13
|
114 $home = rtrim(str_replace('\\', '/', $xdg->getHomeDir()), '/');
|
Chris@13
|
115 $homePattern = '#^' . preg_quote($home, '#') . '/#';
|
Chris@13
|
116
|
Chris@13
|
117 $prettyPath = function ($path) use ($homePattern) {
|
Chris@13
|
118 if (is_string($path)) {
|
Chris@13
|
119 return preg_replace($homePattern, '~/', $path);
|
Chris@13
|
120 } else {
|
Chris@13
|
121 return $path;
|
Chris@13
|
122 }
|
Chris@13
|
123 };
|
Chris@13
|
124
|
Chris@13
|
125 $config = $lastConfig ?: new Configuration();
|
Chris@13
|
126
|
Chris@13
|
127 $core = [
|
Chris@13
|
128 'PsySH version' => Shell::VERSION,
|
Chris@13
|
129 'PHP version' => PHP_VERSION,
|
Chris@13
|
130 'OS' => PHP_OS,
|
Chris@13
|
131 'default includes' => $config->getDefaultIncludes(),
|
Chris@13
|
132 'require semicolons' => $config->requireSemicolons(),
|
Chris@13
|
133 'error logging level' => $config->errorLoggingLevel(),
|
Chris@13
|
134 'config file' => [
|
Chris@13
|
135 'default config file' => $prettyPath($config->getConfigFile()),
|
Chris@13
|
136 'local config file' => $prettyPath($config->getLocalConfigFile()),
|
Chris@13
|
137 'PSYSH_CONFIG env' => $prettyPath(getenv('PSYSH_CONFIG')),
|
Chris@13
|
138 ],
|
Chris@13
|
139 // 'config dir' => $config->getConfigDir(),
|
Chris@13
|
140 // 'data dir' => $config->getDataDir(),
|
Chris@13
|
141 // 'runtime dir' => $config->getRuntimeDir(),
|
Chris@13
|
142 ];
|
Chris@13
|
143
|
Chris@13
|
144 // Use an explicit, fresh update check here, rather than relying on whatever is in $config.
|
Chris@13
|
145 $checker = new GitHubChecker();
|
Chris@13
|
146 $updateAvailable = null;
|
Chris@13
|
147 $latest = null;
|
Chris@13
|
148 try {
|
Chris@13
|
149 $updateAvailable = !$checker->isLatest();
|
Chris@13
|
150 $latest = $checker->getLatest();
|
Chris@13
|
151 } catch (\Exception $e) {
|
Chris@13
|
152 }
|
Chris@13
|
153
|
Chris@13
|
154 $updates = [
|
Chris@13
|
155 'update available' => $updateAvailable,
|
Chris@13
|
156 'latest release version' => $latest,
|
Chris@13
|
157 'update check interval' => $config->getUpdateCheck(),
|
Chris@13
|
158 'update cache file' => $prettyPath($config->getUpdateCheckCacheFile()),
|
Chris@13
|
159 ];
|
Chris@13
|
160
|
Chris@13
|
161 if ($config->hasReadline()) {
|
Chris@13
|
162 $info = readline_info();
|
Chris@13
|
163
|
Chris@13
|
164 $readline = [
|
Chris@13
|
165 'readline available' => true,
|
Chris@13
|
166 'readline enabled' => $config->useReadline(),
|
Chris@13
|
167 'readline service' => get_class($config->getReadline()),
|
Chris@13
|
168 ];
|
Chris@13
|
169
|
Chris@13
|
170 if (isset($info['library_version'])) {
|
Chris@13
|
171 $readline['readline library'] = $info['library_version'];
|
Chris@13
|
172 }
|
Chris@13
|
173
|
Chris@13
|
174 if (isset($info['readline_name']) && $info['readline_name'] !== '') {
|
Chris@13
|
175 $readline['readline name'] = $info['readline_name'];
|
Chris@13
|
176 }
|
Chris@13
|
177 } else {
|
Chris@13
|
178 $readline = [
|
Chris@13
|
179 'readline available' => false,
|
Chris@13
|
180 ];
|
Chris@13
|
181 }
|
Chris@13
|
182
|
Chris@13
|
183 $pcntl = [
|
Chris@13
|
184 'pcntl available' => function_exists('pcntl_signal'),
|
Chris@13
|
185 'posix available' => function_exists('posix_getpid'),
|
Chris@13
|
186 ];
|
Chris@13
|
187
|
Chris@13
|
188 $disabledFuncs = array_map('trim', explode(',', ini_get('disable_functions')));
|
Chris@13
|
189 if (in_array('pcntl_signal', $disabledFuncs) || in_array('pcntl_fork', $disabledFuncs)) {
|
Chris@13
|
190 $pcntl['pcntl disabled'] = true;
|
Chris@13
|
191 }
|
Chris@13
|
192
|
Chris@13
|
193 $history = [
|
Chris@13
|
194 'history file' => $prettyPath($config->getHistoryFile()),
|
Chris@13
|
195 'history size' => $config->getHistorySize(),
|
Chris@13
|
196 'erase duplicates' => $config->getEraseDuplicates(),
|
Chris@13
|
197 ];
|
Chris@13
|
198
|
Chris@13
|
199 $docs = [
|
Chris@13
|
200 'manual db file' => $prettyPath($config->getManualDbFile()),
|
Chris@13
|
201 'sqlite available' => true,
|
Chris@13
|
202 ];
|
Chris@13
|
203
|
Chris@13
|
204 try {
|
Chris@13
|
205 if ($db = $config->getManualDb()) {
|
Chris@13
|
206 if ($q = $db->query('SELECT * FROM meta;')) {
|
Chris@13
|
207 $q->setFetchMode(\PDO::FETCH_KEY_PAIR);
|
Chris@13
|
208 $meta = $q->fetchAll();
|
Chris@13
|
209
|
Chris@13
|
210 foreach ($meta as $key => $val) {
|
Chris@13
|
211 switch ($key) {
|
Chris@13
|
212 case 'built_at':
|
Chris@13
|
213 $d = new \DateTime('@' . $val);
|
Chris@13
|
214 $val = $d->format(\DateTime::RFC2822);
|
Chris@13
|
215 break;
|
Chris@13
|
216 }
|
Chris@13
|
217 $key = 'db ' . str_replace('_', ' ', $key);
|
Chris@13
|
218 $docs[$key] = $val;
|
Chris@13
|
219 }
|
Chris@13
|
220 } else {
|
Chris@13
|
221 $docs['db schema'] = '0.1.0';
|
Chris@13
|
222 }
|
Chris@13
|
223 }
|
Chris@13
|
224 } catch (Exception\RuntimeException $e) {
|
Chris@13
|
225 if ($e->getMessage() === 'SQLite PDO driver not found') {
|
Chris@13
|
226 $docs['sqlite available'] = false;
|
Chris@13
|
227 } else {
|
Chris@13
|
228 throw $e;
|
Chris@13
|
229 }
|
Chris@13
|
230 }
|
Chris@13
|
231
|
Chris@13
|
232 $autocomplete = [
|
Chris@13
|
233 'tab completion enabled' => $config->useTabCompletion(),
|
Chris@13
|
234 'custom matchers' => array_map('get_class', $config->getTabCompletionMatchers()),
|
Chris@13
|
235 'bracketed paste' => $config->useBracketedPaste(),
|
Chris@13
|
236 ];
|
Chris@13
|
237
|
Chris@13
|
238 // Shenanigans, but totally justified.
|
Chris@13
|
239 if ($shell = Sudo::fetchProperty($config, 'shell')) {
|
Chris@13
|
240 $core['loop listeners'] = array_map('get_class', Sudo::fetchProperty($shell, 'loopListeners'));
|
Chris@13
|
241 $core['commands'] = array_map('get_class', $shell->all());
|
Chris@13
|
242
|
Chris@13
|
243 $autocomplete['custom matchers'] = array_map('get_class', Sudo::fetchProperty($shell, 'matchers'));
|
Chris@13
|
244 }
|
Chris@13
|
245
|
Chris@13
|
246 // @todo Show Presenter / custom casters.
|
Chris@13
|
247
|
Chris@13
|
248 return array_merge($core, compact('updates', 'pcntl', 'readline', 'history', 'docs', 'autocomplete'));
|
Chris@13
|
249 }
|
Chris@13
|
250 }
|
Chris@13
|
251
|
Chris@13
|
252 if (!function_exists('Psy\bin')) {
|
Chris@13
|
253 /**
|
Chris@13
|
254 * `psysh` command line executable.
|
Chris@13
|
255 *
|
Chris@13
|
256 * @return \Closure
|
Chris@13
|
257 */
|
Chris@13
|
258 function bin()
|
Chris@13
|
259 {
|
Chris@13
|
260 return function () {
|
Chris@13
|
261 $usageException = null;
|
Chris@13
|
262
|
Chris@13
|
263 $input = new ArgvInput();
|
Chris@13
|
264 try {
|
Chris@13
|
265 $input->bind(new InputDefinition([
|
Chris@13
|
266 new InputOption('help', 'h', InputOption::VALUE_NONE),
|
Chris@13
|
267 new InputOption('config', 'c', InputOption::VALUE_REQUIRED),
|
Chris@13
|
268 new InputOption('version', 'v', InputOption::VALUE_NONE),
|
Chris@13
|
269 new InputOption('cwd', null, InputOption::VALUE_REQUIRED),
|
Chris@13
|
270 new InputOption('color', null, InputOption::VALUE_NONE),
|
Chris@13
|
271 new InputOption('no-color', null, InputOption::VALUE_NONE),
|
Chris@13
|
272
|
Chris@13
|
273 new InputArgument('include', InputArgument::IS_ARRAY),
|
Chris@13
|
274 ]));
|
Chris@13
|
275 } catch (\RuntimeException $e) {
|
Chris@13
|
276 $usageException = $e;
|
Chris@13
|
277 }
|
Chris@13
|
278
|
Chris@13
|
279 $config = [];
|
Chris@13
|
280
|
Chris@13
|
281 // Handle --config
|
Chris@13
|
282 if ($configFile = $input->getOption('config')) {
|
Chris@13
|
283 $config['configFile'] = $configFile;
|
Chris@13
|
284 }
|
Chris@13
|
285
|
Chris@13
|
286 // Handle --color and --no-color
|
Chris@13
|
287 if ($input->getOption('color') && $input->getOption('no-color')) {
|
Chris@13
|
288 $usageException = new \RuntimeException('Using both "--color" and "--no-color" options is invalid');
|
Chris@13
|
289 } elseif ($input->getOption('color')) {
|
Chris@13
|
290 $config['colorMode'] = Configuration::COLOR_MODE_FORCED;
|
Chris@13
|
291 } elseif ($input->getOption('no-color')) {
|
Chris@13
|
292 $config['colorMode'] = Configuration::COLOR_MODE_DISABLED;
|
Chris@13
|
293 }
|
Chris@13
|
294
|
Chris@13
|
295 $shell = new Shell(new Configuration($config));
|
Chris@13
|
296
|
Chris@13
|
297 // Handle --help
|
Chris@13
|
298 if ($usageException !== null || $input->getOption('help')) {
|
Chris@13
|
299 if ($usageException !== null) {
|
Chris@13
|
300 echo $usageException->getMessage() . PHP_EOL . PHP_EOL;
|
Chris@13
|
301 }
|
Chris@13
|
302
|
Chris@13
|
303 $version = $shell->getVersion();
|
Chris@13
|
304 $name = basename(reset($_SERVER['argv']));
|
Chris@13
|
305 echo <<<EOL
|
Chris@13
|
306 $version
|
Chris@13
|
307
|
Chris@13
|
308 Usage:
|
Chris@13
|
309 $name [--version] [--help] [files...]
|
Chris@13
|
310
|
Chris@13
|
311 Options:
|
Chris@13
|
312 --help -h Display this help message.
|
Chris@13
|
313 --config -c Use an alternate PsySH config file location.
|
Chris@13
|
314 --cwd Use an alternate working directory.
|
Chris@13
|
315 --version -v Display the PsySH version.
|
Chris@13
|
316 --color Force colors in output.
|
Chris@13
|
317 --no-color Disable colors in output.
|
Chris@13
|
318
|
Chris@13
|
319 EOL;
|
Chris@13
|
320 exit($usageException === null ? 0 : 1);
|
Chris@13
|
321 }
|
Chris@13
|
322
|
Chris@13
|
323 // Handle --version
|
Chris@13
|
324 if ($input->getOption('version')) {
|
Chris@13
|
325 echo $shell->getVersion() . PHP_EOL;
|
Chris@13
|
326 exit(0);
|
Chris@13
|
327 }
|
Chris@13
|
328
|
Chris@13
|
329 // Pass additional arguments to Shell as 'includes'
|
Chris@13
|
330 $shell->setIncludes($input->getArgument('include'));
|
Chris@13
|
331
|
Chris@13
|
332 try {
|
Chris@13
|
333 // And go!
|
Chris@13
|
334 $shell->run();
|
Chris@13
|
335 } catch (\Exception $e) {
|
Chris@13
|
336 echo $e->getMessage() . PHP_EOL;
|
Chris@13
|
337
|
Chris@13
|
338 // @todo this triggers the "exited unexpectedly" logic in the
|
Chris@13
|
339 // ForkingLoop, so we can't exit(1) after starting the shell...
|
Chris@13
|
340 // fix this :)
|
Chris@13
|
341
|
Chris@13
|
342 // exit(1);
|
Chris@13
|
343 }
|
Chris@13
|
344 };
|
Chris@13
|
345 }
|
Chris@13
|
346 }
|