Dynamic Methods

Codacy Badge StyleCI License Total Downloads Latest Stable Version

Dynamic method dependency for Laravel Enso.

This package can work independently of the Enso ecosystem.

For live examples and demos, you may visit laravel-enso.com

Watch the demo

click on the photo to view a a larger screenshot

Installation

Comes pre-installed in Enso.

To install outside of Enso:

  1. install the package composer require laravel-enso/dynamic-methods

Features

  • provides a trait that can be used to dynamically add methods on a class that would not normally have them
  • additionally, a series of other traits are available that, within the Laravel ecosystem, can be used to ensure that the dynamically added methods can be used as/for accessors and relationships
  • the traits can be used as an alternative to repeatedly extending classes, which can prove difficult and error prone in the context of building packages on top of other packages, on top of other packages.

Usage

Methods - LaravelEnso\DynamicMethods\app\Traits\Methods

This is the core trait that permits adding a method to an object via its main method addDynamicMethod. The method takes a method name and a closure.

Example:

Product::addDynamicMethod('foo', function () {
    return 'bar';
});

Afterwards, you can simply call the newly added method on Product instances, as if the method had been there all along:

$p = new Product();
$p->foo(); //'bar'

Mutators - LaravelEnso\DynamicMethods\app\Traits\Mutators

The trait uses Methods and overwrites Laravel Model's hasGetMutator($key) and hasSetMutator($key) methods so that when dynamically adding mutator methods on models, the newly added methods are used if necessary.

Relations - LaravelEnso\DynamicMethods\app\Traits\Relations

The trait uses Methods and overwrites Laravel Model's getRelationValue($key) method so that when dynamically adding relationship methods on models, the newly added methods are used if necessary.

Example:

Product::addDynamicMethod('manufacturer', function () {
    return $this->belongsTo(Manufacturer::class);
});

StaticMethods - LaravelEnso\DynamicMethods\app\Traits\StaticMethods

Similar to Methods, the trait permits adding a static method to an object via its main method addDynamicStaticMethod. The method takes a method name and a closure.

Of course, when calling the method, the call should be made statically.

Contributions

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

License

This package is released under the MIT license.