1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\AnnotatedCommand\Help; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Application; |
5
|
|
|
use Symfony\Component\Console\Descriptor\XmlDescriptor; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Consolidation\AnnotatedCommand\AnnotatedCommand; |
9
|
|
|
|
10
|
|
|
class HelpDocumentBuilder |
11
|
|
|
{ |
12
|
|
|
public static function alter(\DomDocument $originalDom, AnnotatedCommand $command) |
13
|
|
|
{ |
14
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
15
|
|
|
$dom->appendChild($commandXML = $dom->createElement('command')); |
16
|
|
|
$commandXML->setAttribute('id', $command->getName()); |
17
|
|
|
$commandXML->setAttribute('name', $command->getName()); |
18
|
|
|
|
19
|
|
|
// Get the original <command> element and its top-level elements. |
20
|
|
|
$originalCommandXML = static::getSingleElementByTagName($dom, $originalDom, 'command'); |
21
|
|
|
$originalUsagesXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'usages'); |
22
|
|
|
$originalDescriptionXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'description'); |
23
|
|
|
$originalHelpXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'help'); |
24
|
|
|
$originalArgumentsXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'arguments'); |
25
|
|
|
$originalOptionsXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'options'); |
26
|
|
|
|
27
|
|
|
// Keep only the first of the <usage> elements |
28
|
|
|
$newUsagesXML = $dom->createElement('usages'); |
29
|
|
|
$firstUsageXML = static::getSingleElementByTagName($dom, $originalUsagesXML, 'usage'); |
30
|
|
|
$newUsagesXML->appendChild($firstUsageXML); |
31
|
|
|
|
32
|
|
|
// Create our own <example> elements |
33
|
|
|
$newExamplesXML = $dom->createElement('examples'); |
34
|
|
|
foreach ($command->getExampleUsages() as $usage => $description) { |
35
|
|
|
$newExamplesXML->appendChild($exampleXML = $dom->createElement('example')); |
36
|
|
|
$exampleXML->appendChild($usageXML = $dom->createElement('usage', $usage)); |
37
|
|
|
$exampleXML->appendChild($descriptionXML = $dom->createElement('description', $description)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Create our own <alias> elements |
41
|
|
|
$newAliasesXML = $dom->createElement('aliases'); |
42
|
|
|
foreach ($command->getAliases() as $alias) { |
43
|
|
|
$newAliasesXML->appendChild($dom->createElement('alias', $alias)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Create our own <topic> elements |
47
|
|
|
$newTopicsXML = $dom->createElement('topics'); |
48
|
|
|
foreach ($command->getTopics() as $topic) { |
49
|
|
|
$newTopicsXML->appendChild($topicXML = $dom->createElement('topic', $topic)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Place the different elements into the <command> element in the desired order |
53
|
|
|
$commandXML->appendChild($newUsagesXML); |
54
|
|
|
$commandXML->appendChild($newExamplesXML); |
55
|
|
|
$commandXML->appendChild($originalDescriptionXML); |
56
|
|
|
$commandXML->appendChild($originalArgumentsXML); |
57
|
|
|
$commandXML->appendChild($originalOptionsXML); |
58
|
|
|
$commandXML->appendChild($originalHelpXML); |
59
|
|
|
$commandXML->appendChild($newAliasesXML); |
60
|
|
|
$commandXML->appendChild($newTopicsXML); |
61
|
|
|
|
62
|
|
|
return $dom; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
protected static function getSingleElementByTagName($dom, $parent, $tagName) |
67
|
|
|
{ |
68
|
|
|
// There should always be exactly one '<command>' element. |
69
|
|
|
$elements = $parent->getElementsByTagName($tagName); |
70
|
|
|
$result = $elements->item(0); |
71
|
|
|
|
72
|
|
|
$result = $dom->importNode($result, true); |
73
|
|
|
|
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|