CommandInfoSerializer.php 2.26 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<?php
namespace Consolidation\AnnotatedCommand\Parser;

use Symfony\Component\Console\Input\InputOption;
use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParser;
use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParserFactory;
use Consolidation\AnnotatedCommand\AnnotationData;

/**
 * Serialize a CommandInfo object
 */
class CommandInfoSerializer
{
    public function serialize(CommandInfo $commandInfo)
    {
        $allAnnotations = $commandInfo->getAnnotations();
        $path = $allAnnotations['_path'];
        $className = $allAnnotations['_classname'];

        // Include the minimum information for command info (including placeholder records)
        $info = [
            'schema' => CommandInfo::SERIALIZATION_SCHEMA_VERSION,
            'class' => $className,
            'method_name' => $commandInfo->getMethodName(),
            'mtime' => filemtime($path),
            'injected_classes' => [],
        ];

        // If this is a valid method / hook, then add more information.
        if ($commandInfo->valid()) {
            $info += [
                'name' => $commandInfo->getName(),
                'description' => $commandInfo->getDescription(),
                'help' => $commandInfo->getHelp(),
                'aliases' => $commandInfo->getAliases(),
                'annotations' => $commandInfo->getRawAnnotations()->getArrayCopy(),
                'example_usages' => $commandInfo->getExampleUsages(),
                'return_type' => $commandInfo->getReturnType(),
            ];
            $info['arguments'] = $this->serializeDefaultsWithDescriptions($commandInfo->arguments());
            $info['options'] = $this->serializeDefaultsWithDescriptions($commandInfo->options());
            $info['injected_classes'] = $commandInfo->getInjectedClasses();
        }

        return $info;
    }

    protected function serializeDefaultsWithDescriptions(DefaultsWithDescriptions $defaults)
    {
        $result = [];
        foreach ($defaults->getValues() as $key => $val) {
            $result[$key] = [
                'description' => $defaults->getDescription($key),
            ];
            if ($defaults->hasDefault($key)) {
                $result[$key]['default'] = $val;
            }
        }
        return $result;
    }
}