sanmai/console

Ready-made extensible console application using Symfony Console component

Fund package maintenance!
sanmai

0.1 2025-06-27 17:40 UTC

README

License PHP Version

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

  1. 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;
    }
}
  1. Update the autoloader:
composer dump-autoload --optimize
  1. 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:

  1. Scanning Composer's optimized classmap
  2. Finding classes that end with Command.php
  3. Loading those that extend Symfony\Component\Console\Command\Command
  4. Filtering out vendored files
  5. 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:

  1. Create a console executable file (e.g., bin/console)
  2. Set up the Application instance
  3. Configure command discovery
  4. 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