annotate vendor/psy/psysh/src/Psy/functions.php @ 7:848c88cfe644

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