annotate vendor/consolidation/annotated-command/src/Parser/CommandInfoSerializer.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@0 1 <?php
Chris@0 2 namespace Consolidation\AnnotatedCommand\Parser;
Chris@0 3
Chris@0 4 use Symfony\Component\Console\Input\InputOption;
Chris@0 5 use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParser;
Chris@0 6 use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParserFactory;
Chris@0 7 use Consolidation\AnnotatedCommand\AnnotationData;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Serialize a CommandInfo object
Chris@0 11 */
Chris@0 12 class CommandInfoSerializer
Chris@0 13 {
Chris@0 14 public function serialize(CommandInfo $commandInfo)
Chris@0 15 {
Chris@0 16 $allAnnotations = $commandInfo->getAnnotations();
Chris@0 17 $path = $allAnnotations['_path'];
Chris@0 18 $className = $allAnnotations['_classname'];
Chris@0 19
Chris@0 20 // Include the minimum information for command info (including placeholder records)
Chris@0 21 $info = [
Chris@0 22 'schema' => CommandInfo::SERIALIZATION_SCHEMA_VERSION,
Chris@0 23 'class' => $className,
Chris@0 24 'method_name' => $commandInfo->getMethodName(),
Chris@0 25 'mtime' => filemtime($path),
Chris@17 26 'injected_classes' => [],
Chris@0 27 ];
Chris@0 28
Chris@0 29 // If this is a valid method / hook, then add more information.
Chris@0 30 if ($commandInfo->valid()) {
Chris@0 31 $info += [
Chris@0 32 'name' => $commandInfo->getName(),
Chris@0 33 'description' => $commandInfo->getDescription(),
Chris@0 34 'help' => $commandInfo->getHelp(),
Chris@0 35 'aliases' => $commandInfo->getAliases(),
Chris@0 36 'annotations' => $commandInfo->getRawAnnotations()->getArrayCopy(),
Chris@0 37 'example_usages' => $commandInfo->getExampleUsages(),
Chris@0 38 'return_type' => $commandInfo->getReturnType(),
Chris@0 39 ];
Chris@0 40 $info['arguments'] = $this->serializeDefaultsWithDescriptions($commandInfo->arguments());
Chris@0 41 $info['options'] = $this->serializeDefaultsWithDescriptions($commandInfo->options());
Chris@17 42 $info['injected_classes'] = $commandInfo->getInjectedClasses();
Chris@0 43 }
Chris@0 44
Chris@0 45 return $info;
Chris@0 46 }
Chris@0 47
Chris@0 48 protected function serializeDefaultsWithDescriptions(DefaultsWithDescriptions $defaults)
Chris@0 49 {
Chris@0 50 $result = [];
Chris@0 51 foreach ($defaults->getValues() as $key => $val) {
Chris@0 52 $result[$key] = [
Chris@0 53 'description' => $defaults->getDescription($key),
Chris@0 54 ];
Chris@0 55 if ($defaults->hasDefault($key)) {
Chris@0 56 $result[$key]['default'] = $val;
Chris@0 57 }
Chris@0 58 }
Chris@0 59 return $result;
Chris@0 60 }
Chris@0 61 }