Projek ini ialah satu set kelas untuk mengurus suntikan kebergantungan bahagian aplikasi dalam apl seni bina bersih,
bebas daripada rangka kerja yang digunakan.
Git : https://git.small-project.dev/lib/small-clean-application
Pembungkus : https://packagist.org/packages/small/clean-application
composer require small/clean-application
Parameter diuruskan untuk menyuntiknya secara automatik dalam pembina UseCase.
Anda boleh menetapkan parameter melalui objek statik fasad :
\Small\CleanApplication\Facade::setParameter('test', [ 'host' => 'http://clean.com', 'port' => 80 ]);
Anda juga boleh mendapatkannya melalui fasad :
echo \Small\CleanApplication\Facade::getParameter('test.host');
Output :
http://clean.com
Kes penggunaan ialah perwujudan kelas kes penggunaan yang melaksanakan SmallCleanApplicationContractUseCaseInterface.
Contohnya, ini hanyalah use case yang mengembalikan rentetan :
<?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'); } }
Anda boleh menggunakannya menggunakan fasad :
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());
Output :
a
Anda boleh menyuntik kes guna lain dalam pembina kes guna :
<?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() ); } }
Properti testUseCase akan dibuat secara automatik sebagai objek TestUseCase.
Anda boleh menyuntik parameter dalam use case anda dengan menaip dan menamakan harta dalam use case constructor anda :
<?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() ); } }
Tarik bawah ('_') kekunci tatasusunan berasingan bagi struktur parameter. Berikut ialah contoh yang sepadan dengan
$testUseCase_param :
\Small\CleanApplication\Facade::setParameter('testUseCase', ['param' => 'p']);
Tiga struktur antara muka kod anda :
Inilah kelas permintaan contoh TestDependency kami :
<?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; } }
Dan antara mukanya :
<?php namespace Small\CleanApplication\Test\Feature\Fixture\Interface; use Small\CleanApplication\Contract\RequestInterface; interface TestDependencyRequestInterface extends RequestInterface { public function getBefore(): string; }
Dan berikut ialah pelaksanaan tindak balas :
<?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; } }
Dan antara mukanya :
<?php namespace Small\CleanApplication\Test\Feature\Fixture\Interface; use Small\CleanApplication\Contract\ResponseInterface; interface TestDependencyResponseInterface extends ResponseInterface { public function getStatus(): string; }
Atas ialah kandungan terperinci Aplikasi Bersih Kecil. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!