sidebarDepth |
---|
3 |
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
click on the photo to view a a larger screenshot
Comes pre-installed in Enso.
To install outside of Enso:
- install the package
composer require laravel-enso/dynamic-methods
- 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.
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'
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.
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);
});
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.
are welcome. Pull requests are great, but issues are good too.
This package is released under the MIT license.