annotate vendor/psy/psysh/src/functions.php @ 19:fa3358dc1485 tip

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