Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | 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
 *
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;
13
 
14
use Psy\Output\ShellOutput;
15
use Symfony\Component\Console\Helper\TableHelper;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
 
20
/**
21
 * Help command.
22
 *
23
 * Lists available commands, and gives command-specific help when asked nicely.
24
 */
25
class HelpCommand extends Command
26
{
27
    private $command;
28
 
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('help')
36
            ->setAliases(['?'])
37
            ->setDefinition([
38
                new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name.', null),
39
            ])
40
            ->setDescription('Show a list of commands. Type `help [foo]` for information about [foo].')
41
            ->setHelp('My. How meta.');
42
    }
43
 
44
    /**
45
     * Helper for setting a subcommand to retrieve help for.
46
     *
47
     * @param Command $command
48
     */
49
    public function setCommand(Command $command)
50
    {
51
        $this->command = $command;
52
    }
53
 
54
    /**
55
     * {@inheritdoc}
56
     *
57
     * @return int 0 if everything went fine, or an exit code
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        if ($this->command !== null) {
62
            // help for an individual command
63
            $output->page($this->command->asText());
64
            $this->command = null;
65
        } elseif ($name = $input->getArgument('command_name')) {
66
            // help for an individual command
67
            $output->page($this->getApplication()->get($name)->asText());
68
        } else {
69
            // list available commands
70
            $commands = $this->getApplication()->all();
71
 
72
            $table = $this->getTable($output);
73
 
74
            foreach ($commands as $name => $command) {
75
                if ($name !== $command->getName()) {
76
                    continue;
77
                }
78
 
79
                if ($command->getAliases()) {
80
                    $aliases = \sprintf('<comment>Aliases:</comment> %s', \implode(', ', $command->getAliases()));
81
                } else {
82
                    $aliases = '';
83
                }
84
 
85
                $table->addRow([
86
                    \sprintf('<info>%s</info>', $name),
87
                    $command->getDescription(),
88
                    $aliases,
89
                ]);
90
            }
91
 
92
            if ($output instanceof ShellOutput) {
93
                $output->startPaging();
94
            }
95
 
96
            if ($table instanceof TableHelper) {
97
                $table->render($output);
98
            } else {
99
                $table->render();
100
            }
101
 
102
            if ($output instanceof ShellOutput) {
103
                $output->stopPaging();
104
            }
105
        }
106
 
107
        return 0;
108
    }
109
}