| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of Psy Shell.
|
|
|
5 |
*
|
|
|
6 |
* (c) 2012-2022 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 Psy\Reflection\ReflectionNamespace;
|
|
|
15 |
use Symfony\Component\Console\Input\InputInterface;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Class Enumerator class.
|
|
|
19 |
*/
|
|
|
20 |
class ClassEnumerator extends Enumerator
|
|
|
21 |
{
|
|
|
22 |
/**
|
|
|
23 |
* {@inheritdoc}
|
|
|
24 |
*/
|
|
|
25 |
protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null): array
|
|
|
26 |
{
|
|
|
27 |
// if we have a reflector, ensure that it's a namespace reflector
|
|
|
28 |
if (($target !== null || $reflector !== null) && !$reflector instanceof ReflectionNamespace) {
|
|
|
29 |
return [];
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
$internal = $input->getOption('internal');
|
|
|
33 |
$user = $input->getOption('user');
|
|
|
34 |
$prefix = $reflector === null ? null : \strtolower($reflector->getName()).'\\';
|
|
|
35 |
|
|
|
36 |
$ret = [];
|
|
|
37 |
|
|
|
38 |
// only list classes, interfaces and traits if we are specifically asked
|
|
|
39 |
|
|
|
40 |
if ($input->getOption('classes')) {
|
|
|
41 |
$ret = \array_merge($ret, $this->filterClasses('Classes', \get_declared_classes(), $internal, $user, $prefix));
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
if ($input->getOption('interfaces')) {
|
|
|
45 |
$ret = \array_merge($ret, $this->filterClasses('Interfaces', \get_declared_interfaces(), $internal, $user, $prefix));
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
if ($input->getOption('traits')) {
|
|
|
49 |
$ret = \array_merge($ret, $this->filterClasses('Traits', \get_declared_traits(), $internal, $user, $prefix));
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
return \array_map([$this, 'prepareClasses'], \array_filter($ret));
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Filter a list of classes, interfaces or traits.
|
|
|
57 |
*
|
|
|
58 |
* If $internal or $user is defined, results will be limited to internal or
|
|
|
59 |
* user-defined classes as appropriate.
|
|
|
60 |
*
|
|
|
61 |
* @param string $key
|
|
|
62 |
* @param array $classes
|
|
|
63 |
* @param bool $internal
|
|
|
64 |
* @param bool $user
|
|
|
65 |
* @param string $prefix
|
|
|
66 |
*
|
|
|
67 |
* @return array
|
|
|
68 |
*/
|
|
|
69 |
protected function filterClasses(string $key, array $classes, bool $internal, bool $user, string $prefix = null): array
|
|
|
70 |
{
|
|
|
71 |
$ret = [];
|
|
|
72 |
|
|
|
73 |
if ($internal) {
|
|
|
74 |
$ret['Internal '.$key] = \array_filter($classes, function ($class) use ($prefix) {
|
|
|
75 |
if ($prefix !== null && \strpos(\strtolower($class), $prefix) !== 0) {
|
|
|
76 |
return false;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
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) use ($prefix) {
|
|
|
87 |
if ($prefix !== null && \strpos(\strtolower($class), $prefix) !== 0) {
|
|
|
88 |
return false;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
$refl = new \ReflectionClass($class);
|
|
|
92 |
|
|
|
93 |
return !$refl->isInternal();
|
|
|
94 |
});
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if (!$user && !$internal) {
|
|
|
98 |
$ret[$key] = \array_filter($classes, function ($class) use ($prefix) {
|
|
|
99 |
return $prefix === null || \strpos(\strtolower($class), $prefix) === 0;
|
|
|
100 |
});
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
return $ret;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Prepare formatted class array.
|
|
|
108 |
*
|
|
|
109 |
* @param array $classes
|
|
|
110 |
*
|
|
|
111 |
* @return array
|
|
|
112 |
*/
|
|
|
113 |
protected function prepareClasses(array $classes): array
|
|
|
114 |
{
|
|
|
115 |
\natcasesort($classes);
|
|
|
116 |
|
|
|
117 |
// My kingdom for a generator.
|
|
|
118 |
$ret = [];
|
|
|
119 |
|
|
|
120 |
foreach ($classes as $name) {
|
|
|
121 |
if ($this->showItem($name)) {
|
|
|
122 |
$ret[$name] = [
|
|
|
123 |
'name' => $name,
|
|
|
124 |
'style' => self::IS_CLASS,
|
|
|
125 |
'value' => $this->presentSignature($name),
|
|
|
126 |
];
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
return $ret;
|
|
|
131 |
}
|
|
|
132 |
}
|