krasnikov / ddd-core
0.0.1
2020-05-19 18:47 UTC
Requires
- php: ^7.4
- ext-json: *
- illuminate/http: 7.*
- ramsey/uuid: 4.*
This package is auto-updated.
Last update: 2024-09-20 04:25:35 UTC
README
用于创建使用 DDD 原则的 lumen 或 laravel 应用的类集
套件包含
- 使用 UUID 的基本模型
- 命令总线包
- 分页类
- 排序类
- 仓库接口和 postgres 及数组抽象实现
- 翻译特性
- UUID 特性
安装
composer require krasnikov/ddd-core
使用基本模型
<?php declare(strict_types=1); namespace App\Domain\Auth; use Krasnikov\DDDCore\BaseModel; class AuthToken extends BaseModel { }
使用 UUID 特性
<?php declare(strict_types=1); namespace App\Domain\User; use Illuminate\Database\Eloquent\Model;use Krasnikov\DDDCore\BaseModel; use Illuminate\Auth\Authenticatable; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Krasnikov\DDDCore\Uuid; use Laravel\Lumen\Auth\Authorizable; use Spatie\Permission\Traits\HasRoles; class User extends Model implements AuthenticatableContract, AuthorizableContract { use Authenticatable; use Authorizable; use HasRoles; use Uuid; }
命令总线
在基础控制器中定义命令总线
<?php namespace App\Http\Controllers; use Krasnikov\DDDCore\Command; use Krasnikov\DDDCore\CommandBusInterface; use Laravel\Lumen\Routing\Controller as BaseController; /** * Class Controller * @package App\Http\Controllers */ class Controller extends BaseController { /** * @var CommandBusInterface */ private $dispatcher; /** * Controller constructor. * @param CommandBusInterface $dispatcher */ public function __construct(CommandBusInterface $dispatcher) { $this->dispatcher = $dispatcher; } /** * @param Command $command * @return mixed */ public function execute(Command $command) { return $this->dispatcher->dispatch($command); } }
创建命令
<?php declare(strict_types=1); namespace App\Application\Dashboard\Role\GetRoleList; use Krasnikov\DDDCore\Command; /** * Class GetRoleList * @package App\Application\Dashboard\Role */ class GetRoleList implements Command {}
创建命令处理器
<?php declare(strict_types=1); namespace App\Application\Dashboard\Role\GetRoleList; use Krasnikov\DDDCore\Command; use Krasnikov\DDDCore\Handler; /** * Class GetRoleListHandler * @package App\Application\Dashboard\Role\GetRoleList */ class GetRoleListHandler implements Handler { /** * @param Command|GetRoleList $command */ public function handle(Command $command): void { // same code } }
在控制器中调用
/** * @param Request $request * @return JsonResponse * @throws ValidationException */ public function index(Request $request): JsonResponse { $res = $this->execute(GetRoleList::fromRequest($request)); return new JsonResponse($res); }
仓库
实体仓库接口必须扩展 RepositoryInterface
并定义筛选方法
<?php declare(strict_types=1); namespace App\Domain\Auth; use Krasnikov\DDDCore\RepositoryInterface; use Krasnikov\DDDCore\Sorting; use App\Domain\User\UserRepositoryInterface; /** * Class AuthTokenRepositoryInterface * @package App\Domain\Auth */ interface AuthTokenRepositoryInterface extends RepositoryInterface { /** * @param AuthTokenFilter $filter * @return $this */ public function filter(AuthTokenFilter $filter): self; }
Postgres 实体仓库必须实现实体仓库接口,扩展 AbstractPostgresRepository
并实现筛选和 __constructor 方法
<?php declare(strict_types=1); namespace App\Infrastructure\Persists\PostgresRepository; use App\Domain\Auth\AuthToken; use App\Domain\Auth\AuthTokenFilter; use App\Domain\Auth\AuthTokenNotFoundException; use App\Domain\Auth\AuthTokenRepositoryInterface; use Krasnikov\DDDCore\AbstractPostgresRepository; /** * Class EloquentAuthTokenRepository * @package App\Infrastructure\Persists\EloquentRepository */ class PostgresAuthTokenRepository extends AbstractPostgresRepository implements AuthTokenRepositoryInterface { /** * PostgresAuthTokenRepository constructor. */ public function __construct() { $this->model = new AuthToken(); $this->exceptionNotFound = AuthTokenNotFoundException::class; } /** * @inheritDoc */ public function filter(AuthTokenFilter $filter): AuthTokenRepositoryInterface { $this->makeBuilder(); //some code return $this; } }
Array 实体仓库必须实现实体仓库接口,扩展 AbstractArrayRepository
并实现筛选和 __constructor 方法
<?php declare(strict_types=1); namespace App\Infrastructure\Persists\ArrayRepository; use App\Domain\{Auth\AuthTokenFilter,Auth\AuthTokenRepositoryInterface}; use Krasnikov\DDDCore\AbstractArrayRepository; /** * Class ArrayAuthTokenRepository * @package App\Infrastructure\Persists\ArrayRepository */ class ArrayAuthTokenRepository extends AbstractArrayRepository implements AuthTokenRepositoryInterface { /** * ArrayAuthTokenRepository constructor. */ public function __construct() { parent::__construct(); $this->exceptionNotFound = AuthTokenNotFoundException::class; } /** * @inheritDoc */ public function filter(AuthTokenFilter $filter): AuthTokenRepositoryInterface { $result = $this->seed; if ($this->result->count()) { $result = $this->result; } //some code $this->result = $result; return $this; } }
- 在您的控制器中使用
Krasnikov\DDDCore\Pagination::fromRequest($request)
设置分页并将对象发送到仓库; - 在您的控制器中使用
Krasnikov\DDDCore\Sorting::fromRequest($request)
设置排序并将对象发送到仓库。 - 您可以在控制器中使用
Krasnikov\EloquentJSON\PaginateRequest
或在其表单请求中扩展进行分页验证。
示例分页和排序 URL
GET /articles?page[number]=2&page[size]=10&sort=title,-createdAt HTTP/1.1
Accept: application/vnd.api+json