Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 16
ListCommands
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 16
 handle
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 7
 getApplication
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 9
1<?php
2
3namespace Qmp\Laravel\CommandsLaravel\Console\Commands;
4
5use Illuminate\Console\Command;
6use Illuminate\Support\Str;
7use Symfony\Component\Console\Helper\DescriptorHelper;
8
9class ListCommands extends Command
10{
11    /**
12     * The name and signature of the console command.
13     *
14     * @var string
15     */
16    protected $signature = 'command:list    {--format=txt : Output format. Possible values [txt, xml, json, md]}
17                                            {--raw : get raw list of commands}
18                                            {namespace? : display the commands for a specific namespace}'; 
19
20    /**
21     * The console command description.
22     *
23     * @var string
24     */
25    protected $description = 'List application commands';
26
27
28    protected $help = <<<'EOF'
29    The <info>%command.name%</info> command lists all commands:
30    
31      <info>%command.full_name%</info>
32    
33    You can also display the commands for a specific namespace:
34    
35      <info>%command.full_name% test</info>
36    
37    You can also output the information in other formats by using the <comment>--format</comment> option:
38    
39      <info>%command.full_name% --format=xml</info>
40    
41    It's also possible to get raw list of commands (useful for embedding command runner):
42    
43      <info>%command.full_name% --raw</info>
44    EOF
45    ;
46
47
48    /**
49     * Execute the console command.
50     *
51     * @return mixed
52     */
53    public function handle()
54    {
55        $output = resolve(\Symfony\Component\Console\Output\ConsoleOutput::class);
56 
57        $helper = new DescriptorHelper();
58        $helper->describe($output, $this->getApplication(), [
59            'format' => $this->option('format'),
60            'raw_text' => $this->option('raw'),
61            'namespace' => $this->argument('namespace'),
62            'short' => false,
63        ]);
64    }
65
66    public function getApplication()
67    {
68        $app = app();
69        $events = resolve(\Illuminate\Contracts\Events\Dispatcher::class);
70        return new class($app, $events, $app->version()) extends \Illuminate\Console\Application {
71            public function all(string $namespace = null) {
72                $all = collect(parent::all($namespace))->filter(function($command) {
73                    $class = "\033[0;34m" . implode('\\', array_slice(explode('\\', get_class($command)), 0, 3)) . "\033[0m";
74
75
76                    $command->setDescription($class . ' -> ' . $command->getDescription());
77                    return Str::startsWith(get_class($command), 'App') || Str::startsWith(get_class($command), 'Qmp');
78                });
79
80                //dd($all->toArray());
81
82                return $all->toArray();
83            }
84        };
85    }
86}