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\Shell::debug(get_defined_vars(), isset($this) ? $this : null));';
35 if (!function_exists('Psy\info')) {
37 * Get a bunch of debugging info about the current PsySH environment and
40 * If a Configuration param is passed, that configuration is stored and
41 * used for the current shell session, and no debugging info is returned.
43 * @param Configuration|null $config
47 function info(Configuration $config = null)
50 if ($config !== null) {
51 $lastConfig = $config;
57 $home = rtrim(str_replace('\\', '/', $xdg->getHomeDir()), '/');
58 $homePattern = '#^' . preg_quote($home, '#') . '/#';
60 $prettyPath = function ($path) use ($homePattern) {
61 if (is_string($path)) {
62 return preg_replace($homePattern, '~/', $path);
68 $config = $lastConfig ?: new Configuration();
71 'PsySH version' => Shell::VERSION,
72 'PHP version' => PHP_VERSION,
73 'default includes' => $config->getDefaultIncludes(),
74 'require semicolons' => $config->requireSemicolons(),
75 'error logging level' => $config->errorLoggingLevel(),
76 'config file' => array(
77 'default config file' => $prettyPath($config->getConfigFile()),
78 'local config file' => $prettyPath($config->getLocalConfigFile()),
79 'PSYSH_CONFIG env' => $prettyPath(getenv('PSYSH_CONFIG')),
81 // 'config dir' => $config->getConfigDir(),
82 // 'data dir' => $config->getDataDir(),
83 // 'runtime dir' => $config->getRuntimeDir(),
86 // Use an explicit, fresh update check here, rather than relying on whatever is in $config.
87 $checker = new GitHubChecker();
89 'update available' => !$checker->isLatest(),
90 'latest release version' => $checker->getLatest(),
91 'update check interval' => $config->getUpdateCheck(),
92 'update cache file' => $prettyPath($config->getUpdateCheckCacheFile()),
95 if ($config->hasReadline()) {
96 $info = readline_info();
99 'readline available' => true,
100 'readline enabled' => $config->useReadline(),
101 'readline service' => get_class($config->getReadline()),
104 if (isset($info['library_version'])) {
105 $readline['readline library'] = $info['library_version'];
108 if (isset($info['readline_name']) && $info['readline_name'] !== '') {
109 $readline['readline name'] = $info['readline_name'];
113 'readline available' => false,
118 'pcntl available' => function_exists('pcntl_signal'),
119 'posix available' => function_exists('posix_getpid'),
123 'history file' => $prettyPath($config->getHistoryFile()),
124 'history size' => $config->getHistorySize(),
125 'erase duplicates' => $config->getEraseDuplicates(),
129 'manual db file' => $prettyPath($config->getManualDbFile()),
130 'sqlite available' => true,
134 if ($db = $config->getManualDb()) {
135 if ($q = $db->query('SELECT * FROM meta;')) {
136 $q->setFetchMode(\PDO::FETCH_KEY_PAIR);
137 $meta = $q->fetchAll();
139 foreach ($meta as $key => $val) {
142 $d = new \DateTime('@' . $val);
143 $val = $d->format(\DateTime::RFC2822);
146 $key = 'db ' . str_replace('_', ' ', $key);
150 $docs['db schema'] = '0.1.0';
153 } catch (Exception\RuntimeException $e) {
154 if ($e->getMessage() === 'SQLite PDO driver not found') {
155 $docs['sqlite available'] = false;
161 $autocomplete = array(
162 'tab completion enabled' => $config->getTabCompletion(),
163 'custom matchers' => array_map('get_class', $config->getTabCompletionMatchers()),
166 return array_merge($core, compact('updates', 'pcntl', 'readline', 'history', 'docs', 'autocomplete'));
170 if (!function_exists('Psy\bin')) {
172 * `psysh` command line executable.
179 $usageException = null;
181 $input = new ArgvInput();
183 $input->bind(new InputDefinition(array(
184 new InputOption('help', 'h', InputOption::VALUE_NONE),
185 new InputOption('config', 'c', InputOption::VALUE_REQUIRED),
186 new InputOption('version', 'v', InputOption::VALUE_NONE),
187 new InputOption('cwd', null, InputOption::VALUE_REQUIRED),
188 new InputOption('color', null, InputOption::VALUE_NONE),
189 new InputOption('no-color', null, InputOption::VALUE_NONE),
191 new InputArgument('include', InputArgument::IS_ARRAY),
193 } catch (\RuntimeException $e) {
194 $usageException = $e;
200 if ($configFile = $input->getOption('config')) {
201 $config['configFile'] = $configFile;
204 // Handle --color and --no-color
205 if ($input->getOption('color') && $input->getOption('no-color')) {
206 $usageException = new \RuntimeException('Using both "--color" and "--no-color" options is invalid.');
207 } elseif ($input->getOption('color')) {
208 $config['colorMode'] = Configuration::COLOR_MODE_FORCED;
209 } elseif ($input->getOption('no-color')) {
210 $config['colorMode'] = Configuration::COLOR_MODE_DISABLED;
213 $shell = new Shell(new Configuration($config));
216 if ($usageException !== null || $input->getOption('help')) {
217 if ($usageException !== null) {
218 echo $usageException->getMessage() . PHP_EOL . PHP_EOL;
221 $version = $shell->getVersion();
222 $name = basename(reset($_SERVER['argv']));
227 $name [--version] [--help] [files...]
230 --help -h Display this help message.
231 --config -c Use an alternate PsySH config file location.
232 --cwd Use an alternate working directory.
233 --version -v Display the PsySH version.
234 --color Force colors in output.
235 --no-color Disable colors in output.
238 exit($usageException === null ? 0 : 1);
242 if ($input->getOption('version')) {
243 echo $shell->getVersion() . PHP_EOL;
247 // Pass additional arguments to Shell as 'includes'
248 $shell->setIncludes($input->getArgument('include'));
253 } catch (Exception $e) {
254 echo $e->getMessage() . PHP_EOL;
256 // TODO: this triggers the "exited unexpectedly" logic in the
257 // ForkingLoop, so we can't exit(1) after starting the shell...