Files
php-qml/framework/php/src/Maker/BridgeReadModelMaker.php
T

143 lines
5.2 KiB
PHP
Raw Normal View History

2026-05-03 20:28:50 +02:00
<?php
declare(strict_types=1);
namespace PhpQml\Bridge\Maker;
use Doctrine\ORM\EntityManagerInterface;
use PhpQml\Bridge\Maker\Support\NameInput;
use PhpQml\Bridge\Maker\Support\Naming;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
/**
* `make:bridge:read-model <Name>` — generates a query-only projection.
*
* Read-models are server-side joined / aggregated views QML reads
* without going through a writable `#[BridgeResource]`. Three files:
*
* - src/ReadModel/<Name>ReadModel.php — query service stub
* - src/Controller/<Name>Controller.php — `GET /api/<kebab-plural>`
* - {qml_path}/<Name>List.qml — `ReactiveListModel` bound to the route,
* no Mercure topic (read-models aren't reactive — invalidation is
* driven by domain events; see `make:bridge:event`).
*/
final class BridgeReadModelMaker extends AbstractMaker
{
public function __construct(
private readonly string $qmlPath = '../qml/',
) {
}
public static function getCommandName(): string
{
return 'make:bridge:read-model';
}
public static function getCommandDescription(): string
{
return 'Generate a read-only projection (query service + GET controller + QML stub).';
}
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->addArgument(
'name',
InputArgument::OPTIONAL,
'CamelCase projection name (e.g. TodoSummary, RecentActivity).',
)
->setHelp(
"Creates three files:\n\n"
." • <info>src/ReadModel/<Name>ReadModel.php</info> — query service (you fill in the body)\n"
." • <info>src/Controller/<Name>Controller.php</info> — GET handler at /api/<kebab-plural>\n"
." • <info>{qml_path}/<Name>List.qml</info> — ReactiveListModel bound to the route\n\n"
."Read-models are not auto-reactive. To refresh from server-side changes,\n"
."generate a paired domain event with <info>make:bridge:event</info>.\n"
);
}
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
NameInput::askOrFail(
$input,
$io,
'name',
'Projection name (CamelCase, e.g. TodoSummary)?',
'Projection name cannot be empty.',
);
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$rawName = (string) $input->getArgument('name');
$singular = ucfirst(Str::asCamelCase($rawName));
$pluralCamel = Str::singularCamelCaseToPluralCamelCase($singular);
$resource = Naming::camelTo($singular, '_');
$route = '/api/'.Naming::camelTo($pluralCamel, '-');
$readModelFqcn = $generator->createClassNameDetails(
$singular,
'ReadModel\\',
'ReadModel',
);
$controllerFqcn = $generator->createClassNameDetails(
$singular,
'Controller\\',
'Controller',
);
$vars = [
'singular' => $singular,
'resource' => $resource,
'route' => $route,
'read_model_short' => $readModelFqcn->getShortName(),
'read_model_fqcn' => $readModelFqcn->getFullName(),
'controller_short' => $controllerFqcn->getShortName(),
];
$generator->generateFile(
'src/ReadModel/'.$readModelFqcn->getShortName().'.php',
__DIR__.'/templates/ReadModel.tpl.php',
$vars,
);
$generator->generateFile(
'src/Controller/'.$controllerFqcn->getShortName().'.php',
__DIR__.'/templates/ReadModelController.tpl.php',
$vars,
);
$qmlTarget = rtrim($this->qmlPath, '/').'/'.$singular.'List.qml';
$generator->generateFile(
$qmlTarget,
__DIR__.'/templates/ReadModelQml.tpl.php',
$vars,
);
$generator->writeChanges();
$this->writeSuccessMessage($io);
$io->text([
'Next:',
" • Implement <info>{$readModelFqcn->getShortName()}::query()</info>.",
" • Use <info>{$singular}List.qml</info> in your QML.",
' • Optionally pair with <info>make:bridge:event</info> for invalidation.',
]);
}
public function configureDependencies(DependencyBuilder $dependencies): void
{
$dependencies->addClassDependency(EntityManagerInterface::class, 'doctrine/orm');
$dependencies->addClassDependency(Route::class, 'symfony/routing');
$dependencies->addClassDependency(JsonResponse::class, 'symfony/http-foundation');
}
}