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