Installation:
composer require drinksco/console-ui-bundle
yarn add @drinksco/console-ui
Ensure WebpackEncoreBundle is configured in your Symfony app (default).
Enable Bundle:
Add to config/bundles.php:
DrinksCo\ConsoleUIBundle\DrinksCoConsoleUIBundle::class => ['all' => true],
First Use Case:
use DrinksCo\ConsoleUIBundle\Attribute\ConsoleUI;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[ConsoleUI]
class MyCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Hello from UI!');
return Command::SUCCESS;
}
}
/_console-ui (default route).Command Execution:
#[ConsoleUI] attribute to auto-register commands for UI access.Real-Time Output:
Mercure or raw sockets).Integration with Symfony:
public function __construct(private MyService $service) {}
console.command events for pre/post-processing.
$event->getCommand()->setUiEnabled(true); // Enable UI for specific commands
Custom UI Components:
templates/bundles/consoleui/ (e.g., command_list.html.twig).Authentication:
/_console-ui route with Symfony’s security system:
# config/routes.yaml
_console_ui:
path: /_console-ui
controller: DrinksCo\ConsoleUIBundle\Controller\ConsoleUIController::index
methods: [GET]
security: true
Command Groups:
Use Symfony’s #[Command] groups to categorize commands in the UI:
#[Command(name: 'app:group:subcommand', group: 'app:group')]
Async Execution: Offload long-running commands to a queue (e.g., Symfony Messenger) and poll results via the UI.
Shared State:
Pass data between the UI and command via Symfony’s ParameterBag or session:
$input->getOption('ui-data'); // Parse JSON from the UI
WebSocket Limits:
TypeScript Dependencies:
yarn build --watch
Command Registration:
autoconfigure: true in config/services.yaml:
services:
_controllers:
resource: ../src/Controller/
autoconfigure: true
_commands:
resource: ../src/Command/
tags: ['console.command']
autoconfigure: true
Input Handling:
$input->getArgument('id') ?? throw new \InvalidArgumentException('ID required');
CORS Issues:
# config/packages/security.yaml
cors:
paths:
'^/_console-ui/':
allow_origin: ['*']
Check WebSocket Connection:
Use browser dev tools (Network tab) to verify WebSocket upgrades (ws:// or wss://).
WebSocket connection failed → Check Symfony’s kernel.debug mode or server config.Log Output:
Enable debug mode (APP_DEBUG=1) to see raw command output in Symfony’s log:
tail -f var/log/dev.log
UI Assets: Clear Encore assets after updates:
yarn encore dev --watch
Custom Templates:
Override the UI layout in templates/bundles/consoleui/base.html.twig.
Add Command Metadata:
Use Symfony’s #[Command] attributes to enrich the UI:
#[Command(
description: 'Processes files (UI-friendly)',
help: 'See <info>https://example.com</info> for details.'
)]
Hook into UI Events:
Listen for console.ui.command.execute events to modify behavior:
use DrinksCo\ConsoleUIBundle\Event\ConsoleUIEvent;
public function onCommandExecute(ConsoleUIEvent $event) {
$event->setOutput(new StreamedOutput()); // Custom output handler
}
Localization: Translate command descriptions/options using Symfony’s translation system:
{{ 'app.command.description'|trans }}
How can I help you explore Laravel packages today?