定制 drewlabs/mdl-cli 二次开发

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

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

drewlabs/mdl-cli

Composer 安装命令:

composer require drewlabs/mdl-cli

包简介

PHP script for generating classes, interface, mixin, ect... based on UML model written in YAML or JSON language

README 文档

README

MDL library is a php utility binary that internally uses @drewlabs/code-generator package to create project source code based on a textual UML definition language written in YAML, or JSON language.

MDL definition language

The mdl definition language is a rapid OOP Prototyping tool that abstract aways UML design graphical tools though a YAML or JSON configuration file that is used by the client to generate source codes. Below is a basic example of mdl modeling language written in YAML.

name: 'Drewlabs\\MiaccSdk'
path: "/Users/azandrewsidoine/Workspace/azlabs/lib/php/mdl/src"
directories:
  interfaces: "Contracts"
  mixins: "Traits"

mixins:
  -
    name: "BaseTrait"
    methods:
      -
        name: "baseTraitMethod"

  - name: "MyTrait"
    mixins:
      - "Traits\\BaseTrait"
    methods:
      - name: "sayHello"
        comment: "Say Hello to application users" # optional
        returns: "void" # Optional
        modifier: "public" # Optional
        static: false # Optional
        parameters:
          - name: "name"
            type: string
            default: "PHP"
            optional: true
            variadic: false # Optional
            reference: false # Optional

interfaces:
  - name: "LoggerInterface"
    methods: # Optional
      - name: "log"
        comment: "Log to the console or the standard ouput"
        returns: "void"
        modifier: "public"
        parameters:
          - name: "args"
            default: "PHP"
            optional: false
            variadic: true # Optional
            reference: false # Optional
      -
        name: "baseTraitMethod"
  - 
    name: "MyInterface"
    extends: "Contracts\\LoggerInterface"

classes:
  - name: "BaseClass"
    directory: "Core"
    abstract: true
    implements:
      - "Contracts\\LoggerInterface"
      - 'App\\ValueInterface'
  - name: "AbstractClass"
    abstract: true
    extends: "Core\\BaseClass"
  - name: "MyClass"
    final: true
    extends: "AbstractClass"
    constructor: true
    mixins:
      - 
        "Traits\\DatabaseTrait"
    implements:
      - "Contracts\\TestInterface"
    setter: true
    immutable: true
    properties:
      - name: "console"
        comment: "Injected console object"
        type: 'App\\Console' # Optional
        modifier: "protected" # Optional
        readonly: false  # Optional
        # default: null # Optional
        # constant: false # Optional
    methods:
      - name: "__invoke"
        comment: "Used when the class is invoked as function" # Optional
        returns: "array" # Optional
        modifier: "public" # Optional
        static: false # Optional
        throws:
          - "\\RuntimeException"

The design language consist of two main parts:

  • The project metadata definition As the name implies, the project metadata consist of metadata about the project for which the component are being generated. Below is the list of metadata properties that must be defined for a given project:

    • name : The name metadata defines the project PSR-4 compatible namespace under which the components are generated
    • path: This metadata defines the base path from which OOP component paths are generated.
    • directories: This metadata is optional but help the mdl-cli to separate interfaces from core implementations and also provide directory for mixins which are PHP trait components.
  • OOP components definition OOP components consist of actual classes, interfaces/contracts and mixin/PHP Trait objects generated for the given projects. OOP components are grouped under:

    • classes : which consist of PHP or UML class components
    • mixins: which is not part of UML definition language but help in implementing the DRY recommandation when developping software application. Basically, they consist of PHP traits components and are generated under the directory specify for mixins in the metada configuration.
    • interfaces: which contains the list of interfaces/contracts that might be implemented in the given project.

The MDL CLI

Once the definition language is written, we make use of the mdl-cli tool to generate the our project source code. The syntax for generating source code from language defintion file is:

<PATH_TO_BINARY_DIR>/mdl <PATH_TO_LANGUAGE_DEFINITION_DIRECTORY>/name.yml

For composer based project and assuming the language definition file is at the root of the project:

./vendor/bin/mdl $(pwd)/mdl.yml

Command options

The mdl-cli command support options that allows developpers to change the behaviour of the command line client.

  • settters

Setters are configured at class level in the configuration file. But to tell the cli tool to generate setters for all classes properties, we use the --set or --setters command line option.

./vendor/bin/mdl $(pwd)/mdl.yml --set

  • getters

As with setters, getters are normally configured at class definition level. But we can also tell the cli tool to generate getter class for all classes in the generated code using --get or getters flag.

./vendor/bin/mdl $(pwd)/mdl.yml --get

  • strict rule

PHP behave differently whenever the declare_strict(...) is added to a script file. By default, cli does not add this directive to the generated scripts. To tells the cli tool to add the required directive we use --strict flag in command line:

./vendor/bin/mdl $(pwd)/mdl.yml --get --strict

Version 0.3.x changes

From version 0.3.x, the client support the following flags

  • force

Force is added to allow users to bypass warning messages due to invalid configurations

./vendor/bin/mdl $(pwd)/mdl.yml --strict --force

  • reflect & reflection

Latest changes to the API allows application users to declare some classes are jsonnable, which uses PHP reflection API in class::fromJson implementation. This behavior is prevented by default due to the cost of using reflection API.

To add that behavior, the cli support a --reflect or --reflection class when running the cli.

./vendor/bin/mdl $(pwd)/mdl.yml --strict --reflection

Note Reflection can be enable or disabled at the class level by setting reflection property to true or false.

Note The documentation is still under development and might change as API changes occur.

drewlabs/mdl-cli 适用场景与选型建议

drewlabs/mdl-cli 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 95 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 drewlabs/mdl-cli 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-22