Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ListCommand / ConstantEnumerator.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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         $category  = $input->getOption('user') ? 'user' : $input->getOption('category');
43         $label     = $category ? ucfirst($category) . ' Constants' : 'Constants';
44         $constants = $this->prepareConstants($this->getConstants($category));
45
46         if (empty($constants)) {
47             return;
48         }
49
50         $ret = array();
51         $ret[$label] = $constants;
52
53         return $ret;
54     }
55
56     /**
57      * Get defined constants.
58      *
59      * Optionally restrict constants to a given category, e.g. "date".
60      *
61      * @param string $category
62      *
63      * @return array
64      */
65     protected function getConstants($category = null)
66     {
67         if (!$category) {
68             return get_defined_constants();
69         }
70
71         $consts = get_defined_constants(true);
72
73         return isset($consts[$category]) ? $consts[$category] : array();
74     }
75
76     /**
77      * Prepare formatted constant array.
78      *
79      * @param array $constants
80      *
81      * @return array
82      */
83     protected function prepareConstants(array $constants)
84     {
85         // My kingdom for a generator.
86         $ret = array();
87
88         $names = array_keys($constants);
89         natcasesort($names);
90
91         foreach ($names as $name) {
92             if ($this->showItem($name)) {
93                 $ret[$name] = array(
94                     'name'  => $name,
95                     'style' => self::IS_CONSTANT,
96                     'value' => $this->presentRef($constants[$name]),
97                 );
98             }
99         }
100
101         return $ret;
102     }
103 }