Yaffs site version 1.1
[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\debug(get_defined_vars(), isset($this) ? $this : null));';
32     }
33 }
34
35 if (!function_exists('Psy\debug')) {
36     /**
37      * Invoke a Psy Shell from the current context.
38      *
39      * For example:
40      *
41      *     foreach ($items as $item) {
42      *         \Psy\debug(get_defined_vars());
43      *     }
44      *
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:
47      *
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
51      *     }
52      *
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:
56      *
57      *     class Foo {
58      *         function bar() {
59      *             \Psy\debug(get_defined_vars(), $this);
60      *         }
61      *     }
62      *
63      * This only really works in PHP 5.4+ and HHVM 3.5+, so upgrade already.
64      *
65      * @param array  $vars        Scope variables from the calling context (default: array())
66      * @param object $boundObject Bound object ($this) value for the shell
67      *
68      * @return array Scope variables from the debugger session
69      */
70     function debug(array $vars = array(), $boundObject = null)
71     {
72         echo PHP_EOL;
73
74         $sh = new Shell();
75         $sh->setScopeVariables($vars);
76
77         // Show a couple of lines of call context for the debug session.
78         //
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);
82         }
83
84         if ($boundObject !== null) {
85             $sh->setBoundObject($boundObject);
86         }
87
88         $sh->run();
89
90         return $sh->getScopeVariables(false);
91     }
92 }
93
94 if (!function_exists('Psy\info')) {
95     /**
96      * Get a bunch of debugging info about the current PsySH environment and
97      * configuration.
98      *
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.
101      *
102      * @param Configuration|null $config
103      *
104      * @return array|null
105      */
106     function info(Configuration $config = null)
107     {
108         static $lastConfig;
109         if ($config !== null) {
110             $lastConfig = $config;
111
112             return;
113         }
114
115         $xdg = new Xdg();
116         $home = rtrim(str_replace('\\', '/', $xdg->getHomeDir()), '/');
117         $homePattern = '#^' . preg_quote($home, '#') . '/#';
118
119         $prettyPath = function ($path) use ($homePattern) {
120             if (is_string($path)) {
121                 return preg_replace($homePattern, '~/', $path);
122             } else {
123                 return $path;
124             }
125         };
126
127         $config = $lastConfig ?: new Configuration();
128
129         $core = array(
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')),
139             ),
140             // 'config dir'  => $config->getConfigDir(),
141             // 'data dir'    => $config->getDataDir(),
142             // 'runtime dir' => $config->getRuntimeDir(),
143         );
144
145         // Use an explicit, fresh update check here, rather than relying on whatever is in $config.
146         $checker = new GitHubChecker();
147         $updates = array(
148             'update available'       => !$checker->isLatest(),
149             'latest release version' => $checker->getLatest(),
150             'update check interval'  => $config->getUpdateCheck(),
151             'update cache file'      => $prettyPath($config->getUpdateCheckCacheFile()),
152         );
153
154         if ($config->hasReadline()) {
155             $info = readline_info();
156
157             $readline = array(
158                 'readline available' => true,
159                 'readline enabled'   => $config->useReadline(),
160                 'readline service'   => get_class($config->getReadline()),
161             );
162
163             if (isset($info['library_version'])) {
164                 $readline['readline library'] = $info['library_version'];
165             }
166
167             if (isset($info['readline_name']) && $info['readline_name'] !== '') {
168                 $readline['readline name'] = $info['readline_name'];
169             }
170         } else {
171             $readline = array(
172                 'readline available' => false,
173             );
174         }
175
176         $pcntl = array(
177             'pcntl available' => function_exists('pcntl_signal'),
178             'posix available' => function_exists('posix_getpid'),
179         );
180
181         $history = array(
182             'history file'     => $prettyPath($config->getHistoryFile()),
183             'history size'     => $config->getHistorySize(),
184             'erase duplicates' => $config->getEraseDuplicates(),
185         );
186
187         $docs = array(
188             'manual db file'   => $prettyPath($config->getManualDbFile()),
189             'sqlite available' => true,
190         );
191
192         try {
193             if ($db = $config->getManualDb()) {
194                 if ($q = $db->query('SELECT * FROM meta;')) {
195                     $q->setFetchMode(\PDO::FETCH_KEY_PAIR);
196                     $meta = $q->fetchAll();
197
198                     foreach ($meta as $key => $val) {
199                         switch ($key) {
200                             case 'built_at':
201                                 $d = new \DateTime('@' . $val);
202                                 $val = $d->format(\DateTime::RFC2822);
203                                 break;
204                         }
205                         $key = 'db ' . str_replace('_', ' ', $key);
206                         $docs[$key] = $val;
207                     }
208                 } else {
209                     $docs['db schema'] = '0.1.0';
210                 }
211             }
212         } catch (Exception\RuntimeException $e) {
213             if ($e->getMessage() === 'SQLite PDO driver not found') {
214                 $docs['sqlite available'] = false;
215             } else {
216                 throw $e;
217             }
218         }
219
220         $autocomplete = array(
221             'tab completion enabled' => $config->getTabCompletion(),
222             'custom matchers'        => array_map('get_class', $config->getTabCompletionMatchers()),
223         );
224
225         return array_merge($core, compact('updates', 'pcntl', 'readline', 'history', 'docs', 'autocomplete'));
226     }
227 }
228
229 if (!function_exists('Psy\bin')) {
230     /**
231      * `psysh` command line executable.
232      *
233      * @return Closure
234      */
235     function bin()
236     {
237         return function () {
238             $usageException = null;
239
240             $input = new ArgvInput();
241             try {
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),
249
250                     new InputArgument('include', InputArgument::IS_ARRAY),
251                 )));
252             } catch (\RuntimeException $e) {
253                 $usageException = $e;
254             }
255
256             $config = array();
257
258             // Handle --config
259             if ($configFile = $input->getOption('config')) {
260                 $config['configFile'] = $configFile;
261             }
262
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;
270             }
271
272             $shell = new Shell(new Configuration($config));
273
274             // Handle --help
275             if ($usageException !== null || $input->getOption('help')) {
276                 if ($usageException !== null) {
277                     echo $usageException->getMessage() . PHP_EOL . PHP_EOL;
278                 }
279
280                 $version = $shell->getVersion();
281                 $name    = basename(reset($_SERVER['argv']));
282                 echo <<<EOL
283 $version
284
285 Usage:
286   $name [--version] [--help] [files...]
287
288 Options:
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.
295
296 EOL;
297                 exit($usageException === null ? 0 : 1);
298             }
299
300             // Handle --version
301             if ($input->getOption('version')) {
302                 echo $shell->getVersion() . PHP_EOL;
303                 exit(0);
304             }
305
306             // Pass additional arguments to Shell as 'includes'
307             $shell->setIncludes($input->getArgument('include'));
308
309             try {
310                 // And go!
311                 $shell->run();
312             } catch (Exception $e) {
313                 echo $e->getMessage() . PHP_EOL;
314
315                 // @todo this triggers the "exited unexpectedly" logic in the
316                 // ForkingLoop, so we can't exit(1) after starting the shell...
317                 // fix this :)
318
319                 // exit(1);
320             }
321         };
322     }
323 }