Blame view

vendor/symfony/console/Descriptor/MarkdownDescriptor.php 5.97 KB
6c4edfa3   Alexandre   First Commit LabI...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Console\Descriptor;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
16
use Symfony\Component\Console\Helper\Helper;
6c4edfa3   Alexandre   First Commit LabI...
17
18
19
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
20
use Symfony\Component\Console\Output\OutputInterface;
6c4edfa3   Alexandre   First Commit LabI...
21
22
23
24
25
26
27
28
29
30
31
32
33

/**
 * Markdown descriptor.
 *
 * @author Jean-François Simon <contact@jfsimon.fr>
 *
 * @internal
 */
class MarkdownDescriptor extends Descriptor
{
    /**
     * {@inheritdoc}
     */
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    public function describe(OutputInterface $output, $object, array $options = array())
    {
        $decorated = $output->isDecorated();
        $output->setDecorated(false);

        parent::describe($output, $object, $options);

        $output->setDecorated($decorated);
    }

    /**
     * {@inheritdoc}
     */
    protected function write($content, $decorated = true)
    {
        parent::write($content, $decorated);
    }

    /**
     * {@inheritdoc}
     */
6c4edfa3   Alexandre   First Commit LabI...
55
56
57
    protected function describeInputArgument(InputArgument $argument, array $options = array())
    {
        $this->write(
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
58
59
            '#### `'.($argument->getName() ?: '<none>')."`\n\n"
            .($argument->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $argument->getDescription())."\n\n" : '')
6c4edfa3   Alexandre   First Commit LabI...
60
61
            .'* Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n"
            .'* Is array: '.($argument->isArray() ? 'yes' : 'no')."\n"
6c4edfa3   Alexandre   First Commit LabI...
62
63
64
65
66
67
68
69
70
            .'* Default: `'.str_replace("\n", '', var_export($argument->getDefault(), true)).'`'
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function describeInputOption(InputOption $option, array $options = array())
    {
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
71
72
73
74
75
        $name = '--'.$option->getName();
        if ($option->getShortcut()) {
            $name .= '|-'.implode('|-', explode('|', $option->getShortcut())).'';
        }

6c4edfa3   Alexandre   First Commit LabI...
76
        $this->write(
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
77
78
            '#### `'.$name.'`'."\n\n"
            .($option->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $option->getDescription())."\n\n" : '')
6c4edfa3   Alexandre   First Commit LabI...
79
80
81
            .'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n"
            .'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
            .'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n"
6c4edfa3   Alexandre   First Commit LabI...
82
83
84
85
86
87
88
89
90
91
            .'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`'
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function describeInputDefinition(InputDefinition $definition, array $options = array())
    {
        if ($showArguments = count($definition->getArguments()) > 0) {
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
92
            $this->write('### Arguments');
6c4edfa3   Alexandre   First Commit LabI...
93
94
95
96
97
98
99
100
101
102
103
            foreach ($definition->getArguments() as $argument) {
                $this->write("\n\n");
                $this->write($this->describeInputArgument($argument));
            }
        }

        if (count($definition->getOptions()) > 0) {
            if ($showArguments) {
                $this->write("\n\n");
            }

c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
104
            $this->write('### Options');
6c4edfa3   Alexandre   First Commit LabI...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
            foreach ($definition->getOptions() as $option) {
                $this->write("\n\n");
                $this->write($this->describeInputOption($option));
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function describeCommand(Command $command, array $options = array())
    {
        $command->getSynopsis();
        $command->mergeApplicationDefinition(false);

        $this->write(
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
121
122
123
124
            '`'.$command->getName()."`\n"
            .str_repeat('-', Helper::strlen($command->getName()) + 2)."\n\n"
            .($command->getDescription() ? $command->getDescription()."\n\n" : '')
            .'### Usage'."\n\n"
6c4edfa3   Alexandre   First Commit LabI...
125
            .array_reduce(array_merge(array($command->getSynopsis()), $command->getAliases(), $command->getUsages()), function ($carry, $usage) {
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
126
                return $carry.'* `'.$usage.'`'."\n";
6c4edfa3   Alexandre   First Commit LabI...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
            })
        );

        if ($help = $command->getProcessedHelp()) {
            $this->write("\n");
            $this->write($help);
        }

        if ($command->getNativeDefinition()) {
            $this->write("\n\n");
            $this->describeInputDefinition($command->getNativeDefinition());
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function describeApplication(Application $application, array $options = array())
    {
        $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
        $description = new ApplicationDescription($application, $describedNamespace);
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
148
        $title = $this->getApplicationTitle($application);
6c4edfa3   Alexandre   First Commit LabI...
149

c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
150
        $this->write($title."\n".str_repeat('=', Helper::strlen($title)));
6c4edfa3   Alexandre   First Commit LabI...
151
152
153
154
155
156
157
158

        foreach ($description->getNamespaces() as $namespace) {
            if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
                $this->write("\n\n");
                $this->write('**'.$namespace['id'].':**');
            }

            $this->write("\n\n");
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
159
160
            $this->write(implode("\n", array_map(function ($commandName) use ($description) {
                return sprintf('* [`%s`](#%s)', $commandName, str_replace(':', '', $description->getCommand($commandName)->getName()));
6c4edfa3   Alexandre   First Commit LabI...
161
162
163
164
165
166
167
168
            }, $namespace['commands'])));
        }

        foreach ($description->getCommands() as $command) {
            $this->write("\n\n");
            $this->write($this->describeCommand($command));
        }
    }
c3f2e5a1   Thibaud Ajas   MAJ DE PHP v5.6 -...
169
170
171
172
173
174
175
176
177
178
179
180
181

    private function getApplicationTitle(Application $application)
    {
        if ('UNKNOWN' !== $application->getName()) {
            if ('UNKNOWN' !== $application->getVersion()) {
                return sprintf('%s %s', $application->getName(), $application->getVersion());
            }

            return $application->getName();
        }

        return 'Console Tool';
    }
6c4edfa3   Alexandre   First Commit LabI...
182
}