定制 yorus/domain-extra-library 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yorus/domain-extra-library

Composer 安装命令:

composer require yorus/domain-extra-library

包简介

YoRus Consulting - Common PHP components

README 文档

README

This package provides additional features to the domain layer of a Symfony application:

  • Validation constraints
  • Faker providers
    • Uuid
  • Doctrine types for specific data types
    • jsonb
  • Repository interfaces to manage the data access layer
  • Console commands to generate DDD/CQRS classes

Installation

composer require yorus/domain-extra-library

Add a configuration file to your project to define the services and parameters needed by the library.

# config/packages/yorus_domain_extra_library.yaml

yorus_domain_extra_library:
  namespace: YourNamespace\

This configuration will be used to generate the classes (DDD/CQRS generator) with the correct namespace (and only for the generator scope)

Activate the library in the config/bundles.php file of the project.

YoRus\DomainExtraLibrary\App\Bundle\YorusDomainExtraLibraryBundle::class => ['all' => true],

Usage

Validation constraints

The validation constraints are used to validate the data received by the controllers. The constraints are configurable which allows them to be used in different domains. We distinguish 2 types of constraints:

  • property constraint
  • class constraint

Property constraint

It is used to validate a single property of the request.

Contrainte de classe

It needs several properties of the request to do its validation work. This type of constraint will - most of the time - be placed within a validation group, and this group will be played first. Indeed, if the global validation fails, it is not useful to validate each of the properties.

Some constraints require the implementation of a specific interface at the level of the repository used. Configuration is to be placed in the config\services.yaml file of the project, or any other configuration file of the application.

For example:

app.constraint.validator.category:
        class: \YoRus\DomainExtraLibrary\Infra\Validator\Constraint\ResourceMustExistConstraintValidator
        arguments:
            - '@App\Infra\Repository\DoctrineORM\CategoryReader'
        tags:
            - { name: validator.constraint_validator, alias: app.constraint.category }

app.constraint.validator.formation:
  class: \YoRus\DomainExtraLibrary\Infra\Validator\Constraint\ResourceMustExistConstraintValidator
  arguments:
    - '@App\Infra\Repository\DoctrineORM\FormationReader'
  tags:
    - { name: validator.constraint_validator, alias: app.constraint.formation }

The code above is used to make available the ResourceMustExistConstraint constraint for the Category resource. It will be used in the validation file of the command / query. It uses the CategoryReader service to check if the category exists in the database.

Constraints are used in the yaml file of the command / query validator. For example:

App\Command\Formation\UpdateFormationCommand:
  group_sequence:
    - UpdateFormationCommand
    - resource
  properties:
    id:
      - NotNull: ~
      - Uuid: ~
      - \YoRus\DomainExtraLibrary\Infra\Validator\Constraint\ResourceMustExistConstraint:
          reader: app.constraint.formation
          resource: Formation
          groups: [resource]
    payload:
      - Collection:
          fields:
            label:
              - NotNull: ~
              - NotBlank: ~
              - Length:
                  min: 3
            description:
              - NotNull: ~
              - NotBlank: ~
              - Length:
                  min: 10
            category:
              - NotNull: ~
              - Uuid: ~
              - \YoRus\DomainExtraLibrary\Infra\Validator\Constraint\ResourceMustExistConstraint:
                  reader: app.constraint.category
                  resource: Category
                  groups: [resource]
          allowExtraFields: false

See the official documentation for more details: https://symfony.com/doc/current/validation/custom_constraint.html

Available constraints

ResourceMustExistConstraint

Check that the resource represented by the property on which the constraint is applied actually exists in the database.

  • Constraint type: property
  • Request type: create, retrieve, update, delete

Parameters:

  • reader: the id of the service representing the validator linked to the constraint
  • resource: the name of the current resource
  • code: the HTTP code returned in case the constraint is not validated

Configuration example:

app.constraint.validator.stock:
        class: \YoRus\DomainExtraLibrary\Infra\Validator\Constraint\ResourceMustExistConstraintValidator
        arguments:
            - '@App\Stock\Infra\Repository\DoctrineORM\StockReader'
        tags:
            - { name: validator.constraint_validator, alias: app.constraint.stock }

Usage example:

- YoRus\DomainExtraLibrary\Infra\Validator\Constraint\ResourceMustExistConstraint:
	reader: app.constraint.stock
	resource: Stock
	code: 400
	groups: [Resource]

Faker providers

The library provides a Faker provider to generate UUIDs, useful for generating test data, especially when creating fixtures.

App\Domain\Entity\User:
    user.john_doe:
        __construct: [ <uuidObject('69e4234a-b3c8-4322-aa7f-24164913269a')>, 'John', 'Doe' ]

Doctrine types

The library provides a Doctrine type for the jsonb data type.

Repository interfaces

The library provides interfaces to manage the data access layer. These interfaces are used to define the methods that the repository must implement.

RequiredFinderRepositoryInterface

This interface propose a findRequired method to find an entity by its identifier. Unlike the find method, it suggests to throws an EntityNotFoundException exception if the entity is not found.

Console commands

The library provides console commands to generate DDD/CQRS classes. The generated classes are the following:

  • Controller
  • Entity
  • Repository
  • Query
  • QueryHandler

To use the generator, run the following command:

php bin/console yorus:ddd-cqrs:generate

To obtain help on the command, run:

php bin/console yorus:ddd-cqrs:generate --help

The command accepts the following options:

  • --context: the context in which classes must be generated. Usefull to separate classes by domain or to manage some bounded contexts.
  • --dry-run: to simulate the generation of classes without writing them to the disk. It will display the generated files in the console.

The command will ask you to enter the following information:

  • The name of the domain to manage
  • The list of parameters to manage in the entity

The parameters have the following syntax:

name:type
name*:type
name:?type

The name is the name of the parameter. The type is the type of the parameter. The * indicates that the parameter will be managed as a primary key in the entity. The ? indicates that the parameter is optional

For example:

id*:uuid
name:string
description:string

If no type is provided, the parameter will be managed as a string. It must have one and only one primary key.

yorus/domain-extra-library 适用场景与选型建议

yorus/domain-extra-library 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 yorus/domain-extra-library 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yorus/domain-extra-library 我们能提供哪些服务?
定制开发 / 二次开发

基于 yorus/domain-extra-library 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-27