phpnomad / symfony-console-integration
Package info
github.com/phpnomad/symfony-console-integration
pkg:composer/phpnomad/symfony-console-integration
Requires
- phpnomad/console: ^1.0
- phpnomad/loader: ^1.0 || ^2.0
- phpnomad/logger: ^1.0
- phpnomad/utils: ^1.0
- symfony/console: ^7.2
README
Integrates Symfony Console with PHPNomad's console abstraction. Your commands implement phpnomad/console's Command interface and declare their arguments through PHPNomad's signature syntax. This package parses those signatures, registers the commands with a Symfony Application, and runs handle() through any declared middleware and interceptors. Your command classes never reference Symfony directly.
Installation
composer require phpnomad/symfony-console-integration
What This Provides
ConsoleStrategyparses a PHPNomad signature into SymfonyInputArgumentandInputOptiondefinitions, wraps the command in an anonymous SymfonyCommandsubclass, and runshandle()through anyHasMiddlewareandHasInterceptorshooks.ConsoleExceptionfailures are logged viaLoggerStrategyand surfaced throughOutputStrategy::error()with exit code 1.ConsoleOutputStrategymapswriteln,info,success,warning,error,newline, andtableonto a SymfonyOutputInterfacewith blue, green, yellow, and red formatter styles.Inputwraps a SymfonyInputInterfacebehind PHPNomad'sInputcontract, with an override map so middleware can mutate values without touching the Symfony request.Initializeris aphpnomad/loaderinitializer that binds the strategies to their PHPNomad interfaces and provides a singleton SymfonyOutputInterface(aConsoleOutputwith the default formatter).
Requirements
phpnomad/console, the contract this package implementssymfony/console^7.2, the library this package bridgesphpnomad/loggerfor exception logging inside the command wrapperphpnomad/utilsandphpnomad/loaderfor bootstrapping
Usage
Bind a Symfony Application into your container, pass this package's Initializer to your Bootstrapper alongside any initializer that declares commands via HasCommands, then call run() on the resolved ConsoleStrategy:
<?php use MyApp\Commands\CommandsInitializer; use PHPNomad\Console\Interfaces\ConsoleStrategy; use PHPNomad\Loader\Bootstrapper; use PHPNomad\Symfony\Component\Console\Initializer as SymfonyConsoleInitializer; use Symfony\Component\Console\Application; require __DIR__ . '/../vendor/autoload.php'; $container = require __DIR__ . '/../bootstrap/container.php'; $container->bindSingletonFromFactory( Application::class, fn() => new Application('MyApp CLI') ); $bootstrapper = new Bootstrapper( $container, new SymfonyConsoleInitializer(), new CommandsInitializer() ); $bootstrapper->load(); $container->get(ConsoleStrategy::class)->run();
CommandsInitializer is any class implementing HasCommands. Its getCommands() returns an array of command class names, and the loader registers each one automatically. Each command class implements Command, with getSignature() returning a string like widget:create {name:The widget name} {--force} and handle(Input $input) running the work.
Documentation
PHPNomad documentation lives at phpnomad.com. For the underlying library, see the Symfony Console documentation.
License
Licensed under the MIT License.