Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\VarDumper\Command;
13
 
14
use Symfony\Component\Console\Attribute\AsCommand;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Completion\CompletionInput;
17
use Symfony\Component\Console\Completion\CompletionSuggestions;
18
use Symfony\Component\Console\Exception\InvalidArgumentException;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
use Symfony\Component\VarDumper\Cloner\Data;
24
use Symfony\Component\VarDumper\Command\Descriptor\CliDescriptor;
25
use Symfony\Component\VarDumper\Command\Descriptor\DumpDescriptorInterface;
26
use Symfony\Component\VarDumper\Command\Descriptor\HtmlDescriptor;
27
use Symfony\Component\VarDumper\Dumper\CliDumper;
28
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
29
use Symfony\Component\VarDumper\Server\DumpServer;
30
 
31
/**
32
 * Starts a dump server to collect and output dumps on a single place with multiple formats support.
33
 *
34
 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
35
 *
36
 * @final
37
 */
38
#[AsCommand(name: 'server:dump', description: 'Start a dump server that collects and displays dumps in a single place')]
39
class ServerDumpCommand extends Command
40
{
41
    private DumpServer $server;
42
 
43
    /** @var DumpDescriptorInterface[] */
44
    private array $descriptors;
45
 
46
    public function __construct(DumpServer $server, array $descriptors = [])
47
    {
48
        $this->server = $server;
49
        $this->descriptors = $descriptors + [
50
            'cli' => new CliDescriptor(new CliDumper()),
51
            'html' => new HtmlDescriptor(new HtmlDumper()),
52
        ];
53
 
54
        parent::__construct();
55
    }
56
 
57
    protected function configure()
58
    {
59
        $this
60
            ->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', implode(', ', $this->getAvailableFormats())), 'cli')
61
            ->setHelp(<<<'EOF'
62
<info>%command.name%</info> starts a dump server that collects and displays
63
dumps in a single place for debugging you application:
64
 
65
  <info>php %command.full_name%</info>
66
 
67
You can consult dumped data in HTML format in your browser by providing the <comment>--format=html</comment> option
68
and redirecting the output to a file:
69
 
70
  <info>php %command.full_name% --format="html" > dump.html</info>
71
 
72
EOF
73
            )
74
        ;
75
    }
76
 
77
    protected function execute(InputInterface $input, OutputInterface $output): int
78
    {
79
        $io = new SymfonyStyle($input, $output);
80
        $format = $input->getOption('format');
81
 
82
        if (!$descriptor = $this->descriptors[$format] ?? null) {
83
            throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $format));
84
        }
85
 
86
        $errorIo = $io->getErrorStyle();
87
        $errorIo->title('Symfony Var Dumper Server');
88
 
89
        $this->server->start();
90
 
91
        $errorIo->success(sprintf('Server listening on %s', $this->server->getHost()));
92
        $errorIo->comment('Quit the server with CONTROL-C.');
93
 
94
        $this->server->listen(function (Data $data, array $context, int $clientId) use ($descriptor, $io) {
95
            $descriptor->describe($io, $data, $context, $clientId);
96
        });
97
 
98
        return 0;
99
    }
100
 
101
    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
102
    {
103
        if ($input->mustSuggestOptionValuesFor('format')) {
104
            $suggestions->suggestValues($this->getAvailableFormats());
105
        }
106
    }
107
 
108
    private function getAvailableFormats(): array
109
    {
110
        return array_keys($this->descriptors);
111
    }
112
}