2017 © Pedro Peláez
 

library command

The command pattern for PHP 5.3

image

amonger/command

The command pattern for PHP 5.3

  • Wednesday, July 15, 2015
  • by haveacigaro
  • Repository
  • 1 Watchers
  • 0 Stars
  • 381 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

Command

This is a simple command bus for use with PHP 5.3, (*2)

Use

<?php
    namespace Command\Command;

    class MyHandlerCommand
    {
        public $someParam;

        public function __construct($someParam)
        {
            $this->someParam = $someParam;
        }
    }
<?php
    namespace Command\Handler;

    class MyHandlerHandler implements HandlerInterface, ApplicationInterface
    {
        protected $app;

        public function handle($object)
        {
            echo $app['db']->persist($object->someParam);
        }

        public function setApplication($app)
        {
            $this->app = $app;
        }
    }
    $command = new Command\Command(new Command\Resolver);
    $command->setApplication($app);
    $command->dispatch(new MyHandlerCommand($someParam));

This will resolve MyHandlerHandler which implements HandlerInterface, and provides access to your application container., (*3)

Self handling

Alternatively you might not want to separate your DTOs from your commands. In this case, you can just implement the self handling interface which will call the handle method., (*4)

<?php
namespace Command\Command;

class MyHandlerCommand implements SelfHandling
{
    protected $someParam;

    public function __construct($someParam)
    {
        $this->someParam = $someParam;
    }

    public function handle()
    {
        return $this->someParam * 2;
    }
}

Finally, you can get access to the application by implementing ApplicationInterface, (*5)

<?php
class TestCommand implements SelfHandling, ApplicationInterface
{
    private $name;
    private $application;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function handle()
    {
        $app = $this->application;
        var_dump($app($this->name));
    }

    /**
     * @param $application
     * @return void
     */
    public function setApplication($application)
    {
        $this->application = $application;
    }

}
    $resolver = new \Command\Resolver\SelfHandlingResolver();
    $resolver->setApplication(function($a){ return $a*2;});

    $command = new Command\Command($resolver);
    $command->dispatch(new TestCommand(2));

The Versions

15/07 2015

dev-master

9999999-dev

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger

15/07 2015

2.1.2

2.1.2.0

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger

15/07 2015

2.1.1

2.1.1.0

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger

15/07 2015

2.1.0

2.1.0.0

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger

11/07 2015

dev-cleanup

dev-cleanup

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger

11/07 2015

2.0.1

2.0.1.0

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger

10/07 2015

dev-self-handling-fixes

dev-self-handling-fixes

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger

10/07 2015

2.0.0

2.0.0.0

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger

30/06 2015

1.0.1

1.0.1.0

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger

24/06 2015

1.0.0

1.0.0.0

The command pattern for PHP 5.3

  Sources   Download

MIT

The Development Requires

by Alan Monger