0576e04a3683aa22c3d39894ec63051b2fa8e7c5
[yaffs-website] / vendor / psy / psysh / src / Psy / functions.php
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\Shell::debug(get_defined_vars(), isset($this) ? $this : null));';
32     }
33 }
34
35 if (!function_exists('Psy\info')) {
36     /**
37      * Get a bunch of debugging info about the current PsySH environment and
38      * configuration.
39      *
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.
42      *
43      * @param Configuration|null $config
44      *
45      * @return array|null
46      */
47     function info(Configuration $config = null)
48     {
49         static $lastConfig;
50         if ($config !== null) {
51             $lastConfig = $config;
52
53             return;
54         }
55
56         $xdg = new Xdg();
57         $home = rtrim(str_replace('\\', '/', $xdg->getHomeDir()), '/');
58         $homePattern = '#^' . preg_quote($home, '#') . '/#';
59
60         $prettyPath = function ($path) use ($homePattern) {
61             if (is_string($path)) {
62                 return preg_replace($homePattern, '~/', $path);
63             } else {
64                 return $path;
65             }
66         };
67
68         $config = $lastConfig ?: new Configuration();
69
70         $core = array(
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')),
80             ),
81             // 'config dir'  => $config->getConfigDir(),
82             // 'data dir'    => $config->getDataDir(),
83             // 'runtime dir' => $config->getRuntimeDir(),
84         );
85
86         // Use an explicit, fresh update check here, rather than relying on whatever is in $config.
87         $checker = new GitHubChecker();
88         $updates = array(
89             'update available'       => !$checker->isLatest(),
90             'latest release version' => $checker->getLatest(),
91             'update check interval'  => $config->getUpdateCheck(),
92             'update cache file'      => $prettyPath($config->getUpdateCheckCacheFile()),
93         );
94
95         if ($config->hasReadline()) {
96             $info = readline_info();
97
98             $readline = array(
99                 'readline available' => true,
100                 'readline enabled'   => $config->useReadline(),
101                 'readline service'   => get_class($config->getReadline()),
102             );
103
104             if (isset($info['library_version'])) {
105                 $readline['readline library'] = $info['library_version'];
106             }
107
108             if (isset($info['readline_name']) && $info['readline_name'] !== '') {
109                 $readline['readline name'] = $info['readline_name'];
110             }
111         } else {
112             $readline = array(
113                 'readline available' => false,
114             );
115         }
116
117         $pcntl = array(
118             'pcntl available' => function_exists('pcntl_signal'),
119             'posix available' => function_exists('posix_getpid'),
120         );
121
122         $history = array(
123             'history file'     => $prettyPath($config->getHistoryFile()),
124             'history size'     => $config->getHistorySize(),
125             'erase duplicates' => $config->getEraseDuplicates(),
126         );
127
128         $docs = array(
129             'manual db file'   => $prettyPath($config->getManualDbFile()),
130             'sqlite available' => true,
131         );
132
133         try {
134             if ($db = $config->getManualDb()) {
135                 if ($q = $db->query('SELECT * FROM meta;')) {
136                     $q->setFetchMode(\PDO::FETCH_KEY_PAIR);
137                     $meta = $q->fetchAll();
138
139                     foreach ($meta as $key => $val) {
140                         switch ($key) {
141                             case 'built_at':
142                                 $d = new \DateTime('@' . $val);
143                                 $val = $d->format(\DateTime::RFC2822);
144                                 break;
145                         }
146                         $key = 'db ' . str_replace('_', ' ', $key);
147                         $docs[$key] = $val;
148                     }
149                 } else {
150                     $docs['db schema'] = '0.1.0';
151                 }
152             }
153         } catch (Exception\RuntimeException $e) {
154             if ($e->getMessage() === 'SQLite PDO driver not found') {
155                 $docs['sqlite available'] = false;
156             } else {
157                 throw $e;
158             }
159         }
160
161         $autocomplete = array(
162             'tab completion enabled' => $config->getTabCompletion(),
163             'custom matchers'        => array_map('get_class', $config->getTabCompletionMatchers()),
164         );
165
166         return array_merge($core, compact('updates', 'pcntl', 'readline', 'history', 'docs', 'autocomplete'));
167     }
168 }
169
170 if (!function_exists('Psy\bin')) {
171     /**
172      * `psysh` command line executable.
173      *
174      * @return Closure
175      */
176     function bin()
177     {
178         return function () {
179             $usageException = null;
180
181             $input = new ArgvInput();
182             try {
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),
190
191                     new InputArgument('include', InputArgument::IS_ARRAY),
192                 )));
193             } catch (\RuntimeException $e) {
194                 $usageException = $e;
195             }
196
197             $config = array();
198
199             // Handle --config
200             if ($configFile = $input->getOption('config')) {
201                 $config['configFile'] = $configFile;
202             }
203
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;
211             }
212
213             $shell = new Shell(new Configuration($config));
214
215             // Handle --help
216             if ($usageException !== null || $input->getOption('help')) {
217                 if ($usageException !== null) {
218                     echo $usageException->getMessage() . PHP_EOL . PHP_EOL;
219                 }
220
221                 $version = $shell->getVersion();
222                 $name    = basename(reset($_SERVER['argv']));
223                 echo <<<EOL
224 $version
225
226 Usage:
227   $name [--version] [--help] [files...]
228
229 Options:
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.
236
237 EOL;
238                 exit($usageException === null ? 0 : 1);
239             }
240
241             // Handle --version
242             if ($input->getOption('version')) {
243                 echo $shell->getVersion() . PHP_EOL;
244                 exit(0);
245             }
246
247             // Pass additional arguments to Shell as 'includes'
248             $shell->setIncludes($input->getArgument('include'));
249
250             try {
251                 // And go!
252                 $shell->run();
253             } catch (Exception $e) {
254                 echo $e->getMessage() . PHP_EOL;
255
256                 // TODO: this triggers the "exited unexpectedly" logic in the
257                 // ForkingLoop, so we can't exit(1) after starting the shell...
258                 // fix this :)
259
260                 // exit(1);
261             }
262         };
263     }
264 }