PHP code example of thesis / symfony-console-module

1. Go to this page and download the library: Download thesis/symfony-console-module library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thesis / symfony-console-module example snippets




declare(strict_types=1);

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Thesis\Dic;
use Thesis\Dic\Module;
use Thesis\Dic\Ref;
use Thesis\SymfonyConsoleModule;
use Thesis\SymfonyConsoleModule\AutoconfigureCommands;

#[AsCommand('greet', description: 'Greets someone', aliases: ['g'])]
final class GreetCommand
{
    public function __invoke(#[Argument] string $name, SymfonyStyle $io): int
    {
        $io->title("Hello, {$name}!");
    
        return Command::SUCCESS;
    }
}

/**
 * @implements Module<Ref<Application>>
 */
final readonly class App implements Module
{
    public function configure(Dic $dic): mixed
    {
        $dic->apply(new AutoconfigureCommands());

        $dic->object(GreetCommand::class);

        return $dic->import(new SymfonyConsoleModule(
            name: 'MyApp',
            version: '1.0.0',
        ));
    }
}

$status = Dic::run(
    module: new App(),
    main: static fn (Application $app) => $app->run(),
);

exit($status);

use Thesis\SymfonyConsoleModule\CommandTag;

$dic->object(GreetCommand::class)
    ->tag(new CommandTag(
        name: 'greet',
        description: 'Greets someone',
        aliases: ['g'],
    ));

$dic->function(static fn(): int => Command::SUCCESS)
    ->tag(new CommandTag('util:noop'));

use Thesis\SymfonyConsoleModule\LegacyCommandTag;

// Setting the name makes the command lazy
$dic->object(LegacyGreetCommand::class)
    ->tag(new LegacyCommandTag(
        name: 'greet:legacy',
        description: 'Legacy greet',
    ));

// Omitting the name 

$dic->apply(new AutoconfigureCommands());

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;

#[AsCommand('greet', 'Greets someone')]
final class GreetCommand
{
    public function __invoke(): int
    {
        return Command::SUCCESS;
    }
}

$dic->object(GreetCommand::class);

final class GreetCommands
{
    #[AsCommand('greet:alice', 'Greet Alice')]
    public function greetAlice(): int { /* ... */ }

    #[AsCommand('greet:bob', 'Greet Bob')]
    public function greetBob(): int { /* ... */ }
}

$dic->object(GreetCommands::class);

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class LegacyGreetCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        return self::SUCCESS;
    }
}

$dic->object(LegacyGreetCommand::class);