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 * Class Enumerator class.
19 class ClassEnumerator extends Enumerator
24 protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
26 // only list classes when no Reflector is present.
28 // @todo make a NamespaceReflector and pass that in for commands like:
32 // ... for listing classes in the Foo namespace
34 if ($reflector !== null || $target !== null) {
38 $user = $input->getOption('user');
39 $internal = $input->getOption('internal');
43 // only list classes, interfaces and traits if we are specifically asked
45 if ($input->getOption('classes')) {
46 $ret = array_merge($ret, $this->filterClasses('Classes', get_declared_classes(), $internal, $user));
49 if ($input->getOption('interfaces')) {
50 $ret = array_merge($ret, $this->filterClasses('Interfaces', get_declared_interfaces(), $internal, $user));
53 if (function_exists('get_declared_traits') && $input->getOption('traits')) {
54 $ret = array_merge($ret, $this->filterClasses('Traits', get_declared_traits(), $internal, $user));
57 return array_map([$this, 'prepareClasses'], array_filter($ret));
61 * Filter a list of classes, interfaces or traits.
63 * If $internal or $user is defined, results will be limited to internal or
64 * user-defined classes as appropriate.
67 * @param array $classes
68 * @param bool $internal
73 protected function filterClasses($key, $classes, $internal, $user)
78 $ret['Internal ' . $key] = array_filter($classes, function ($class) {
79 $refl = new \ReflectionClass($class);
81 return $refl->isInternal();
86 $ret['User ' . $key] = array_filter($classes, function ($class) {
87 $refl = new \ReflectionClass($class);
89 return !$refl->isInternal();
93 if (!$user && !$internal) {
94 $ret[$key] = $classes;
101 * Prepare formatted class array.
103 * @param array $classes
107 protected function prepareClasses(array $classes)
109 natcasesort($classes);
111 // My kingdom for a generator.
114 foreach ($classes as $name) {
115 if ($this->showItem($name)) {
118 'style' => self::IS_CLASS,
119 'value' => $this->presentSignature($name),