andrewdyer/slim3-console

Command line interface for your Slim Framework application.

1.0.1 2024-12-22 17:48 UTC

This package is auto-updated.

Last update: 2024-12-22 17:49:17 UTC


README

Codacy Badge Latest Stable Version Total Downloads Daily Downloads Monthly Downloads Latest Unstable Version License

Command line interface for your Slim Framework application.

License

Licensed under MIT. Totally free for private or commercial projects.

Installation

composer require andrewdyer/slim3-console

Usage

$app = new \Slim\App();

$kernel = new \Anddye\Console\Kernel\Kernel();
$kernel->addCommand(\Anddye\Console\Commands\SayHelloCommand::class);

$console = new \Anddye\Console\Console($app);
$console->boot($kernel);
$console->run();

Creating a console instance and adding commands without the kernel.

$app = new \Slim\App();

$container = $app->getContainer();

$console = new \Anddye\Console\Console($app);
$console->add(new \Anddye\Console\Commands\SayHelloCommand($container));
$console->run();

Adding commands from app container.

$app = new \Slim\App([
    'settings' => [
        'commands' => [
            \Anddye\Console\Commands\SayGoodbyeCommand::class,
            \Anddye\Console\Commands\SayHelloCommand::class,
        ]
    ]
]);

$container = $app->getContainer();
$commands = $container->get('settings')->get('commands');

$kernel = new \Anddye\Console\Kernel\Kernel();
$kernel->addCommands($commands);

$console = new \Anddye\Console\Console($app);
$console->boot($kernel);
$console->run();

Commands

Commands should be defined in classes extending the Anddye\Console\Commands\AbstractCommand class. Here is a basic usage example of a command:

<?php

namespace App\Commands;

use Anddye\Console\Commands\AbstractCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class SayHelloCommand extends AbstractCommand
{
    /**
     * Sets an array of argument to add to the command.
     *
     * @return array
     */
    public function arguments(): array
    {
        return [
            ['name', InputArgument::REQUIRED, 'Your name.'],
        ];
    }

    /**
     * Sets the description for the command.
     *
     * @return string
     */
    public function description(): string
    {
        return 'Prints "Hello" and your name for a specific number of times.';
    }

    /**
     * The body of the command.
     *
     * @param InputInterface  $input
     * @param OutputInterface $output
     *
     * @return mixed
     */
    public function handle(InputInterface $input, OutputInterface $output)
    {
        for ($i = 0; $i < $input->getOption('repeat'); ++$i) {
            $this->info('Hello '.$input->getArgument('name'));
        }
    }

    /**
     * Sets the help for the command.
     *
     * @return string
     */
    public function help(): string
    {
        return '';
    }

    /**
     * Sets the name of the command.
     *
     * @return string
     */
    public function name(): string
    {
        return 'say:hello';
    }

    /**
     * Sets an array of options to add to the command.
     *
     * @return array
     */
    public function options(): array
    {
        return [
            ['repeat', 'r', InputOption::VALUE_OPTIONAL, 'Times to repeat the output.', 1],
        ];
    }
}

Support

If you are having any issues with this library, then please feel free to contact me on Twitter.

If you think you've found an bug, please report it using the issue tracker, or better yet, fork the repository and submit a pull request.

If you're using this package, I'd love to hear your thoughts!

Useful Links