This commit is contained in:
2023-11-30 17:02:21 +08:00
parent 314fadca7f
commit 4740eb1ca9
2 changed files with 54 additions and 63 deletions
+44 -49
View File
@@ -4,10 +4,9 @@ declare(strict_types=1);
namespace Gii;
use Database\DatabasesProviders;
use Exception;
use Kiri;
use Kiri\Di\LocalService;
use Kiri\Exception\ConfigException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@@ -20,59 +19,55 @@ use Symfony\Component\Console\Output\OutputInterface;
class GiiCommand extends Command
{
public string $command = 'sw:gii';
public string $command = 'sw:gii';
public string $description = './snowflake sw:gii make=model|controller|task|interceptor|limits|middleware name=xxxx';
public string $description = './snowflake sw:gii make=model|controller|task|interceptor|limits|middleware name=xxxx';
private LocalService $service;
/**
*
*/
protected function configure()
{
$this->setName('sw:gii')
->addOption('make', 'm', InputArgument::OPTIONAL)
->addOption('table', 't', InputArgument::OPTIONAL)
->addOption('database', 'd', InputArgument::OPTIONAL)
->setDescription('php kiri.php sw:gii --table u_user --database users --make model');
}
/**
*
*/
protected function configure()
{
$this->service = Kiri::getDi()->get(LocalService::class);
$this->setName('sw:gii')
->addOption('make', 'm', InputArgument::OPTIONAL)
->addOption('table', 't', InputArgument::OPTIONAL)
->addOption('database', 'd', InputArgument::OPTIONAL)
->setDescription('php kiri.php sw:gii --table u_user --database users --make model');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws Exception
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
try {
$database = Kiri::getDi()->get(DatabasesProviders::class);
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws Exception
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
try {
/** @var Gii $gii */
$gii = $this->service->get('gii');
if (($db = $input->getOption('database')) != null) {
$gii->run($this->service->get($db), $input);
} else {
$action = $input->getOption('make');
if (!in_array($action, ['model', 'controller'])) {
$gii->run(null, $input);
} else {
$array = [];
foreach (\config('databases.connections') as $key => $connection) {
$array[$key] = $gii->run($this->service->get($key), $input);
}
$output->writeln(json_encode($array, JSON_UNESCAPED_UNICODE));
}
}
} catch (\Throwable $throwable) {
$output->writeln(throwable($throwable));
} finally {
return 1;
}
}
/** @var Gii $gii */
$gii = Kiri::getDi()->get(Gii::class);
if (($db = $input->getOption('database')) != null) {
return count($gii->run($database->get($db), $input));
}
$action = $input->getOption('make');
if (!in_array($action, ['model', 'controller'])) {
return count($gii->run(null, $input));
}
$array = [];
foreach (\config('databases.connections') as $key => $connection) {
$array[$key] = $gii->run($database->get($key), $input);
}
$output->writeln(json_encode($array, JSON_UNESCAPED_UNICODE));
return 0;
} catch (\Throwable $throwable) {
$output->writeln(throwable($throwable));
return 1;
}
}
}
+10 -14
View File
@@ -6,7 +6,6 @@ namespace Gii;
use Kiri\Abstracts\Providers;
use Kiri\Di\LocalService;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\Console\Application;
@@ -19,17 +18,14 @@ class GiiProviders extends Providers
{
/**
* @param LocalService $application
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function onImport(LocalService $application): void
{
$application->set('gii', ['class' => Gii::class]);
$console = $this->container->get(Application::class);
$console->add($this->container->get(GiiCommand::class));
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function onImport(): void
{
$console = $this->container->get(Application::class);
$console->add($this->container->get(GiiCommand::class));
}
}