4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
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;
21 if (!function_exists('Psy\sh')) {
23 * Command to return the eval-able code to startup PsySH.
31 return 'extract(\Psy\debug(get_defined_vars(), isset($this) ? $this : null));';
35 if (!function_exists('Psy\debug')) {
37 * Invoke a Psy Shell from the current context.
41 * foreach ($items as $item) {
42 * \Psy\debug(get_defined_vars());
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:
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
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:
59 * \Psy\debug(get_defined_vars(), $this);
63 * This only really works in PHP 5.4+ and HHVM 3.5+, so upgrade already.
65 * @param array $vars Scope variables from the calling context (default: array())
66 * @param object $boundObject Bound object ($this) value for the shell
68 * @return array Scope variables from the debugger session
70 function debug(array $vars = array(), $boundObject = null)
75 $sh->setScopeVariables($vars);
77 // Show a couple of lines of call context for the debug session.
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);
84 if ($boundObject !== null) {
85 $sh->setBoundObject($boundObject);
90 return $sh->getScopeVariables(false);
94 if (!function_exists('Psy\info')) {
96 * Get a bunch of debugging info about the current PsySH environment and
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.
102 * @param Configuration|null $config
106 function info(Configuration $config = null)
109 if ($config !== null) {
110 $lastConfig = $config;
116 $home = rtrim(str_replace('\\', '/', $xdg->getHomeDir()), '/');
117 $homePattern = '#^' . preg_quote($home, '#') . '/#';
119 $prettyPath = function ($path) use ($homePattern) {
120 if (is_string($path)) {
121 return preg_replace($homePattern, '~/', $path);
127 $config = $lastConfig ?: new Configuration();
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')),
140 // 'config dir' => $config->getConfigDir(),
141 // 'data dir' => $config->getDataDir(),
142 // 'runtime dir' => $config->getRuntimeDir(),
145 // Use an explicit, fresh update check here, rather than relying on whatever is in $config.
146 $checker = new GitHubChecker();
148 'update available' => !$checker->isLatest(),
149 'latest release version' => $checker->getLatest(),
150 'update check interval' => $config->getUpdateCheck(),
151 'update cache file' => $prettyPath($config->getUpdateCheckCacheFile()),
154 if ($config->hasReadline()) {
155 $info = readline_info();
158 'readline available' => true,
159 'readline enabled' => $config->useReadline(),
160 'readline service' => get_class($config->getReadline()),
163 if (isset($info['library_version'])) {
164 $readline['readline library'] = $info['library_version'];
167 if (isset($info['readline_name']) && $info['readline_name'] !== '') {
168 $readline['readline name'] = $info['readline_name'];
172 'readline available' => false,
177 'pcntl available' => function_exists('pcntl_signal'),
178 'posix available' => function_exists('posix_getpid'),
182 'history file' => $prettyPath($config->getHistoryFile()),
183 'history size' => $config->getHistorySize(),
184 'erase duplicates' => $config->getEraseDuplicates(),
188 'manual db file' => $prettyPath($config->getManualDbFile()),
189 'sqlite available' => true,
193 if ($db = $config->getManualDb()) {
194 if ($q = $db->query('SELECT * FROM meta;')) {
195 $q->setFetchMode(\PDO::FETCH_KEY_PAIR);
196 $meta = $q->fetchAll();
198 foreach ($meta as $key => $val) {
201 $d = new \DateTime('@' . $val);
202 $val = $d->format(\DateTime::RFC2822);
205 $key = 'db ' . str_replace('_', ' ', $key);
209 $docs['db schema'] = '0.1.0';
212 } catch (Exception\RuntimeException $e) {
213 if ($e->getMessage() === 'SQLite PDO driver not found') {
214 $docs['sqlite available'] = false;
220 $autocomplete = array(
221 'tab completion enabled' => $config->getTabCompletion(),
222 'custom matchers' => array_map('get_class', $config->getTabCompletionMatchers()),
225 return array_merge($core, compact('updates', 'pcntl', 'readline', 'history', 'docs', 'autocomplete'));
229 if (!function_exists('Psy\bin')) {
231 * `psysh` command line executable.
238 $usageException = null;
240 $input = new ArgvInput();
242 $input->bind(new InputDefinition(array(
243 new InputOption('help', 'h', InputOption::VALUE_NONE),
244 new InputOption('config', 'c', InputOption::VALUE_REQUIRED),
245 new InputOption('version', 'v', InputOption::VALUE_NONE),
246 new InputOption('cwd', null, InputOption::VALUE_REQUIRED),
247 new InputOption('color', null, InputOption::VALUE_NONE),
248 new InputOption('no-color', null, InputOption::VALUE_NONE),
250 new InputArgument('include', InputArgument::IS_ARRAY),
252 } catch (\RuntimeException $e) {
253 $usageException = $e;
259 if ($configFile = $input->getOption('config')) {
260 $config['configFile'] = $configFile;
263 // Handle --color and --no-color
264 if ($input->getOption('color') && $input->getOption('no-color')) {
265 $usageException = new \RuntimeException('Using both "--color" and "--no-color" options is invalid.');
266 } elseif ($input->getOption('color')) {
267 $config['colorMode'] = Configuration::COLOR_MODE_FORCED;
268 } elseif ($input->getOption('no-color')) {
269 $config['colorMode'] = Configuration::COLOR_MODE_DISABLED;
272 $shell = new Shell(new Configuration($config));
275 if ($usageException !== null || $input->getOption('help')) {
276 if ($usageException !== null) {
277 echo $usageException->getMessage() . PHP_EOL . PHP_EOL;
280 $version = $shell->getVersion();
281 $name = basename(reset($_SERVER['argv']));
286 $name [--version] [--help] [files...]
289 --help -h Display this help message.
290 --config -c Use an alternate PsySH config file location.
291 --cwd Use an alternate working directory.
292 --version -v Display the PsySH version.
293 --color Force colors in output.
294 --no-color Disable colors in output.
297 exit($usageException === null ? 0 : 1);
301 if ($input->getOption('version')) {
302 echo $shell->getVersion() . PHP_EOL;
306 // Pass additional arguments to Shell as 'includes'
307 $shell->setIncludes($input->getArgument('include'));
312 } catch (Exception $e) {
313 echo $e->getMessage() . PHP_EOL;
315 // @todo this triggers the "exited unexpectedly" logic in the
316 // ForkingLoop, so we can't exit(1) after starting the shell...