Giter Club home page Giter Club logo

tactician-container-selfhandling's Introduction

Tactician Container-Aware Self-Handling Commands

Latest Stable Version Build Status License Code Coverage Scrutinizer Code Quality SensioLabsInsight

Install

Via Composer

$ composer require spekkionu/tactician-container-selfhandling

If you want to use the container aware commands you will also need to install league/container

Usage

Add MiddleWare

use League\Tactician\CommandBus;
use Spekkionu\Tactician\SelfExecuting\SelfExecutionMiddleware;

$commandBus = new CommandBus([
    // any other pre-execution middleware
    new SelfExecutionMiddleware(),
    // other middleware to handle non-self-executing commands
    // any other post-execution middleware
]);

Create a Command

Your commands must implement Spekkionu\Tactician\SelfExecuting\SelfExecutingCommand and have a handle() method.

The handle method must have no parameters.

use Spekkionu\Tactician\SelfExecuting\SelfExecutingCommand;

/**
 * Class ExampleSelfExecutingCommand
 */
class ExampleSelfExecutingCommand implements SelfExecutingCommand
{
    /**
     * @return string
     */
    public function handle()
    {
        // do work here
    }
}

Run Command

$commandBus->handle(new ExampleSelfExecutingCommand());

Container-Aware Self-Executing Commands

Container aware commands will have dependencies injected into the handle() method from league/container.

use League\Container\Container;
use League\Tactician\CommandBus;
use Spekkionu\Tactician\SelfExecuting\SelfExecutionMiddleware;

// Setup the Container
$container = new Container();
$container->delegate(
    new \League\Container\ReflectionContainer
);
$container->add('Logger', function() {
    return new Logger();
});

// Setup the command bus
$commandBus = new CommandBus([
    // any other pre-execution middleware
    new ContainerAwareSelfExecutionMiddleware($container),
    // other middleware to handle non-self-executing commands
    // any other post-execution middleware
]);
use Spekkionu\Tactician\SelfExecuting\SelfExecutingCommand;

/**
 * Class ExampleSelfExecutingCommand
 */
class ExampleSelfExecutingCommand implements SelfExecutingCommand
{
    /**
     * The logger will be injected automatically
     * @return string
     */
    public function handle(Logger $logger)
    {
        $logger->log('log message');
    }
}
$commandBus->handle(new ExampleSelfExecutingCommand());

Testing

$ composer test

License

The MIT License (MIT). Please see License File for more information.

tactician-container-selfhandling's People

Contributors

spekkionu avatar

Stargazers

 avatar

Watchers

 avatar  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.