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
|
|
|
$info = [ |
21
|
|
|
'schema' => CommandInfo::SERIALIZATION_SCHEMA_VERSION, |
22
|
|
|
'class' => $className, |
23
|
|
|
'method_name' => $commandInfo->getMethodName(), |
24
|
|
|
'name' => $commandInfo->getName(), |
25
|
|
|
'description' => $commandInfo->getDescription(), |
26
|
|
|
'help' => $commandInfo->getHelp(), |
27
|
|
|
'aliases' => $commandInfo->getAliases(), |
28
|
|
|
'annotations' => $commandInfo->getRawAnnotations()->getArrayCopy(), |
29
|
|
|
'example_usages' => $commandInfo->getExampleUsages(), |
30
|
|
|
'return_type' => $commandInfo->getReturnType(), |
31
|
|
|
'mtime' => filemtime($path), |
32
|
|
|
]; |
33
|
|
|
$info['arguments'] = $this->serializeDefaultsWithDescriptions($commandInfo->arguments()); |
34
|
|
|
$info['options'] = $this->serializeDefaultsWithDescriptions($commandInfo->options()); |
35
|
|
|
|
36
|
|
|
return $info; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function serializeDefaultsWithDescriptions(DefaultsWithDescriptions $defaults) |
40
|
|
|
{ |
41
|
|
|
$result = []; |
42
|
|
|
foreach ($defaults->getValues() as $key => $val) { |
43
|
|
|
$result[$key] = [ |
44
|
|
|
'description' => $defaults->getDescription($key), |
45
|
|
|
]; |
46
|
|
|
if ($defaults->hasDefault($key)) { |
47
|
|
|
$result[$key]['default'] = $val; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|