這個專案是一組用於管理乾淨架構應用程式的應用程式部分的依賴注入的類,
獨立於所使用的框架。
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中文網其他相關文章!