Git Product home page Git Product logo

slim-cli-runner's Introduction

Slim CLI Runner

Create and run command line tasks for the Slim PHP micro-framework

Installation

Installation

composer require adrianfalleiro/slim-cli-runner ^2.6

Register Middleware

Register the middleware in middleware.php

$app->add(\adrianfalleiro\SlimCLIRunner::class);

Define, Register and run your tasks

Task definition

Tasks are simply classes which have a public command() method.

The dependency container is passed to the constructor, and console arguments are passed to the command() method.

use \Psr\Container\ContainerInterface;
use \RuntimeException;

class SampleTask {

    /** @var ContainerInterface */
    protected $container;

    /**
     * Constructor
     *
     * @param ContainerInterface $container
     * @return void
     */
    public function __construct($container)
    {
        // access container classes
        // eg $container->get('redis');
        $this->container = $container;
    }

    /**
     * SampleTask command
     *
     * @param array $args
     * @return void
     */
    public function command($args)
    {
        // Access items in container
        $settings = $this->container->get('settings');

        // Throw if no arguments provided
        if (empty($args)) {
            throw new RuntimeException("No arguments passed to command");
        }

        $firstArg = $args[0];

        // Output the first argument
        return $firstArg;
    }
}

Tasks registration

Add a new key in your settings.php file called commands and list your tasks.
Keep in mind that you should NOT add this within the 'settings' values
To define a default task (For use when no command name is provided) add a new task with __default as key

'settings' => [
    // ...
],
'commands' => [
    'SampleTask' => \Namespace\To\Task::class
],

Run Tasks

There are multiple ways of doing this:
Directly via commandline:

php /path/to/slim/public/index.php SampleTask argument1 argument2 argument3

Via composer:
composer.json

{
    /*...*/
    "config": {
        "process-timeout" : 0
    },
    "scripts": {
        /*...*/
        "cli": "php public/index.php"
    }
}

The command

composer cli SampleTask argument1 argument2 argument3

Examples

Examples can be found in the examples/ folder

slim-cli-runner's People

Contributors

laurarozier avatar adrianfalleiro avatar benrogmans avatar jeroenvreuls avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.