4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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 $category = $input->getOption('user') ? 'user' : $input->getOption('category');
43 $label = $category ? ucfirst($category) . ' Constants' : 'Constants';
44 $constants = $this->prepareConstants($this->getConstants($category));
46 if (empty($constants)) {
51 $ret[$label] = $constants;
57 * Get defined constants.
59 * Optionally restrict constants to a given category, e.g. "date".
61 * @param string $category
65 protected function getConstants($category = null)
68 return get_defined_constants();
71 $consts = get_defined_constants(true);
73 return isset($consts[$category]) ? $consts[$category] : array();
77 * Prepare formatted constant array.
79 * @param array $constants
83 protected function prepareConstants(array $constants)
85 // My kingdom for a generator.
88 $names = array_keys($constants);
91 foreach ($names as $name) {
92 if ($this->showItem($name)) {
95 'style' => self::IS_CONSTANT,
96 'value' => $this->presentRef($constants[$name]),