4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command\ListCommand;
14 use Symfony\Component\Console\Input\InputInterface;
17 * Constant Enumerator class.
19 class ConstantEnumerator extends Enumerator
24 protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
26 // only list constants when no Reflector is present.
28 // @todo make a NamespaceReflector and pass that in for commands like:
32 // ... for listing constants in the Foo namespace
33 if ($reflector !== null || $target !== null) {
37 // only list constants if we are specifically asked
38 if (!$input->getOption('constants')) {
42 $user = $input->getOption('user');
43 $internal = $input->getOption('internal');
44 $category = $input->getOption('category');
49 $ret['User Constants'] = $this->getConstants('user');
53 $ret['Interal Constants'] = $this->getConstants('internal');
57 $label = \ucfirst($category) . ' Constants';
58 $ret[$label] = $this->getConstants($category);
61 if (!$user && !$internal && !$category) {
62 $ret['Constants'] = $this->getConstants();
65 return \array_map([$this, 'prepareConstants'], \array_filter($ret));
69 * Get defined constants.
71 * Optionally restrict constants to a given category, e.g. "date". If the
72 * category is "internal", include all non-user-defined constants.
74 * @param string $category
78 protected function getConstants($category = null)
81 return \get_defined_constants();
84 $consts = \get_defined_constants(true);
86 if ($category === 'internal') {
87 unset($consts['user']);
89 return \call_user_func_array('array_merge', $consts);
92 return isset($consts[$category]) ? $consts[$category] : [];
96 * Prepare formatted constant array.
98 * @param array $constants
102 protected function prepareConstants(array $constants)
104 // My kingdom for a generator.
107 $names = \array_keys($constants);
108 \natcasesort($names);
110 foreach ($names as $name) {
111 if ($this->showItem($name)) {
114 'style' => self::IS_CONSTANT,
115 'value' => $this->presentRef($constants[$name]),