<?php

namespace app\controller\admin\system\dev;

use app\BaseController;
use think\facade\Console;
use think\response\Json;

class CommandController extends BaseController
{
    public function list(): Json
    {
        $all = Console::all();
        $lists = [];
        foreach ($all as $cmdName => $class) {
            $names = explode(':', $cmdName);
            $group = '';
            if(count($names) > 1){
                $group = $names[0];
            }
            if(is_string($class)){
                $classObj = Console::getCommand($cmdName);
                $className = $class;
            }else{
                $classObj = $class;
                $className = get_class($class);
            }
            $className = ltrim($className, '\\');
            $type = "";
            if(str_starts_with($className, 'think\\')){
                $type = "think";
            }

            $lists[$group][] = [
                'name'       => $cmdName,
                'type'       => $type,
                'group'      => $group,
                'class'      => $className,
                'description'=> $classObj->getDescription(),
                'aliases'    => $classObj->getAliases(),
                'arguments'  => $classObj->getDefinition()->getArguments(),
                'options'    => $classObj->getDefinition()->getOptions()
            ];
        }
        $results = [];
        foreach ($lists as $group => $list) {
            foreach ($list as $item) {
                $results[] = $item;
            }
        }
        return $this->writeSuccess('ok', $results);
    }
}