Security update for Core, with self-updated composer
[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', '__out', '__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         // self:: doesn't work inside closures in PHP 5.3 :-/
82         $specialNames = self::$specialNames;
83
84         $scopeVars = $this->context->getAll();
85         uksort($scopeVars, function ($a, $b) use ($specialNames) {
86             $aIndex = array_search($a, $specialNames);
87             $bIndex = array_search($b, $specialNames);
88
89             if ($aIndex !== false) {
90                 if ($bIndex !== false) {
91                     return $aIndex - $bIndex;
92                 }
93
94                 return 1;
95             }
96
97             if ($bIndex !== false) {
98                 return -1;
99             }
100
101             return strnatcasecmp($a, $b);
102         });
103
104         $ret = array();
105         foreach ($scopeVars as $name => $val) {
106             if (!$showAll && in_array($name, self::$specialNames)) {
107                 continue;
108             }
109
110             $ret[$name] = $val;
111         }
112
113         return $ret;
114     }
115
116     /**
117      * Prepare formatted variable array.
118      *
119      * @param array $variables
120      *
121      * @return array
122      */
123     protected function prepareVariables(array $variables)
124     {
125         // My kingdom for a generator.
126         $ret = array();
127         foreach ($variables as $name => $val) {
128             if ($this->showItem($name)) {
129                 $fname = '$' . $name;
130                 $ret[$fname] = array(
131                     'name'  => $fname,
132                     'style' => in_array($name, self::$specialNames) ? self::IS_PRIVATE : self::IS_PUBLIC,
133                     'value' => $this->presentRef($val),
134                 );
135             }
136         }
137
138         return $ret;
139     }
140 }