bapao/symfony-console-provider

Bapao service provider for console commands


Keywords
console, command, service provider, symfony console, bapao
License
MIT

Documentation

(Symfony) Console Provider

Build Status Scrutinizer Code Quality Code Coverage

Bapao service provider for console commands.

Installation

Install Console Provider via Composer:

$ composer require bapao/symfony-console-provider

About

This service provider makes use of the following packages:

Simple example

Create a php file for cli usage (for example console or console.php). Add the service provider directly into the The PHP League Container, before or after Bapao application construction.

#!/usr/bin/env php
<?php
require_once __DIR__.'/../vendor/autoload.php';

set_time_limit(0);

use Bapao\Bapao\Application;
use Bapao\LeagueContainerProvider\LeagueContainerProvider;
use Bapao\SymfonyConsoleProvider\SymfonyConsoleProvider;
use League\Container\Container;

$container = new Container();
$container->addServiceProvider(new LeagueContainerProvider());

// add console provider
$container->addServiceProvider(new SymfonyConsoleProvider());

// ...

// init Bapao application
$app = new Application($container);

// ...

// init console
$console = $app->getContainer()->get('console');

// add commands
$console->add(new ExampleCommand());

// run console application
$console->run();

Run console commands via cli:

$ php console.php example:command

A command must extend Bapao\SymfonyConsoleProvider\Command\Command. You can access the container via $this->getContainer().

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

/**
 * Class ExampleCommand.
 */
class ExampleCommand extends Command
{
    /**
     * @return void
     */
    protected function configure()
    {
        $this
            ->setName('example:command')
            ->setDescription('An example command');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // get application container: $container = $this->getContainer();
        $output->writeln('This is an example command');
    }
}

Test

Run tests via phpunit:

$ php vendor/bin/phpunit

License

Console Provider is licensed under the MIT license.