这个项目是一组用于管理干净架构应用程序的应用程序部分的依赖注入的类,
独立于所使用的框架。
Git:https://git.small-project.dev/lib/small-clean-application
Packagist :https://packagist.org/packages/small/clean-application
composer require small/clean-application
参数被管理以自动将它们注入到 UseCase 构造函数中。
可以通过Facade静态对象设置参数:
\Small\CleanApplication\Facade::setParameter('test', [ 'host' => 'http://clean.com', 'port' => 80 ]);
您也可以通过门面获取它们:
echo \Small\CleanApplication\Facade::getParameter('test.host');
输出:
http://clean.com
用例是实现 SmallCleanApplicationContractUseCaseInterface 的用例的类具体化。
例如,这里是返回字符串的简单用例:
<?php namespace Small\CleanApplication\Test\Feature\Fixture\UseCase; use Small\CleanApplication\Test\Feature\Fixture\Interface\TestResponseInterface; class TestUseCase implements \Small\CleanApplication\Contract\UseCaseInterface { public function execute($request): TestResponseInterface { return new TestResponse('a'); } }
您可以使用facade来使用它:
use Small\CleanApplication\Test\Feature\Fixture\UseCase\TestUseCase; use \Small\CleanApplication\Test\Feature\Fixture\UseCase\TestRequest; echo \Small\CleanApplication\Facade::execute(TestUseCase::class, new TestRequest());
输出:
a
您可以在用例构造函数中注入另一个用例:
<?php namespace Small\CleanApplication\Test\Feature\Fixture\UseCase; use Small\CleanApplication\Contract\UseCaseInterface; use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyRequestInterface; use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyResponseInterface; class TestDependencyUseCase implements UseCaseInterface { public function __construct( protected TestUseCase $testUseCase, ) {} public function execute($request): TestDependencyResponseInterface { return new TestDependencyResponse( $request->getBefore() . $this->testUseCase->execute($request)->getStatus() ); } }
属性 testUseCase 将自动创建为 TestUseCase 对象。
您可以通过在用例构造函数中键入和命名属性来在用例中注入参数:
<?php namespace Small\CleanApplication\Test\Feature\Fixture\UseCase; use Small\CleanApplication\Contract\UseCaseInterface; use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyRequestInterface; use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyResponseInterface; class TestDependencyParamUseCase implements UseCaseInterface { public function __construct( protected string $testUseCase_param, protected TestUseCase $testUseCase, ) {} public function execute($request): TestDependencyResponseInterface { if (!$request instanceof TestDependencyRequestInterface) { throw new \Exception('Bad request'); } return new TestDependencyResponse( $this->testUseCase_param . $request->getBefore() . $this->testUseCase->execute($request)->getStatus() ); } }
下划线('_')分隔参数结构的数组键。这是与
匹配的示例
$testUseCase_param :
\Small\CleanApplication\Facade::setParameter('testUseCase', ['param' => 'p']);
三个接口构建您的代码:
这是我们的TestDependency示例请求类:
<?php namespace Small\CleanApplication\Test\Feature\Fixture\UseCase; use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyRequestInterface; readonly class TestDependencyRequest implements TestDependencyRequestInterface { public function __construct( protected string $before, ) {} public function getBefore(): string { return $this->before; } }
和他的界面:
<?php namespace Small\CleanApplication\Test\Feature\Fixture\Interface; use Small\CleanApplication\Contract\RequestInterface; interface TestDependencyRequestInterface extends RequestInterface { public function getBefore(): string; }
这是响应实现:
<?php namespace Small\CleanApplication\Test\Feature\Fixture\UseCase; use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyResponseInterface; readonly class TestDependencyResponse implements TestDependencyResponseInterface { public function __construct( protected string $status, ) {} public function getStatus(): string { return $this->status; } }
和他的界面:
<?php namespace Small\CleanApplication\Test\Feature\Fixture\Interface; use Small\CleanApplication\Contract\ResponseInterface; interface TestDependencyResponseInterface extends ResponseInterface { public function getStatus(): string; }
以上是小型清洁应用的详细内容。更多信息请关注PHP中文网其他相关文章!