laravel-enso/dynamic-methods 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

laravel-enso/dynamic-methods

Composer 安装命令:

composer require laravel-enso/dynamic-methods

包简介

Dynamic methods, relations or accessors for models

README 文档

README

License Stable Downloads PHP Issues Merge Requests

Description

Dynamic Methods adds runtime-bound relations, instance methods, scopes, mutators, and static methods to Laravel models and classes.

The package scans Dynamics folders from configured vendor packages and from the host application, instantiates the dynamic definitions it finds, and binds their closures onto the target classes during boot.

In the Enso ecosystem it is the mechanism used to let independent packages augment shared models like User without editing those models directly.

Installation

Install the package:

composer require laravel-enso/dynamic-methods

The service provider is auto-registered and immediately binds all discovered dynamics on boot.

If you want to customize which vendor namespaces are scanned, publish the configuration:

php artisan vendor:publish --tag=dynamics-config

Default configuration:

return [
    'vendors' => ['laravel-enso'],
];

For application-level dynamics, place your classes under your app PSR-4 Dynamics folder. In a standard Laravel application this means app/Dynamics.

Features

  • Scans configured vendor packages for Dynamics classes.
  • Scans the host application for its own Dynamics classes.
  • Binds dynamic Eloquent relations through resolveRelationUsing().
  • Binds dynamic instance methods through resolveMethodUsing().
  • Supports dynamic scopes and attribute mutators through the Abilities trait.
  • Supports dynamic static methods through resolveStaticMethodUsing().
  • Keeps the dynamic definition format small and package-friendly.

Usage

To receive dynamic instance methods, relations, scopes, and mutators, a model should implement LaravelEnso\DynamicMethods\Contracts\DynamicMethods and use LaravelEnso\DynamicMethods\Traits\Abilities.

Example model:

use Illuminate\Database\Eloquent\Model;
use LaravelEnso\DynamicMethods\Contracts\DynamicMethods;
use LaravelEnso\DynamicMethods\Traits\Abilities;

class User extends Model implements DynamicMethods
{
    use Abilities;
}

Define a dynamic relation in a package or app Dynamics class:

use Closure;
use LaravelEnso\ActionLogger\Models\ActionLog;
use LaravelEnso\DynamicMethods\Contracts\Relation;
use LaravelEnso\Users\Models\User;

class ActionLogs implements Relation
{
    public function bindTo(): array
    {
        return [User::class];
    }

    public function name(): string
    {
        return 'actionLogs';
    }

    public function closure(): Closure
    {
        return fn (User $user) => $user->hasMany(ActionLog::class);
    }
}

Define a dynamic instance method:

use Closure;
use Illuminate\Support\Facades\Session;
use LaravelEnso\DynamicMethods\Contracts\Method;
use LaravelEnso\Users\Models\User;

class IsImpersonating implements Method
{
    public function bindTo(): array
    {
        return [User::class];
    }

    public function name(): string
    {
        return 'isImpersonating';
    }

    public function closure(): Closure
    {
        return fn () => Session::has('impersonating');
    }
}

After boot, the bound methods can be used as if they were defined on the model itself:

$user->actionLogs();
$user->isImpersonating();

::: warning Note The binder discovers dynamics by reading package composer.json PSR-4 configuration and then scanning a Dynamics directory relative to that namespace root.

In practice, the package also relies on laravel-enso/helpers for JsonReader, even though that dependency is currently not declared in composer.json. :::

API

Configuration

config/dynamics.php

Keys:

  • vendors

The binder scans:

  • vendor/<configured-vendor>/*
  • the application base path and its PSR-4 root

Service Provider

LaravelEnso\DynamicMethods\AppServiceProvider

Responsibilities:

  • merges enso.dynamics config
  • publishes config/dynamics.php
  • runs the binder during boot

Contracts

  • LaravelEnso\DynamicMethods\Contracts\Method
  • LaravelEnso\DynamicMethods\Contracts\Relation
  • LaravelEnso\DynamicMethods\Contracts\StaticMethod
  • LaravelEnso\DynamicMethods\Contracts\DynamicMethods
  • LaravelEnso\DynamicMethods\Contracts\DynamicStaticMethods

Definition contracts require:

  • name(): string
  • closure(): Closure
  • bindTo(): array

Traits

  • LaravelEnso\DynamicMethods\Traits\Methods
  • LaravelEnso\DynamicMethods\Traits\StaticMethods
  • LaravelEnso\DynamicMethods\Traits\Abilities

Abilities extends Methods and also makes dynamic scopes, accessors, and mutators discoverable through Eloquent's standard model checks.

Services

  • LaravelEnso\DynamicMethods\Services\Binder
  • LaravelEnso\DynamicMethods\Services\Dynamics
  • LaravelEnso\DynamicMethods\Services\Method
  • LaravelEnso\DynamicMethods\Services\Relation
  • LaravelEnso\DynamicMethods\Services\StaticMethod

Depends On

Required Enso packages:

Framework dependency:

Contributions

are welcome. Pull requests are great, but issues are good too.

Thank you to all the people who already contributed to Enso!

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固