sanmai / console
Ready-made extensible console application using Symfony Console component
Fund package maintenance!
sanmai
Requires
- php: >=8.2
- sanmai/later: ^0.1.7
- sanmai/pipeline: ^6.17
- sanmai/version-info: ^0.2
- symfony/console: ^6.0 || ^7.0
Requires (Dev)
- composer/composer: ^2.2
- ergebnis/composer-normalize: ^2.8
- friendsofphp/php-cs-fixer: ^3.17
- infection/infection: >=0.29
- league/pipeline: ^0.3 || ^1.0
- php-coveralls/php-coveralls: ^2.4.1
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2
- phpunit/phpunit: >=9.4 <12
- symfony/process: ^7.3
- vimeo/psalm: >=2
This package is auto-updated.
Last update: 2025-06-27 17:41:58 UTC
README
Zero-configuration console executable that auto-discovers your Symfony Console commands.
Requirements
- Composer with optimized autoloader (
composer dump-autoload --optimize
)
Installation
composer require sanmai/console composer dump-autoload --optimize
Why
--optimize
? Command discovery uses Composer's classmap, which requires an optimized autoloader.
Quick Start
- Create a Symfony Console command:
<?php // src/Commands/HelloCommand.php namespace App\Commands; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; #[AsCommand( name: 'hello', description: 'Says hello' )] class HelloCommand extends Command { // Avoid side effects in constructors - commands are instantiated during discovery. protected function execute(InputInterface $input, OutputInterface $output): int { $output->writeln('Hello, World!'); return Command::SUCCESS; } }
- Update the autoloader:
composer dump-autoload --optimize
- Run your command:
vendor/bin/console hello
That's it! No configuration files, no manual command registration.
How It Works
This library provides a ready-made vendor/bin/console
executable that automatically discovers all Symfony Console commands in your project by:
- Scanning Composer's optimized classmap
- Finding classes that end with
Command.php
- Loading those that extend
Symfony\Component\Console\Command\Command
- Filtering out vendored files
- Instantiating each command (commands that throw exceptions or errors are skipped)
The Problem It Solves
Even with Symfony's built-in command discovery, you still need to:
- Create a console executable file (e.g.,
bin/console
) - Set up the Application instance
- Configure command discovery
- Make the file executable
With sanmai/console
, you get a ready-made vendor/bin/console
executable installed via Composer. No files to create, no permissions to set - just install the package and vendor/bin/console
is ready to use.
Bootstrap Configuration
Configure a custom bootstrap script in your composer.json
:
{ "extra": { "console": { "bootstrap": "app/bootstrap.php" } } }
The bootstrap script runs after Composer's autoloader is initialized. Including vendor/autoload.php
again is safe - the library handles this gracefully.
Example bootstrap script:
<?php // bootstrap.php // Set up error handlers, load environment variables, configure services define('APP_ENV', $_ENV['APP_ENV'] ?? 'production'); // Composer autoloader is already loaded // Safe to include vendor/autoload.php if needed
Troubleshooting
Commands not showing up?
- Run
composer dump-autoload --optimize
(add--dev
if your commands are in autoload-dev) - Verify your command files end with
Command.php
- Check that commands extend
Symfony\Component\Console\Command\Command
- Commands in
vendor/
are ignored by default - Commands with required constructor arguments are filtered out
Need commands with constructor dependencies? Make constructor parameters optional with default values.
Testing
make -j -k