定制 amonger/command 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amonger/command

最新稳定版本:2.1.2

Composer 安装命令:

composer require amonger/command

包简介

The command pattern for PHP 5.3

README 文档

README

Build Status #Command#

This is a simple command bus for use with PHP 5.3

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.

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.

<?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

<?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));

统计信息

  • 总下载量: 397
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-29