Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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
 * Function Enumerator class.
19
 */
20
class FunctionEnumerator 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
        // only list functions if we are specifically asked
33
        if (!$input->getOption('functions')) {
34
            return [];
35
        }
36
 
37
        if ($input->getOption('user')) {
38
            $label = 'User Functions';
39
            $functions = $this->getFunctions('user');
40
        } elseif ($input->getOption('internal')) {
41
            $label = 'Internal Functions';
42
            $functions = $this->getFunctions('internal');
43
        } else {
44
            $label = 'Functions';
45
            $functions = $this->getFunctions();
46
        }
47
 
48
        $prefix = $reflector === null ? null : \strtolower($reflector->getName()).'\\';
49
        $functions = $this->prepareFunctions($functions, $prefix);
50
 
51
        if (empty($functions)) {
52
            return [];
53
        }
54
 
55
        $ret = [];
56
        $ret[$label] = $functions;
57
 
58
        return $ret;
59
    }
60
 
61
    /**
62
     * Get defined functions.
63
     *
64
     * Optionally limit functions to "user" or "internal" functions.
65
     *
66
     * @param string|null $type "user" or "internal" (default: both)
67
     *
68
     * @return array
69
     */
70
    protected function getFunctions(string $type = null): array
71
    {
72
        $funcs = \get_defined_functions();
73
 
74
        if ($type) {
75
            return $funcs[$type];
76
        } else {
77
            return \array_merge($funcs['internal'], $funcs['user']);
78
        }
79
    }
80
 
81
    /**
82
     * Prepare formatted function array.
83
     *
84
     * @param array  $functions
85
     * @param string $prefix
86
     *
87
     * @return array
88
     */
89
    protected function prepareFunctions(array $functions, string $prefix = null): array
90
    {
91
        \natcasesort($functions);
92
 
93
        // My kingdom for a generator.
94
        $ret = [];
95
 
96
        foreach ($functions as $name) {
97
            if ($prefix !== null && \strpos(\strtolower($name), $prefix) !== 0) {
98
                continue;
99
            }
100
 
101
            if ($this->showItem($name)) {
102
                try {
103
                    $ret[$name] = [
104
                        'name'  => $name,
105
                        'style' => self::IS_FUNCTION,
106
                        'value' => $this->presentSignature($name),
107
                    ];
108
                } catch (\Throwable $e) {
109
                    // Ignore failures.
110
                }
111
            }
112
        }
113
 
114
        return $ret;
115
    }
116
}