appendChild($commandXML = $dom->createElement('command')); $commandXML->setAttribute('id', $command->getName()); $commandXML->setAttribute('name', $command->getName()); // Get the original element and its top-level elements. $originalCommandXML = static::getSingleElementByTagName($dom, $originalDom, 'command'); $originalUsagesXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'usages'); $originalDescriptionXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'description'); $originalHelpXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'help'); $originalArgumentsXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'arguments'); $originalOptionsXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'options'); // Keep only the first of the elements $newUsagesXML = $dom->createElement('usages'); $firstUsageXML = static::getSingleElementByTagName($dom, $originalUsagesXML, 'usage'); $newUsagesXML->appendChild($firstUsageXML); // Create our own elements $newExamplesXML = $dom->createElement('examples'); foreach ($command->getExampleUsages() as $usage => $description) { $newExamplesXML->appendChild($exampleXML = $dom->createElement('example')); $exampleXML->appendChild($usageXML = $dom->createElement('usage', $usage)); $exampleXML->appendChild($descriptionXML = $dom->createElement('description', $description)); } // Create our own elements $newAliasesXML = $dom->createElement('aliases'); foreach ($command->getAliases() as $alias) { $newAliasesXML->appendChild($dom->createElement('alias', $alias)); } // Create our own elements $newTopicsXML = $dom->createElement('topics'); foreach ($command->getTopics() as $topic) { $newTopicsXML->appendChild($topicXML = $dom->createElement('topic', $topic)); } // Place the different elements into the element in the desired order $commandXML->appendChild($newUsagesXML); $commandXML->appendChild($newExamplesXML); $commandXML->appendChild($originalDescriptionXML); $commandXML->appendChild($originalArgumentsXML); $commandXML->appendChild($originalOptionsXML); $commandXML->appendChild($originalHelpXML); $commandXML->appendChild($newAliasesXML); $commandXML->appendChild($newTopicsXML); return $dom; } protected static function getSingleElementByTagName($dom, $parent, $tagName) { // There should always be exactly one '' element. $elements = $parent->getElementsByTagName($tagName); $result = $elements->item(0); $result = $dom->importNode($result, true); return $result; } }