annotate vendor/consolidation/annotated-command/src/Help/HelpDocumentBuilder.php @ 4:a9cd425dd02b

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