divsmith / illuminate-airlock
常见 Laravel Illuminate 类的 Airlock 适配器。
Requires
- php: >=5.4.0
- divsmith/airlock: ~1.0.1
- laravel/framework: ~4.2.0
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); } }
安装
-
将以下内容添加到您的 composer.json 文件中
{ "require": { "divsmith/airlock": "dev-master" } }
-
运行
composer install
使用
-
创建一个包含您的领域逻辑将使用的 Illuminate 类上的方法的接口。
// Acme\Events\EventInterface.php interface EventInterface { public function fire(); public function listen(); }
-
然后,创建一个实现该接口并扩展相应的 IlluminateAirlock 类的类。
// Acme\Events\IlluminateEvents.php class IlluminateEvents implements EventInterface extends \IlluminateAirlock\Events\Dispatcher {}
IlluminateAirlock 的命名约定遵循 Laravel 的,因此
Illuminate\Events\Dispatcher的 IlluminateAirlock 适配器为Divsmith\IlluminateAirlock\Events\Dispatcher,Illuminate\Log\Writer为Divsmith\IlluminateAirlock\Log\Writer等。每个IlluminateAirlock类都有与原始Illuminate类相同的公共方法,所以只要您的接口坚持这些,通常不需要在您的类中实现它们。 -
通过服务提供者将实现绑定到接口的 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' ), ....
-
享受吧!
注意
-
您可能已经注意到没有
tests目录。我故意省略了单元测试,因为此包不包含可测试的逻辑。它只是传递方法调用;因此,任何编写的单元测试都只能验证特定函数是否被调用,而这并不是单元测试的目的。 -
您可能会偶尔发现关联的 IlluminateAirlock 适配器中没有实现 Illuminate 的某个方法。这是因为它没有在 Illuminate 类的公共 API 中明确定义,而是通过
__call()魔法方法调用。您可以自由地向 IlluminateAirlock 适配器添加具体方法,并提交一个 pull request。 -
您可能会注意到一些 Laravel 的外观没有提供 IlluminateAirlock 适配器。我做出了有意识地省略那些涉及应用逻辑的决策,以帮助保持领域逻辑的整洁。如果您觉得遗漏了一个有效的适配器,请发给我消息或创建相关的适配器并提交一个 pull request。
贡献
我对任何和所有的 pull request 都持开放态度,从错别字到缺少方法添加到新的适配器。请将所有 pull request 提交到 dev 分支。