Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | 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
 *
6
 * (c) 2012-2022 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\Reflection\ReflectionNamespace;
15
use Symfony\Component\Console\Input\InputInterface;
16
 
17
/**
18
 * Constant Enumerator class.
19
 */
20
class ConstantEnumerator extends Enumerator
21
{
22
    // Because `Json` is ugly.
23
    private static $categoryLabels = [
24
        'libxml'   => 'libxml',
25
        'openssl'  => 'OpenSSL',
26
        'pcre'     => 'PCRE',
27
        'sqlite3'  => 'SQLite3',
28
        'curl'     => 'cURL',
29
        'dom'      => 'DOM',
30
        'ftp'      => 'FTP',
31
        'gd'       => 'GD',
32
        'gmp'      => 'GMP',
33
        'iconv'    => 'iconv',
34
        'json'     => 'JSON',
35
        'ldap'     => 'LDAP',
36
        'mbstring' => 'mbstring',
37
        'odbc'     => 'ODBC',
38
        'pcntl'    => 'PCNTL',
39
        'pgsql'    => 'pgsql',
40
        'posix'    => 'POSIX',
41
        'mysqli'   => 'mysqli',
42
        'soap'     => 'SOAP',
43
        'exif'     => 'EXIF',
44
        'sysvmsg'  => 'sysvmsg',
45
        'xml'      => 'XML',
46
        'xsl'      => 'XSL',
47
    ];
48
 
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null): array
53
    {
54
        // if we have a reflector, ensure that it's a namespace reflector
55
        if (($target !== null || $reflector !== null) && !$reflector instanceof ReflectionNamespace) {
56
            return [];
57
        }
58
 
59
        // only list constants if we are specifically asked
60
        if (!$input->getOption('constants')) {
61
            return [];
62
        }
63
 
64
        $user = $input->getOption('user');
65
        $internal = $input->getOption('internal');
66
        $category = $input->getOption('category');
67
 
68
        if ($category) {
69
            $category = \strtolower($category);
70
 
71
            if ($category === 'internal') {
72
                $internal = true;
73
                $category = null;
74
            } elseif ($category === 'user') {
75
                $user = true;
76
                $category = null;
77
            }
78
        }
79
 
80
        $ret = [];
81
 
82
        if ($user) {
83
            $ret['User Constants'] = $this->getConstants('user');
84
        }
85
 
86
        if ($internal) {
87
            $ret['Internal Constants'] = $this->getConstants('internal');
88
        }
89
 
90
        if ($category) {
91
            $caseCategory = \array_key_exists($category, self::$categoryLabels) ? self::$categoryLabels[$category] : \ucfirst($category);
92
            $label = $caseCategory.' Constants';
93
            $ret[$label] = $this->getConstants($category);
94
        }
95
 
96
        if (!$user && !$internal && !$category) {
97
            $ret['Constants'] = $this->getConstants();
98
        }
99
 
100
        if ($reflector !== null) {
101
            $prefix = \strtolower($reflector->getName()).'\\';
102
 
103
            foreach ($ret as $key => $names) {
104
                foreach (\array_keys($names) as $name) {
105
                    if (\strpos(\strtolower($name), $prefix) !== 0) {
106
                        unset($ret[$key][$name]);
107
                    }
108
                }
109
            }
110
        }
111
 
112
        return \array_map([$this, 'prepareConstants'], \array_filter($ret));
113
    }
114
 
115
    /**
116
     * Get defined constants.
117
     *
118
     * Optionally restrict constants to a given category, e.g. "date". If the
119
     * category is "internal", include all non-user-defined constants.
120
     *
121
     * @param string $category
122
     *
123
     * @return array
124
     */
125
    protected function getConstants(string $category = null): array
126
    {
127
        if (!$category) {
128
            return \get_defined_constants();
129
        }
130
 
131
        $consts = \get_defined_constants(true);
132
 
133
        if ($category === 'internal') {
134
            unset($consts['user']);
135
 
136
            return \array_merge(...\array_values($consts));
137
        }
138
 
139
        foreach ($consts as $key => $value) {
140
            if (\strtolower($key) === $category) {
141
                return $value;
142
            }
143
        }
144
 
145
        return [];
146
    }
147
 
148
    /**
149
     * Prepare formatted constant array.
150
     *
151
     * @param array $constants
152
     *
153
     * @return array
154
     */
155
    protected function prepareConstants(array $constants): array
156
    {
157
        // My kingdom for a generator.
158
        $ret = [];
159
 
160
        $names = \array_keys($constants);
161
        \natcasesort($names);
162
 
163
        foreach ($names as $name) {
164
            if ($this->showItem($name)) {
165
                $ret[$name] = [
166
                    'name'  => $name,
167
                    'style' => self::IS_CONSTANT,
168
                    'value' => $this->presentRef($constants[$name]),
169
                ];
170
            }
171
        }
172
 
173
        return $ret;
174
    }
175
}