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\Console\Descriptor;
13
 
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputDefinition;
18
use Symfony\Component\Console\Input\InputOption;
19
 
20
/**
21
 * XML descriptor.
22
 *
23
 * @author Jean-François Simon <contact@jfsimon.fr>
24
 *
25
 * @internal
26
 */
27
class XmlDescriptor extends Descriptor
28
{
29
    public function getInputDefinitionDocument(InputDefinition $definition): \DOMDocument
30
    {
31
        $dom = new \DOMDocument('1.0', 'UTF-8');
32
        $dom->appendChild($definitionXML = $dom->createElement('definition'));
33
 
34
        $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
35
        foreach ($definition->getArguments() as $argument) {
36
            $this->appendDocument($argumentsXML, $this->getInputArgumentDocument($argument));
37
        }
38
 
39
        $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
40
        foreach ($definition->getOptions() as $option) {
41
            $this->appendDocument($optionsXML, $this->getInputOptionDocument($option));
42
        }
43
 
44
        return $dom;
45
    }
46
 
47
    public function getCommandDocument(Command $command, bool $short = false): \DOMDocument
48
    {
49
        $dom = new \DOMDocument('1.0', 'UTF-8');
50
        $dom->appendChild($commandXML = $dom->createElement('command'));
51
 
52
        $commandXML->setAttribute('id', $command->getName());
53
        $commandXML->setAttribute('name', $command->getName());
54
        $commandXML->setAttribute('hidden', $command->isHidden() ? 1 : 0);
55
 
56
        $commandXML->appendChild($usagesXML = $dom->createElement('usages'));
57
 
58
        $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
59
        $descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getDescription())));
60
 
61
        if ($short) {
62
            foreach ($command->getAliases() as $usage) {
63
                $usagesXML->appendChild($dom->createElement('usage', $usage));
64
            }
65
        } else {
66
            $command->mergeApplicationDefinition(false);
67
 
68
            foreach (array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()) as $usage) {
69
                $usagesXML->appendChild($dom->createElement('usage', $usage));
70
            }
71
 
72
            $commandXML->appendChild($helpXML = $dom->createElement('help'));
73
            $helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
74
 
75
            $definitionXML = $this->getInputDefinitionDocument($command->getDefinition());
76
            $this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
77
        }
78
 
79
        return $dom;
80
    }
81
 
82
    public function getApplicationDocument(Application $application, string $namespace = null, bool $short = false): \DOMDocument
83
    {
84
        $dom = new \DOMDocument('1.0', 'UTF-8');
85
        $dom->appendChild($rootXml = $dom->createElement('symfony'));
86
 
87
        if ('UNKNOWN' !== $application->getName()) {
88
            $rootXml->setAttribute('name', $application->getName());
89
            if ('UNKNOWN' !== $application->getVersion()) {
90
                $rootXml->setAttribute('version', $application->getVersion());
91
            }
92
        }
93
 
94
        $rootXml->appendChild($commandsXML = $dom->createElement('commands'));
95
 
96
        $description = new ApplicationDescription($application, $namespace, true);
97
 
98
        if ($namespace) {
99
            $commandsXML->setAttribute('namespace', $namespace);
100
        }
101
 
102
        foreach ($description->getCommands() as $command) {
103
            $this->appendDocument($commandsXML, $this->getCommandDocument($command, $short));
104
        }
105
 
106
        if (!$namespace) {
107
            $rootXml->appendChild($namespacesXML = $dom->createElement('namespaces'));
108
 
109
            foreach ($description->getNamespaces() as $namespaceDescription) {
110
                $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
111
                $namespaceArrayXML->setAttribute('id', $namespaceDescription['id']);
112
 
113
                foreach ($namespaceDescription['commands'] as $name) {
114
                    $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
115
                    $commandXML->appendChild($dom->createTextNode($name));
116
                }
117
            }
118
        }
119
 
120
        return $dom;
121
    }
122
 
123
    protected function describeInputArgument(InputArgument $argument, array $options = [])
124
    {
125
        $this->writeDocument($this->getInputArgumentDocument($argument));
126
    }
127
 
128
    protected function describeInputOption(InputOption $option, array $options = [])
129
    {
130
        $this->writeDocument($this->getInputOptionDocument($option));
131
    }
132
 
133
    protected function describeInputDefinition(InputDefinition $definition, array $options = [])
134
    {
135
        $this->writeDocument($this->getInputDefinitionDocument($definition));
136
    }
137
 
138
    protected function describeCommand(Command $command, array $options = [])
139
    {
140
        $this->writeDocument($this->getCommandDocument($command, $options['short'] ?? false));
141
    }
142
 
143
    protected function describeApplication(Application $application, array $options = [])
144
    {
145
        $this->writeDocument($this->getApplicationDocument($application, $options['namespace'] ?? null, $options['short'] ?? false));
146
    }
147
 
148
    /**
149
     * Appends document children to parent node.
150
     */
151
    private function appendDocument(\DOMNode $parentNode, \DOMNode $importedParent)
152
    {
153
        foreach ($importedParent->childNodes as $childNode) {
154
            $parentNode->appendChild($parentNode->ownerDocument->importNode($childNode, true));
155
        }
156
    }
157
 
158
    /**
159
     * Writes DOM document.
160
     */
161
    private function writeDocument(\DOMDocument $dom)
162
    {
163
        $dom->formatOutput = true;
164
        $this->write($dom->saveXML());
165
    }
166
 
167
    private function getInputArgumentDocument(InputArgument $argument): \DOMDocument
168
    {
169
        $dom = new \DOMDocument('1.0', 'UTF-8');
170
 
171
        $dom->appendChild($objectXML = $dom->createElement('argument'));
172
        $objectXML->setAttribute('name', $argument->getName());
173
        $objectXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
174
        $objectXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
175
        $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
176
        $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
177
 
178
        $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
179
        $defaults = \is_array($argument->getDefault()) ? $argument->getDefault() : (\is_bool($argument->getDefault()) ? [var_export($argument->getDefault(), true)] : ($argument->getDefault() ? [$argument->getDefault()] : []));
180
        foreach ($defaults as $default) {
181
            $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
182
            $defaultXML->appendChild($dom->createTextNode($default));
183
        }
184
 
185
        return $dom;
186
    }
187
 
188
    private function getInputOptionDocument(InputOption $option): \DOMDocument
189
    {
190
        $dom = new \DOMDocument('1.0', 'UTF-8');
191
 
192
        $dom->appendChild($objectXML = $dom->createElement('option'));
193
        $objectXML->setAttribute('name', '--'.$option->getName());
194
        $pos = strpos($option->getShortcut() ?? '', '|');
195
        if (false !== $pos) {
196
            $objectXML->setAttribute('shortcut', '-'.substr($option->getShortcut(), 0, $pos));
197
            $objectXML->setAttribute('shortcuts', '-'.str_replace('|', '|-', $option->getShortcut()));
198
        } else {
199
            $objectXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
200
        }
201
        $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
202
        $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
203
        $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
204
        $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
205
        $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
206
 
207
        if ($option->acceptValue()) {
208
            $defaults = \is_array($option->getDefault()) ? $option->getDefault() : (\is_bool($option->getDefault()) ? [var_export($option->getDefault(), true)] : ($option->getDefault() ? [$option->getDefault()] : []));
209
            $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
210
 
211
            if (!empty($defaults)) {
212
                foreach ($defaults as $default) {
213
                    $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
214
                    $defaultXML->appendChild($dom->createTextNode($default));
215
                }
216
            }
217
        }
218
 
219
        if ($option->isNegatable()) {
220
            $dom->appendChild($objectXML = $dom->createElement('option'));
221
            $objectXML->setAttribute('name', '--no-'.$option->getName());
222
            $objectXML->setAttribute('shortcut', '');
223
            $objectXML->setAttribute('accept_value', 0);
224
            $objectXML->setAttribute('is_value_required', 0);
225
            $objectXML->setAttribute('is_multiple', 0);
226
            $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
227
            $descriptionXML->appendChild($dom->createTextNode('Negate the "--'.$option->getName().'" option'));
228
        }
229
 
230
        return $dom;
231
    }
232
}