ad4ce0d12553de21680f3132375f8c512c5f07f5
[yaffs-website] / vendor / psy / psysh / src / Command / ListCommand / ConstantEnumerator.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 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 Symfony\Component\Console\Input\InputInterface;
15
16 /**
17  * Constant Enumerator class.
18  */
19 class ConstantEnumerator extends Enumerator
20 {
21     /**
22      * {@inheritdoc}
23      */
24     protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
25     {
26         // only list constants when no Reflector is present.
27         //
28         // @todo make a NamespaceReflector and pass that in for commands like:
29         //
30         //     ls --constants Foo
31         //
32         // ... for listing constants in the Foo namespace
33         if ($reflector !== null || $target !== null) {
34             return;
35         }
36
37         // only list constants if we are specifically asked
38         if (!$input->getOption('constants')) {
39             return;
40         }
41
42         $user     = $input->getOption('user');
43         $internal = $input->getOption('internal');
44         $category = $input->getOption('category');
45
46         $ret = [];
47
48         if ($user) {
49             $ret['User Constants'] = $this->getConstants('user');
50         }
51
52         if ($internal) {
53             $ret['Interal Constants'] = $this->getConstants('internal');
54         }
55
56         if ($category) {
57             $label = \ucfirst($category) . ' Constants';
58             $ret[$label] = $this->getConstants($category);
59         }
60
61         if (!$user && !$internal && !$category) {
62             $ret['Constants'] = $this->getConstants();
63         }
64
65         return \array_map([$this, 'prepareConstants'], \array_filter($ret));
66     }
67
68     /**
69      * Get defined constants.
70      *
71      * Optionally restrict constants to a given category, e.g. "date". If the
72      * category is "internal", include all non-user-defined constants.
73      *
74      * @param string $category
75      *
76      * @return array
77      */
78     protected function getConstants($category = null)
79     {
80         if (!$category) {
81             return \get_defined_constants();
82         }
83
84         $consts = \get_defined_constants(true);
85
86         if ($category === 'internal') {
87             unset($consts['user']);
88
89             return \call_user_func_array('array_merge', $consts);
90         }
91
92         return isset($consts[$category]) ? $consts[$category] : [];
93     }
94
95     /**
96      * Prepare formatted constant array.
97      *
98      * @param array $constants
99      *
100      * @return array
101      */
102     protected function prepareConstants(array $constants)
103     {
104         // My kingdom for a generator.
105         $ret = [];
106
107         $names = \array_keys($constants);
108         \natcasesort($names);
109
110         foreach ($names as $name) {
111             if ($this->showItem($name)) {
112                 $ret[$name] = [
113                     'name'  => $name,
114                     'style' => self::IS_CONSTANT,
115                     'value' => $this->presentRef($constants[$name]),
116                 ];
117             }
118         }
119
120         return $ret;
121     }
122 }