Code Monkey home page Code Monkey logo

commander's Introduction

Domain commander

=========

This package is able run domain commands easily. Commands are used to separate domain logic from your php framework.

This package is based on Laravel Commander build by JeffreyWay.

Installation

Install through Composer.

"require": {
    "jildertmiedema/commander": "~0.1"
}

What does it do

  • It creates a command object. The command object is data transfer object between the framework/controller and the domain logic.
  • It will fill the command object with the request data (For example $_GET, $_POST, $app['request']->query->all()).
  • It will try to find a validator class, when it's found it will validate the input.
  • If decorators have been set it will execute the decorators. (For example a sanitizer).
  • It will find and execute the handler for the command.

Integrate to the framework

Silex

This package can easily be used in Silex.

It will try to find a handler(required) and a validator (not required) from application container using the following convention.

//without namespace "ExampleCommand"
$app[$className . '.handler']; // exampleCommand.handler
$app[$className . '.validator']; // exampleCommand.validator

//with namespace "Acme\Domain\ExampleCommand"
$app[$className . '.handler']; // acme.domain.exampleCommand.handler
$app[$className . '.validator']; // acme.domain.exampleCommand.validator

Usage:

$app->register(new JildertMiedema\Commander\Silex\CommanderServiceProvider());

Example:

class TestCommand {
    public $test;
    public $extra;

    public function __construct($test, $extra)
    {
        $this->test = $test;
        $this->extra = $extra;
    }
}

class TestCommandHandler implements CommandHandler
{

    public function handle($command)
    {
        //handle the command
    }
}

$app['testCommand.handler'] = $app->share(function() use ($app) {
    return new TestCommandHandler();
});

$app->get('/', function() use ($app) {
    return $app['commander.executor']->execute('TestCommand');
});

For a full example see silex.php

For usage in controllers see CommanderController.php

Vanilla php

To use commander in another framework you can use this code:

use JildertMiedema\Commander\Manager;
use JildertMiedema\Commander\Vanilla\Executor;

$manager = new Manager();
$executor = new Executor($manager);

echo $executor->execute('TestCommand', null, ['TestSanitizer']);

The vanilla php solution does not support out-of-the-box dependency injection, therefore you need to implement a translator and a resolver.

An example vanilla.php

Creating your own translator and resolver

For integration with a another framework you can implement your own translator and resolver.

$translator = new YourOwnCommandTranslator; // Must implement `JildertMiedema\Commander\CommandTranslatorInterface`
$resolver =  new YourOwnResolver; // Must implement `JildertMiedema\Commander\ResolverInterface`
$defaultCommandBus = new DefaultCommandBus($translator, $resolver);
$commandBus = new ValidationCommandBus($defaultCommandBus, $translator, $resolver);
$manager = new Manager($commandBus);

Examples

Run the examples

cd YOUR_PACKAGE_DIR
cd examples
php -S localhost:8000

Visit:

http://localhost:8000/silex.php?test=test%20%20%20&extra=123
http://localhost:8000/silex.php/sanitizer?test=test%20%20%20&extra=123
http://localhost:8000/silex.php/controller?test=test%20%20%20&extra=123
http://localhost:8000/vanilla.php?test=test%20%20%20&extra=123

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.