annotate vendor/consolidation/annotated-command/src/Help/HelpDocumentBuilder.php @ 19:fa3358dc1485 tip

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