divsmith/illuminate-airlock

此包最新版本(dev-master)没有可用的许可证信息。

常见 Laravel Illuminate 类的 Airlock 适配器。

dev-master 2014-09-02 05:02 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:03:37 UTC


README

Laravel 通过各种外观和驱动的 Illuminate 类提供大量功能,界面美观易用。虽然这对于应用层面的类和服务来说很棒,但我们不希望在领域逻辑中这样做。

// Acme\Handlers\UserSubscribedCommandHandler.php

class UserSubscribedCommandHandler {

    public function handle($command)
    {
        // Do everything involved with a new user subscription
        
        Event::fire('user.subscribed', $command); // Very tightly coupled!
    }
}

一种常见的替代方案是通过构造函数注入 Illuminate 类

// Acme\Handlers\UserSubscribedCommandHandler.php

class UserSubscribedCommandHandler {

    protected $dispatcher;

    public function __construct(\Illuminate\Events\Dispatcher $dispatcher) // Less tightly coupled
    {
        $this->dispatcher = $dispatcher;
    }

    public function handle($command)
    {
        $this->dispatcher->fire('user.subscribed', $command);
    }
}

但这种方法仍然让我们直接依赖于 Illuminate\Events\Dispatcher 类。Illuminate-Airlock 允许我们打破这种耦合,并使用我们指定和控制的外接接口。

// Acme\Handlers\UserSubscribedCommandHandler.php

class UserSubscribedCommandHandler {
    
    protected $dispatcher;
    
    public function __construct(\Acme\Events\EventInterface $dispatcher) // Completely decoupled
    {
        $this->dispatcher = $dispatcher;
    }
    
    public function handle($command)
    {
        $this->dispatcher->fire('user.subscribed', $command);
    }
}

安装

  1. 将以下内容添加到您的 composer.json 文件中

    {
        "require": {
            "divsmith/airlock": "dev-master"
        }
    }
  2. 运行 composer install

使用

  1. 创建一个包含您的领域逻辑将使用的 Illuminate 类上的方法的接口。

    // Acme\Events\EventInterface.php
    
    interface EventInterface {
        public function fire();
        public function listen();
    }
  2. 然后,创建一个实现该接口并扩展相应的 IlluminateAirlock 类的类。

    // Acme\Events\IlluminateEvents.php
    
    class IlluminateEvents implements EventInterface extends \IlluminateAirlock\Events\Dispatcher {}

    IlluminateAirlock 的命名约定遵循 Laravel 的,因此 Illuminate\Events\Dispatcher 的 IlluminateAirlock 适配器为 Divsmith\IlluminateAirlock\Events\DispatcherIlluminate\Log\WriterDivsmith\IlluminateAirlock\Log\Writer 等。每个 IlluminateAirlock 类都有与原始 Illuminate 类相同的公共方法,所以只要您的接口坚持这些,通常不需要在您的类中实现它们。

  3. 通过服务提供者将实现绑定到接口的 IoC 容器中

    // vendor\divsmith\illuminate-Airlock\src\IlluminateAirlockServiceProvider.php
    
    class IlluminateAirlockServiceProvider extends ServiceProvider {
        
        public function register()
        {
            $this->app->bind('\Acme\Events\EventInterface', function() 
            {
                return new \Acme\Events\IlluminateEvents;
            }
        }
    }

    并将其添加到您的 providers 数组中

    // app\config\app.php
    
        ....
        'Illuminate\Session\SessionServiceProvider',
        'Illuminate\Translation\TranslationServiceProvider',
        'Illuminate\Validation\ValidationServiceProvider',
        'Illuminate\View\ViewServiceProvider',
        'Divsmith\IlluminateAirlock\IlluminateAirlockServiceProvider'
    ),
    ....
  4. 享受吧!

注意

  1. 您可能已经注意到没有 tests 目录。我故意省略了单元测试,因为此包不包含可测试的逻辑。它只是传递方法调用;因此,任何编写的单元测试都只能验证特定函数是否被调用,而这并不是单元测试的目的。

  2. 您可能会偶尔发现关联的 IlluminateAirlock 适配器中没有实现 Illuminate 的某个方法。这是因为它没有在 Illuminate 类的公共 API 中明确定义,而是通过 __call() 魔法方法调用。您可以自由地向 IlluminateAirlock 适配器添加具体方法,并提交一个 pull request。

  3. 您可能会注意到一些 Laravel 的外观没有提供 IlluminateAirlock 适配器。我做出了有意识地省略那些涉及应用逻辑的决策,以帮助保持领域逻辑的整洁。如果您觉得遗漏了一个有效的适配器,请发给我消息或创建相关的适配器并提交一个 pull request。

贡献

我对任何和所有的 pull request 都持开放态度,从错别字到缺少方法添加到新的适配器。请将所有 pull request 提交到 dev 分支。