2017 © Pedro Peláez
 

library console

Creating Console Application library on top of Symfony Console

image

selami/console

Creating Console Application library on top of Symfony Console

  • Wednesday, July 11, 2018
  • by mkorkmaz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 141 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 26 % Grown

The README.md

Selami Console

Symfony Console application factory that autowires dependecies. Use any PSR-11 compatible DIC library., (*1)

Build Status Coverage Status Scrutinizer Code Quality Codacy Badge Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

Installation

composer require selami/console

Usage

Assume we use Zend ServiceManager as our PSR-11 compatible DIC., (*3)

1. Create your service

<?php
declare(strict_types=1);

namespace MyConsoleApplication\Service;

class PrintService
{
    public function formatMessage(string $greeting, string $message) : string
    {
        return 'Hello ' . $greeting . ' ' . $message;
    }
}

2. Create a factory for your service

<?php
declare(strict_types=1);

namespace MyConsoleApplication\Factory;

use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
use MyConsoleApplication\Service\PrintService;

class PrintServiceFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) : PrintService
    {
        return new PrintService();
    }
}

3. Create your Symfony Command

<?php
declare(strict_types=1);

namespace MyConsoleApplication\Command;

use MyConsoleApplication\Service\PrintService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

class GreetingCommand extends Command
{
    /**
     * @var PrintService
     */
    private $printService;
    private $config;

    public function __construct(PrintService $printService, array $config, string $name = null)
    {
        $this->printService = $printService;
        $this->config = $config;
        parent::__construct($name);
    }

    protected function configure() : void
    {
        $this
            ->setName('command:greeting')
            ->setDescription('Prints "Hello {config.greeting} {name}')
            ->setDefinition([
                new InputArgument('name', InputArgument::REQUIRED),
            ]);
    }

    protected function execute(InputInterface $input, OutputInterface $output) : void
    {
        $name = $input->getArgument('name');
        $output->writeln($this->printService->formatMessage($this->config['greeting'], $name));
    }
}

5.Create your executable console application (i.e. ./bin/console)

#!/usr/bin/env php

<?php
declare(strict_types=1);

require_once ('path/to/vendor/autoload.php');

use Selami\Console\ApplicationFactory;
use Laminas\ServiceManager\ServiceManager;

$container = new ServiceManager(
    [
        'factories' => [
            MyConsoleApplication\Service\PrintService::class => MyConsoleApplication\Factory\PrintServiceFactory::class
        ]
    ]
);
$container->setService(
    'commands', [
        MyConsoleApplication\Command\GreetingCommand::class
    ]
);
$container->setService('config', ['greeting' => 'Dear']);

$cli = ApplicationFactory::makeApplication($container);
$cli->run();

6. Run your command on terminal

./bin/console command:greeting Kedibey

Prints: Hello Dear Kedibey, (*4)

The Versions

11/07 2018

dev-master

9999999-dev

Creating Console Application library on top of Symfony Console

  Sources   Download

MIT

The Requires

 

The Development Requires

11/07 2018
11/07 2018
16/07 2017
30/05 2017

0.2.1

0.2.1.0

Creating Console Application library on top of Symfony Console

  Sources   Download

MIT

The Requires

 

The Development Requires

30/05 2017

0.2

0.2.0.0

Creating Console Application library on top of Symfony Console

  Sources   Download

MIT

The Requires

 

The Development Requires

29/05 2017

0.1

0.1.0.0

Creating Console Application library on top of Symfony Console

  Sources   Download

MIT

The Requires

 

The Development Requires