small/clean-application
Composer 安装命令:
composer require small/clean-application
包简介
This project is a set of class to manage dependency injection of application's part of a clean architecture app, independently of the framework used.
README 文档
README

Small Clean Application

This project is a set of class to manage dependency injection of application's part of a clean architecture app, independently of the framework used.
Install
composer require small/clean-application
Parameters
Parameters are managed to automatically inject them in UseCase constructor.
You can set parameters through the facade static object :
\Small\CleanApplication\Facade::setParameter('test', [
'host' => 'http://clean.com',
'port' => 80
]);
You can also get them through the facade :
echo \Small\CleanApplication\Facade::getParameter('test.host');
Output :
http://clean.com
UseCase class
Simple case
A use case is a class materialization of use case that implement Small\CleanApplication\Contract\UseCaseInterface.
For example, here is simply use case that return a string :
<?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');
}
}
You can use it using 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());
Output :
a
Injecting another use case in your use case
You can inject another use case in the use case constructor :
<?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()
);
}
}
The property testUseCase will automatically be created as TestUseCase object.
Injecting parameters in your use case
You can inject parameters in your use case by typing and naming property in your use case constructor :
<?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()
);
}
}
The underscrore ('_') separate array keys of parameters structure. Here is an example matching with the $testUseCase_param :
\Small\CleanApplication\Facade::setParameter('testUseCase', ['param' => 'p']);
Interfaces
Three interface structure your code :
- Small\CleanApplication\Contract\UseCaseInterface : All your use cases must implement this interface
- Small\CleanApplication\Contract\Request : All your use case requests must implement this interface
- Small\CleanApplication\Contract\Response : All your use case response must implement this interface
Here's our TestDependency example request class :
<?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;
}
}
And his interface :
<?php
namespace Small\CleanApplication\Test\Feature\Fixture\Interface;
use Small\CleanApplication\Contract\RequestInterface;
interface TestDependencyRequestInterface extends RequestInterface
{
public function getBefore(): string;
}
And here is the response implementation :
<?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;
}
}
And his interface :
<?php
namespace Small\CleanApplication\Test\Feature\Fixture\Interface;
use Small\CleanApplication\Contract\ResponseInterface;
interface TestDependencyResponseInterface extends ResponseInterface
{
public function getStatus(): string;
}
Copyright
This project is under MIT license
small/clean-application 适用场景与选型建议
small/clean-application 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 110 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「php」 「clean architecture」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 small/clean-application 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 small/clean-application 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 small/clean-application 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Dependency injection container for the Monolith framework.
Http Microframework for Hack
Alfabank REST API integration
PHP Dependency Injection Container
统计信息
- 总下载量: 110
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-26