4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command\ListCommand;
15 use Psy\VarDumper\Presenter;
16 use Symfony\Component\Console\Input\InputInterface;
19 * Variable Enumerator class.
21 class VariableEnumerator extends Enumerator
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',
31 * Variable Enumerator constructor.
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.
36 * @param Presenter $presenter
37 * @param Context $context
39 public function __construct(Presenter $presenter, Context $context)
41 $this->context = $context;
42 parent::__construct($presenter);
48 protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
50 // only list variables when no Reflector is present.
51 if ($reflector !== null || $target !== null) {
55 // only list variables if we are specifically asked
56 if (!$input->getOption('vars')) {
60 $showAll = $input->getOption('all');
61 $variables = $this->prepareVariables($this->getVariables($showAll));
63 if (empty($variables)) {
68 'Variables' => $variables,
73 * Get scope variables.
75 * @param bool $showAll Include special variables (e.g. $_)
79 protected function getVariables($showAll)
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);
86 if ($aIndex !== false) {
87 if ($bIndex !== false) {
88 return $aIndex - $bIndex;
94 if ($bIndex !== false) {
98 // @todo this should be natcasesort
99 return strcasecmp($a, $b);
103 foreach ($scopeVars as $name => $val) {
104 if (!$showAll && in_array($name, self::$specialNames)) {
115 * Prepare formatted variable array.
117 * @param array $variables
121 protected function prepareVariables(array $variables)
123 // My kingdom for a generator.
125 foreach ($variables as $name => $val) {
126 if ($this->showItem($name)) {
127 $fname = '$' . $name;
128 $ret[$fname] = array(
130 'style' => in_array($name, self::$specialNames) ? self::IS_PRIVATE : self::IS_PUBLIC,
131 'value' => $this->presentRef($val),