承接 doekenorg/decorate-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

doekenorg/decorate-php

Composer 安装命令:

composer require doekenorg/decorate-php

包简介

Create a PHP decorator or proxy based on an interface or (abstract) class.

README 文档

README

Easily create decorators and proxies with a simple (composer) command.

Do you enjoy using decorators in PHP, but hate having to implement one with large amounts of methods? Then this is the plugin for you! You can now quickly create a (final or abstract) class from an interface with all the methods already implemented and forwarded to the next instance. This little time saver lets you get on with the things you enjoy.

Installation

composer global require doekenorg/decorate-php

Notice: This is a global plugin, so don't forget global in the command!

Usage

The plugin adds a decorate command to your composer instance. It needs a source class (the interface you want to decorate) and a target class. You can optionally provide the name of the variable it uses for the next class. Currently, the plugin only works for composer projects.

Create a new decorator

composer decorate "Package\Namespace\SomeInterface" "My\Namespace\DestinationClass"

This will create, and write, a file called DestinationClass.php in the appropriate folder mapped in your psr-4 autoloader configuration.

A file like this will be created:

<?php

namespace My\Namespace;

use Package\Namespace\SomeInterface;

class DestinationClass implements SomeInterface {
    public function __construct(private SomeInterface $next) {
    }
    
    public function any_method(string $variable, ...$variadic_variable): void {
        $this->next->any_method($variable, ...$variadic_variable);
    }
}

Create a new decorator with a specific variable name.

By default, the decorated instance is mapped to a variable called $next. You can overwrite this by providing it as the third parameter. In the next example the variable will be called $client.

composer decorate "Package\Namespace\ClientInterface" "My\Namespace\MyClient" "client"

Here the output will be something like:

<?php

namespace My\Namespace;

use Package\Namespace\ClientInterface;

class MyClient implements ClientInterface {
    public function __construct(private ClientInterface $client) {
    }

    // ...
}

Note: The command will not overwrite an existing file. Provide the --overwrite option to force write.

Options

The command comes with the following options:

  • --spaces will replace the indentation from tabs to 4 spaces by default. If you want 2 spaces; use --spaces=2. You can also provide a default in the global configuration (see next section).
  • --output will output the code to the console instead of writing it.
  • --overwrite will force-overwrite the file if it already exists.
  • --abstract will create an abstract class. The next variable will now be protected instead of private.
  • --final will create a final class.

Global configuration

You can use the following configuration in the extra key of your global composer.json (usually in ~/.composer).

{
  //...
  "extra": {
    "decorate-php": {
      "spaces": 4,
      "variable": "next",
      "use-property-promotion": true,
      "use-func-get-args": false,
      "use-final-class": true
    }
  }
}
  • spaces will set the indentation to this amount of spaces by default; removing the need for --spaces.
  • variable will overwrite the default of next with this value.
  • use-property-promotion whether to use property promotion in the constructor (true by default).
  • use-func-get-args whether to replace the actual arguments on the method call with ...func_get_args().
  • use-final-class whether to create a final class by default.

Other information

PSR-4 only

Writing files is only supported for PSR-4 namespaces provided in the autoload key of your project. No PSR-0 support.

No decorating final classes

Obviously, you cannot create a decorator for a final class.

Decorating abstract classes

Although not common, you can also decorate abstract classes. In this case, any final methods are ignored when creating the decorator.

(Decorating non-abstract classes isn't really useful; but I kept the possibility for the extenders among us.)

Constructors

When an abstract class or interface declares a __construct method; it will append the next instance as the first argument. In case of an abstract class, it will also call the parent::__construct() method with the appropriate arguments.

Caveats

  • Because composer uses some packages under the hood; it might use those interfaces, instead of the one in your project. For example: Psr\Container and Psr\Log namespaces. There might be a version discrepancy in that case.

doekenorg/decorate-php 适用场景与选型建议

doekenorg/decorate-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 29, 最近一次更新时间为 2024 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 doekenorg/decorate-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2024-01-04