承接 phpnomad/decorator 相关项目开发

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

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

phpnomad/decorator

最新稳定版本:1.0.0

Composer 安装命令:

composer require phpnomad/decorator

包简介

Assists with implementing decorator patterns.

README 文档

README

Latest Version Total Downloads PHP Version License

phpnomad/decorator is a small helper for writing decorator classes in PHP. It ships a single trait, WithDecoratedInstance, that forwards method calls to a wrapped object through __call() using an explicit allowlist, so a decorator only has to implement the behavior it actually changes.

Installation

composer require phpnomad/decorator

Quick Start

Add the trait to a class that wraps another instance, assign the wrapped object, and list the methods you want forwarded.

<?php

use PHPNomad\Decorator\Traits\WithDecoratedInstance;

class CachingRepository
{
    use WithDecoratedInstance;

    protected array $allowedMethods = ['find', 'all'];

    public function __construct(Repository $repository)
    {
        $this->decoratedInstance = $repository;
    }

    public function save(Model $model): void
    {
        // Custom behavior here, then delegate if needed.
        $this->decoratedInstance->save($model);
    }
}

Calls to find() or all() on a CachingRepository are forwarded to the wrapped Repository. Calls to any method that is not on $allowedMethods and is not defined on the decorator itself raise an E_USER_ERROR.

Overview

The package exports one trait, PHPNomad\Decorator\Traits\WithDecoratedInstance. Mixing it into a class adds four things.

  • A $decoratedInstance property that holds the object being wrapped.
  • An $allowedMethods array so that method forwarding is opt-in rather than automatic.
  • A __call() implementation that routes allowlisted method calls to $decoratedInstance via call_user_func_array.
  • An explicit E_USER_ERROR for any other inaccessible method call, so typos and unintended forwards fail loudly instead of silently.

Because the allowlist is a plain array property, you control exactly which parts of the wrapped object's surface leak through the decorator.

Documentation

Broader PHPNomad framework documentation lives at phpnomad.com.

License

Released under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-18