Skip to content

Commit

Permalink
Table output for activerecords:install
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 30, 2023
1 parent 751f7ff commit 52a73d0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
2 changes: 2 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ services:

ICanBoogie\Binding\ActiveRecord\Console\InstallCommand:
class: ICanBoogie\Binding\ActiveRecord\Console\InstallCommand
arguments:
$style: '%console.style.table%'
tags: [ console.command ]
55 changes: 36 additions & 19 deletions lib/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@
use ICanBoogie\ActiveRecord\ModelProvider;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

use function array_filter;
use function sprintf;

#[AsCommand('activerecord:install', "Install models")]
final class InstallCommand extends Command
{
public function __construct(
private readonly ModelProvider $models,
private readonly ModelIterator $iterator,
private readonly string $style,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
/**
* @var array<class-string<ActiveRecord>, true|null> $tried
*/
/** @var array<class-string<ActiveRecord>, bool> $tried */
$tried = [];

$recursive_install = function (string $activerecord_class) use (&$recursive_install, &$tried, $output): bool {
/** @var array<class-string<ActiveRecord>, true> $installed */
$installed = [];
/** @var array<class-string<ActiveRecord>, true> $already_installed */
$already_installed = [];
/** @var array<class-string<ActiveRecord>, string> $errors */
$errors = [];

$recursive_install = function (string $activerecord_class) use (
&$recursive_install,
&$tried,
&$installed,
&$already_installed,
&$errors,
): bool {
/** @var class-string<ActiveRecord> $activerecord_class */

if (isset($tried[$activerecord_class])) {
Expand All @@ -41,7 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$model = $this->models->model_for_record($activerecord_class);

if ($model->is_installed()) {
$output->writeln("<info>Model already installed: $activerecord_class</info>");
$already_installed[$activerecord_class] = true;

return $tried[$activerecord_class] = true;
}
Expand All @@ -52,35 +61,43 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$rc = $recursive_install($parent_activerecord_class);

if (!$rc) {
$output->writeln(
sprintf(
"<error>Unable to install model '%s', parent install failed: '%s'",
$activerecord_class,
$parent_activerecord_class
)
);
$errors[$activerecord_class] = "Parent install failed: '$parent_activerecord_class'";

return $tried[$activerecord_class] = false;
}
}

try {
$model->install();
$output->writeln("<info>Model installed: $activerecord_class</info>");
$installed[$activerecord_class] = true;

return $tried[$activerecord_class] = true;
} catch (Throwable $e) {
$output->writeln("<error>Unable to install model '$activerecord_class': {$e->getMessage()}</error>");
$errors[$activerecord_class] = $e->getMessage();
}

return $tried[$activerecord_class] = false;
};

$rows = [];

foreach ($this->iterator->model_iterator() as $activerecord_class => $_) {
$recursive_install($activerecord_class);

$rows[] = [
$activerecord_class,
isset($already_installed[$activerecord_class]) ? "Already" : (isset($installed[$activerecord_class]) ? "Yes" : "No"),
$errors[$activerecord_class] ?? "",
];
}

return count(array_filter($tried, fn($v) => $v === false))
$table = new Table($output);
$table->setHeaders([ 'Record', 'Installed', 'Error' ]);
$table->setRows($rows);
$table->setStyle($this->style);
$table->render();

return count($errors)
? Command::FAILURE
: Command::SUCCESS;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/Console/InstallCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Test\ICanBoogie\Binding\ActiveRecord\Console;

use ICanBoogie\Binding\ActiveRecord\Console\InstallCommand;
use ICanBoogie\Console\Test\CommandTestCase;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Node;

final class InstallCommandTest extends CommandTestCase
{
public static function provideExecute(): array
{
return [

[
'activerecord:install',
InstallCommand::class,
[],
[
Node::class,
"Yes",
""
]
],

];
}
}

0 comments on commit 52a73d0

Please sign in to comment.