Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ListCommand / FunctionEnumerator.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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  * Function Enumerator class.
18  */
19 class FunctionEnumerator extends Enumerator
20 {
21     /**
22      * {@inheritdoc}
23      */
24     protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
25     {
26         // only list functions when no Reflector is present.
27         //
28         // @todo make a NamespaceReflector and pass that in for commands like:
29         //
30         //     ls --functions Foo
31         //
32         // ... for listing functions in the Foo namespace
33
34         if ($reflector !== null || $target !== null) {
35             return;
36         }
37
38         // only list functions if we are specifically asked
39         if (!$input->getOption('functions')) {
40             return;
41         }
42
43         if ($input->getOption('user')) {
44             $label     = 'User Functions';
45             $functions = $this->getFunctions('user');
46         } elseif ($input->getOption('internal')) {
47             $label     = 'Internal Functions';
48             $functions = $this->getFunctions('internal');
49         } else {
50             $label     = 'Functions';
51             $functions = $this->getFunctions();
52         }
53
54         $functions = $this->prepareFunctions($functions);
55
56         if (empty($functions)) {
57             return;
58         }
59
60         $ret = array();
61         $ret[$label] = $functions;
62
63         return $ret;
64     }
65
66     /**
67      * Get defined functions.
68      *
69      * Optionally limit functions to "user" or "internal" functions.
70      *
71      * @param null|string $type "user" or "internal" (default: both)
72      *
73      * @return array
74      */
75     protected function getFunctions($type = null)
76     {
77         $funcs = get_defined_functions();
78
79         if ($type) {
80             return $funcs[$type];
81         } else {
82             return array_merge($funcs['internal'], $funcs['user']);
83         }
84     }
85
86     /**
87      * Prepare formatted function array.
88      *
89      * @param array $functions
90      *
91      * @return array
92      */
93     protected function prepareFunctions(array $functions)
94     {
95         natcasesort($functions);
96
97         // My kingdom for a generator.
98         $ret = array();
99
100         foreach ($functions as $name) {
101             if ($this->showItem($name)) {
102                 $ret[$name] = array(
103                     'name'  => $name,
104                     'style' => self::IS_FUNCTION,
105                     'value' => $this->presentSignature($name),
106                 );
107             }
108         }
109
110         return $ret;
111     }
112 }