Download the PHP package laravel-enso/dynamic-methods without Composer
On this page you can find all versions of the php package laravel-enso/dynamic-methods. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download laravel-enso/dynamic-methods
More information about laravel-enso/dynamic-methods
Files in laravel-enso/dynamic-methods
Package dynamic-methods
Short Description Dynamic methods, relations or accessors for models
License MIT
Homepage https://github.com/laravel-enso/dynamic-methods
Informations about the package dynamic-methods
Dynamic Methods
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:
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:
Default configuration:
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
Dynamicsclasses. - Scans the host application for its own
Dynamicsclasses. - Binds dynamic Eloquent relations through
resolveRelationUsing(). - Binds dynamic instance methods through
resolveMethodUsing(). - Supports dynamic scopes and attribute mutators through the
Abilitiestrait. - 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:
Define a dynamic relation in a package or app Dynamics class:
Define a dynamic instance method:
After boot, the bound methods can be used as if they were defined on the model itself:
::: 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.dynamicsconfig - publishes
config/dynamics.php - runs the binder during boot
Contracts
LaravelEnso\DynamicMethods\Contracts\MethodLaravelEnso\DynamicMethods\Contracts\RelationLaravelEnso\DynamicMethods\Contracts\StaticMethodLaravelEnso\DynamicMethods\Contracts\DynamicMethodsLaravelEnso\DynamicMethods\Contracts\DynamicStaticMethods
Definition contracts require:
name(): stringclosure(): ClosurebindTo(): array
Traits
LaravelEnso\DynamicMethods\Traits\MethodsLaravelEnso\DynamicMethods\Traits\StaticMethodsLaravelEnso\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\BinderLaravelEnso\DynamicMethods\Services\DynamicsLaravelEnso\DynamicMethods\Services\MethodLaravelEnso\DynamicMethods\Services\RelationLaravelEnso\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!