Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ListCommand / GlobalVariableEnumerator.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\Command\ListCommand;
13
14 use Symfony\Component\Console\Input\InputInterface;
15
16 /**
17  * Global Variable Enumerator class.
18  */
19 class GlobalVariableEnumerator extends Enumerator
20 {
21     /**
22      * {@inheritdoc}
23      */
24     protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
25     {
26         // only list globals when no Reflector is present.
27         if ($reflector !== null || $target !== null) {
28             return;
29         }
30
31         // only list globals if we are specifically asked
32         if (!$input->getOption('globals')) {
33             return;
34         }
35
36         $globals = $this->prepareGlobals($this->getGlobals());
37
38         if (empty($globals)) {
39             return;
40         }
41
42         return array(
43             'Global Variables' => $globals,
44         );
45     }
46
47     /**
48      * Get defined global variables.
49      *
50      * @return array
51      */
52     protected function getGlobals()
53     {
54         global $GLOBALS;
55
56         $names = array_keys($GLOBALS);
57         natcasesort($names);
58
59         $ret = array();
60         foreach ($names as $name) {
61             $ret[$name] = $GLOBALS[$name];
62         }
63
64         return $ret;
65     }
66
67     /**
68      * Prepare formatted global variable array.
69      *
70      * @param array $globals
71      *
72      * @return array
73      */
74     protected function prepareGlobals($globals)
75     {
76         // My kingdom for a generator.
77         $ret = array();
78
79         foreach ($globals as $name => $value) {
80             if ($this->showItem($name)) {
81                 $fname = '$' . $name;
82                 $ret[$fname] = array(
83                     'name'  => $fname,
84                     'style' => self::IS_GLOBAL,
85                     'value' => $this->presentRef($value),
86                 );
87             }
88         }
89
90         return $ret;
91     }
92 }