126a49c22e9d02cc48658575c2b289a92bf23c37
[yaffs-website] / vendor / psy / psysh / src / Command / ListCommand / ClassEnumerator.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  * Class Enumerator class.
18  */
19 class ClassEnumerator extends Enumerator
20 {
21     /**
22      * {@inheritdoc}
23      */
24     protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
25     {
26         // only list classes when no Reflector is present.
27         //
28         // @todo make a NamespaceReflector and pass that in for commands like:
29         //
30         //     ls --classes Foo
31         //
32         // ... for listing classes in the Foo namespace
33
34         if ($reflector !== null || $target !== null) {
35             return;
36         }
37
38         $user     = $input->getOption('user');
39         $internal = $input->getOption('internal');
40
41         $ret = [];
42
43         // only list classes, interfaces and traits if we are specifically asked
44
45         if ($input->getOption('classes')) {
46             $ret = array_merge($ret, $this->filterClasses('Classes', get_declared_classes(), $internal, $user));
47         }
48
49         if ($input->getOption('interfaces')) {
50             $ret = array_merge($ret, $this->filterClasses('Interfaces', get_declared_interfaces(), $internal, $user));
51         }
52
53         if (function_exists('get_declared_traits') && $input->getOption('traits')) {
54             $ret = array_merge($ret, $this->filterClasses('Traits', get_declared_traits(), $internal, $user));
55         }
56
57         return array_map([$this, 'prepareClasses'], array_filter($ret));
58     }
59
60     /**
61      * Filter a list of classes, interfaces or traits.
62      *
63      * If $internal or $user is defined, results will be limited to internal or
64      * user-defined classes as appropriate.
65      *
66      * @param string $key
67      * @param array  $classes
68      * @param bool   $internal
69      * @param bool   $user
70      *
71      * @return array
72      */
73     protected function filterClasses($key, $classes, $internal, $user)
74     {
75         $ret = [];
76
77         if ($internal) {
78             $ret['Internal ' . $key] = array_filter($classes, function ($class) {
79                 $refl = new \ReflectionClass($class);
80
81                 return $refl->isInternal();
82             });
83         }
84
85         if ($user) {
86             $ret['User ' . $key] = array_filter($classes, function ($class) {
87                 $refl = new \ReflectionClass($class);
88
89                 return !$refl->isInternal();
90             });
91         }
92
93         if (!$user && !$internal) {
94             $ret[$key] = $classes;
95         }
96
97         return $ret;
98     }
99
100     /**
101      * Prepare formatted class array.
102      *
103      * @param array $classes
104      *
105      * @return array
106      */
107     protected function prepareClasses(array $classes)
108     {
109         natcasesort($classes);
110
111         // My kingdom for a generator.
112         $ret = [];
113
114         foreach ($classes as $name) {
115             if ($this->showItem($name)) {
116                 $ret[$name] = [
117                     'name'  => $name,
118                     'style' => self::IS_CLASS,
119                     'value' => $this->presentSignature($name),
120                 ];
121             }
122         }
123
124         return $ret;
125     }
126 }