Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ListCommand / VariableEnumerator.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 Psy\Context;
15 use Psy\VarDumper\Presenter;
16 use Symfony\Component\Console\Input\InputInterface;
17
18 /**
19  * Variable Enumerator class.
20  */
21 class VariableEnumerator extends Enumerator
22 {
23     // n.b. this array is the order in which special variables will be listed
24     private static $specialNames = array(
25         '_', '_e', '__function', '__method', '__class', '__namespace', '__file', '__line', '__dir',
26     );
27
28     private $context;
29
30     /**
31      * Variable Enumerator constructor.
32      *
33      * Unlike most other enumerators, the Variable Enumerator needs access to
34      * the current scope variables, so we need to pass it a Context instance.
35      *
36      * @param Presenter $presenter
37      * @param Context   $context
38      */
39     public function __construct(Presenter $presenter, Context $context)
40     {
41         $this->context = $context;
42         parent::__construct($presenter);
43     }
44
45     /**
46      * {@inheritdoc}
47      */
48     protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
49     {
50         // only list variables when no Reflector is present.
51         if ($reflector !== null || $target !== null) {
52             return;
53         }
54
55         // only list variables if we are specifically asked
56         if (!$input->getOption('vars')) {
57             return;
58         }
59
60         $showAll   = $input->getOption('all');
61         $variables = $this->prepareVariables($this->getVariables($showAll));
62
63         if (empty($variables)) {
64             return;
65         }
66
67         return array(
68             'Variables' => $variables,
69         );
70     }
71
72     /**
73      * Get scope variables.
74      *
75      * @param bool $showAll Include special variables (e.g. $_)
76      *
77      * @return array
78      */
79     protected function getVariables($showAll)
80     {
81         $scopeVars = $this->context->getAll();
82         uksort($scopeVars, function ($a, $b) {
83             $aIndex = array_search($a, self::$specialNames);
84             $bIndex = array_search($b, self::$specialNames);
85
86             if ($aIndex !== false) {
87                 if ($bIndex !== false) {
88                     return $aIndex - $bIndex;
89                 }
90
91                 return 1;
92             }
93
94             if ($bIndex !== false) {
95                 return -1;
96             }
97
98             // @todo this should be natcasesort
99             return strcasecmp($a, $b);
100         });
101
102         $ret = array();
103         foreach ($scopeVars as $name => $val) {
104             if (!$showAll && in_array($name, self::$specialNames)) {
105                 continue;
106             }
107
108             $ret[$name] = $val;
109         }
110
111         return $ret;
112     }
113
114     /**
115      * Prepare formatted variable array.
116      *
117      * @param array $variables
118      *
119      * @return array
120      */
121     protected function prepareVariables(array $variables)
122     {
123         // My kingdom for a generator.
124         $ret = array();
125         foreach ($variables as $name => $val) {
126             if ($this->showItem($name)) {
127                 $fname = '$' . $name;
128                 $ret[$fname] = array(
129                     'name'  => $fname,
130                     'style' => in_array($name, self::$specialNames) ? self::IS_PRIVATE : self::IS_PUBLIC,
131                     'value' => $this->presentRef($val),
132                 );
133             }
134         }
135
136         return $ret;
137     }
138 }