openclassrooms/use-case
Composer 安装命令:
composer require openclassrooms/use-case
包简介
Use case library
关键字:
README 文档
README
UseCase is a library that provides facilities to manage technical code over a Use Case in a Clean / Hexagonal / Use Case Architecture.
- Security access
- Cache management
- Transactional context
- Events
- Logs
The goal is to have only functional code on the Use Case and manage technical code in an elegant way using annotations.
More details on :
Installation
composer require openclassrooms/use-case
or by adding the package to the composer.json file directly.
{
"require": {
"openclassrooms/use-case": "*"
}
}
<?php require 'vendor/autoload.php'; use OpenClassrooms\UseCase\Application\Services\Proxy\UseCases\UseCaseProxy; //do things
Instantiation
The UseCaseProxy needs a lot of dependencies.
The Dependency Injection Pattern is clearly helpful.
For an implementation with Symfony2, the UseCaseBundle is more appropriate.
UseCaseProxy can be instantiate as following:
class app() { /** * @var OpenClassrooms\UseCase\Application\Services\Proxy\UseCases\UseCaseProxyBuilder */ private $builder; /** * @var OpenClassrooms\UseCase\Application\Services\Security\Security; */ private $security; /** * @var OpenClassrooms\Cache\Cache\Cache; */ private $cache; /** * @var OpenClassrooms\UseCase\Application\Services\Transaction\Transaction; */ private $transaction; /** * @var OpenClassrooms\UseCase\Application\Services\EventSender\EventSender; */ private $event; /** * @var OpenClassrooms\UseCase\Application\Services\EventSender\EventFactory */ private $eventFactory; /** * @var Psr\Log\LoggerInterface */ private $logger; /** * @var Doctrine\Common\Annotations\Reader */ private $reader; public function method() { $useCase = $this->builder ->create(new OriginalUseCase()) ->withReader($this->reader) ->withSecurity($this->security) ->withCache($this->cache) ->withTransaction($this->transaction) ->withEventSender($this->event) ->withEventFactory($this->eventFactory) ->withLogger($this->logger) ->build(); } }
Only UseCaseProxyBuilder::create(UseCase $useCase) and UseCaseProxyBuilder::withReader(AnnotationReader $reader) are mandatory.
Usage
A classic Use Case in Clean / Hexagonal / Use Case Architecture style looks like this:
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase; use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCaseRequest; use OpenClassrooms\UseCase\BusinessRules\Responders\UseCaseResponse; class AUseCase implements UseCase { /** * @return UseCaseResponse */ public function execute(UseCaseRequest $useCaseRequest) { // do things return $useCaseResponse; } }
The library provides a Proxy of the UseCase.
Security
@Security annotation allows to check access.
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase; use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCaseRequest; use OpenClassrooms\UseCase\BusinessRules\Responders\UseCaseResponse; use OpenClassrooms\UseCase\Application\Annotations\Security; class AUseCase implements UseCase { /** * @Security (roles = "ROLE_1") * * @return UseCaseResponse */ public function execute(UseCaseRequest $useCaseRequest) { // do things return $useCaseResponse; } }
"roles" is mandatory.
Other options :
/** * @Security (roles = "ROLE_1, ROLE_2") * Check the array of roles * * @Security (roles = "ROLE_1", checkRequest = true) * Check access for the object $useCaseRequest * * @Security (roles = "ROLE_1", checkField = "fieldName") * Check access for the field "fieldName" of the object $useCaseRequest */
Cache
@Cache annotation allows to manage cache.
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase; use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCaseRequest; use OpenClassrooms\UseCase\BusinessRules\Responders\UseCaseResponse; use OpenClassrooms\UseCase\Application\Annotations\Cache; class AUseCase implements UseCase { /** * @Cache * * @return UseCaseResponse */ public function execute(UseCaseRequest $useCaseRequest) { // do things return $useCaseResponse; } }
The key is equal to : md5(serialize($useCaseRequest)) and the TTL is the default one.
Other options:
/** * @Cache (lifetime=1000) * Add a TTL of 1000 seconds * * @Cache (namespacePrefix="namespace_prefix") * Add a namespace to the id with a namespace id equals to "namespace_prefix" * * @Cache (namespacePrefix="namespace prefix", namespaceAttribute="fieldName") * Add a namespace to the id with a namespace id equals to "namespace_prefix" . "$useCaseRequest->fieldName" */
Transaction
@Transaction annotation gives a transactional context around the Use Case.
- begin transaction
- execute()
- commit
- rollback on exception
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase; use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCaseRequest; use OpenClassrooms\UseCase\BusinessRules\Responders\UseCaseResponse; use OpenClassrooms\UseCase\Application\Annotations\Transaction; class AUseCase implements UseCase { /** * @Transaction * * @return UseCaseResponse */ public function execute(UseCaseRequest $useCaseRequest) { // do things return $useCaseResponse; } }
Event
@Event annotation allows to send events.
An implementation of OpenClassrooms\UseCase\Application\Services\EventSender\EventFactory must be written in the application context.
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase; use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCaseRequest; use OpenClassrooms\UseCase\BusinessRules\Responders\UseCaseResponse; use OpenClassrooms\UseCase\Application\Annotations\EventSender; class AUseCase implements UseCase { /** * @Event * * @return UseCaseResponse */ public function execute(UseCaseRequest $useCaseRequest) { // do things return $useCaseResponse; } }
The message can be send:
- pre execute
- post execute
- on exception or all of them.
Post is default.
The name of the event is the name of the use case with underscore, prefixed by the method. For previous example, the name would be : use_case.post.a_use_case
Prefixes can be :
- use_case.pre.
- use_case.post.
- use_case.exception.
/** * @Event(name="event_name") * Send an event with event name equals to *prefix*.event_name * (note: the name is always converted to underscore) * * @Event(methods="pre") * Send an event before the call of UseCase->execute() * * @Event(methods="pre, post, onException") * Send an event before the call of UseCase->execute(), after the call of UseCase->execute() or on exception * * @Event(name="first_event") * @Event(name="second_event") * Send two events */
Log
@Log annotation allows to add log following the PSR standard.
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase; use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCaseRequest; use OpenClassrooms\UseCase\BusinessRules\Responders\UseCaseResponse; use OpenClassrooms\UseCase\Application\Annotations\Log; class AUseCase implements UseCase { /** * @Log * * @return UseCaseResponse */ public function execute(UseCaseRequest $useCaseRequest) { // do things return $useCaseResponse; } }
The log can be:
- pre execute
- post execute
- on exception or all of them.
On exception is default.
Level can be specified following PSR's levels. Warning is default.
/** * @Log(level="error") * Log with the level 'error' * * @Log (message="message with context {foo}", context={"foo":"bar"}) * Log with standard message * * @Log(methods="pre") * Log before the call of UseCase->execute() * * @Log(methods="pre, post, onException") * Log before the call of UseCase->execute(), after the call of UseCase->execute() or on exception * * @Log(methods="pre", level="debug") * @Log(methods="onException", level="error") * Log before the call of UseCase->execute() with debug level and on exception with error level */
Workflow
The execution order is the following:
Pre Excecute:
- log (pre)
- security
- cache (fetch)
- transaction (begin transaction)
- event (pre)
Post Excecute:
- cache (save if needed)
- transaction (commit)
- event (post)
- log (post)
On Exception:
- log (on exception)
- transaction (rollback)
- event (on exception)
Utils
The library provide a generic response for paginated collection.
openclassrooms/use-case 适用场景与选型建议
openclassrooms/use-case 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 414.82k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「design」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 openclassrooms/use-case 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 openclassrooms/use-case 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 openclassrooms/use-case 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package for the Repository Design Pattern.
Symfony bundle for broadway/broadway.
A PHP console tool to generate Markdown documentation from your docblocks and types hints using templates.
A PHP library to implement the observer pattern
Build a domain-oriented application on Laravel Framework
Material Design Icons for Laravel Blade views.
统计信息
- 总下载量: 414.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-05-28