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\ReflectionClassConstant;
15
use Symfony\Component\Console\Input\InputInterface;
16
 
17
/**
18
 * Class Constant Enumerator class.
19
 */
20
class ClassConstantEnumerator extends Enumerator
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null): array
26
    {
27
        // only list constants when a Reflector is present.
28
        if ($reflector === null) {
29
            return [];
30
        }
31
 
32
        // We can only list constants on actual class (or object) reflectors.
33
        if (!$reflector instanceof \ReflectionClass) {
34
            // @todo handle ReflectionExtension as well
35
            return [];
36
        }
37
 
38
        // only list constants if we are specifically asked
39
        if (!$input->getOption('constants')) {
40
            return [];
41
        }
42
 
43
        $noInherit = $input->getOption('no-inherit');
44
        $constants = $this->prepareConstants($this->getConstants($reflector, $noInherit));
45
 
46
        if (empty($constants)) {
47
            return [];
48
        }
49
 
50
        $ret = [];
51
        $ret[$this->getKindLabel($reflector)] = $constants;
52
 
53
        return $ret;
54
    }
55
 
56
    /**
57
     * Get defined constants for the given class or object Reflector.
58
     *
59
     * @param \Reflector $reflector
60
     * @param bool       $noInherit Exclude inherited constants
61
     *
62
     * @return array
63
     */
64
    protected function getConstants(\Reflector $reflector, bool $noInherit = false): array
65
    {
66
        $className = $reflector->getName();
67
 
68
        $constants = [];
69
        foreach ($reflector->getConstants() as $name => $constant) {
70
            $constReflector = ReflectionClassConstant::create($reflector->name, $name);
71
 
72
            if ($noInherit && $constReflector->getDeclaringClass()->getName() !== $className) {
73
                continue;
74
            }
75
 
76
            $constants[$name] = $constReflector;
77
        }
78
 
79
        \ksort($constants, \SORT_NATURAL | \SORT_FLAG_CASE);
80
 
81
        return $constants;
82
    }
83
 
84
    /**
85
     * Prepare formatted constant array.
86
     *
87
     * @param array $constants
88
     *
89
     * @return array
90
     */
91
    protected function prepareConstants(array $constants): array
92
    {
93
        // My kingdom for a generator.
94
        $ret = [];
95
 
96
        foreach ($constants as $name => $constant) {
97
            if ($this->showItem($name)) {
98
                $ret[$name] = [
99
                    'name'  => $name,
100
                    'style' => self::IS_CONSTANT,
101
                    'value' => $this->presentRef($constant->getValue()),
102
                ];
103
            }
104
        }
105
 
106
        return $ret;
107
    }
108
 
109
    /**
110
     * Get a label for the particular kind of "class" represented.
111
     *
112
     * @param \ReflectionClass $reflector
113
     *
114
     * @return string
115
     */
116
    protected function getKindLabel(\ReflectionClass $reflector): string
117
    {
118
        if ($reflector->isInterface()) {
119
            return 'Interface Constants';
120
        } else {
121
            return 'Class Constants';
122
        }
123
    }
124
}