Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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 = [
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): array
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 [
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(bool $showAll): array
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
            return \strnatcasecmp($a, $b);
99
        });
100
 
101
        $ret = [];
102
        foreach ($scopeVars as $name => $val) {
103
            if (!$showAll && \in_array($name, self::$specialNames)) {
104
                continue;
105
            }
106
 
107
            $ret[$name] = $val;
108
        }
109
 
110
        return $ret;
111
    }
112
 
113
    /**
114
     * Prepare formatted variable array.
115
     *
116
     * @param array $variables
117
     *
118
     * @return array
119
     */
120
    protected function prepareVariables(array $variables): array
121
    {
122
        // My kingdom for a generator.
123
        $ret = [];
124
        foreach ($variables as $name => $val) {
125
            if ($this->showItem($name)) {
126
                $fname = '$'.$name;
127
                $ret[$fname] = [
128
                    'name'  => $fname,
129
                    'style' => \in_array($name, self::$specialNames) ? self::IS_PRIVATE : self::IS_PUBLIC,
130
                    'value' => $this->presentRef($val),
131
                ];
132
            }
133
        }
134
 
135
        return $ret;
136
    }
137
}