Installation:
composer require draw/console
Add the bundle to config/bundles.php (Symfony) or AppKernel.php (legacy Laravel <5.5):
Draw\Component\Console\DrawConsoleBundle::class => ['all' => true],
Configuration:
Add the minimal config to config/packages/draw_command.yaml (Symfony) or config/draw_command.php (Laravel):
draw_command:
sonata:
commands:
clearCache:
commandName: "cache:clear"
label: "Clear Cache"
icon: "fa-ban"
First Use Case:
php artisan your:command --output=buffered
(Note: Laravel doesn’t natively support Symfony bundles, so this assumes a Laravel-Symfony hybrid setup or a custom CLI wrapper.)Sonata Admin Integration:
draw_command.yaml with commandName, label, and icon.sonata-admin bundle).cache:clear and logs execution (output, exit code, duration).CLI Logging:
BufferedConsoleOutput to capture logs:
$output = new \Draw\Component\Console\Output\BufferedConsoleOutput();
$application->run($input, $output);
command_execution table).Laravel-Specific Adaptation:
Artisan::call() to log executions:
use Draw\Component\Console\Output\BufferedConsoleOutput;
$output = new BufferedConsoleOutput();
$exitCode = Artisan::call('command:name', [], $output);
// Save $output->getContent() to DB.
Illuminate\Console\Events\ArtisanStarting to auto-log commands.Dynamic Command Arguments:
reIndexSearch:
commandName: "fos:elastica:populate"
arguments: ["--env=prod"]
Database Schema:
command_execution table. If missing, create it manually or extend the bundle to auto-migrate (e.g., via Doctrine migrations).CREATE TABLE command_execution (
id INT AUTO_INCREMENT PRIMARY KEY,
command_name VARCHAR(255),
output TEXT,
exit_code INT,
duration INT,
created_at TIMESTAMP
);
Ignored Commands:
help, doctrine:database:drop, etc.) cannot be customized without patching the bundle. Override via a custom logger:
// config/draw_command.php
'logger' => [
'ignored_commands' => ['custom:ignored-command'],
],
Laravel Compatibility:
BufferedConsoleOutput with Artisan commands.Command class to integrate with Laravel’s Artisan facade.Artisan::call() doesn’t natively support Symfony’s Application. Use a wrapper:
class ConsoleLogger
{
public function log(string $command, array $args = []): string
{
$output = new BufferedConsoleOutput();
Artisan::call($command, $args, $output);
return $output->getContent();
}
}
Output Buffering:
BufferedConsoleOutput captures only the output passed to it. Ensure all CLI output (e.g., echo, dd()) uses the buffered output:
// Wrong: Uses default output
echo "Debug: " . $var;
// Right: Uses buffered output
$output->writeln("Debug: " . $var);
Debugging Failed Commands:
command_execution table for failed runs (exit_code != 0) and inspect output for errors.SELECT * FROM command_execution WHERE exit_code != 0 ORDER BY created_at DESC LIMIT 10;
Extending Ignored Commands:
# config/services.yaml
Draw\Component\Console\Logger\CommandLogger:
arguments:
$ignoredCommands: ['%kernel.environment%:debug', 'custom:command']
Performance:
cache:clear) asynchronously to avoid blocking the admin UI. Use Laravel’s queues or Symfony’s Messenger component.Security:
sonata_admin roles).rm -rf).Testing:
BufferedConsoleOutput in PHPUnit:
$output = $this->createMock(BufferedConsoleOutput::class);
$output->method('getContent')->willReturn('Mocked output');
How can I help you explore Laravel packages today?