PHP code example of selami / console

1. Go to this page and download the library: Download selami/console 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/ */

    

selami / console example snippets



declare(strict_types=1);

namespace MyConsoleApplication\Service;

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


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();
    }
}



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));
    }
}

#!/usr/bin/env php


declare(strict_types=1);

\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();