comparison vendor/psy/psysh/src/Psy/functions.php @ 0:4c8ae668cc8c

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