I'm thinking of a way to implement plugin system in my project. What I want to accomplish is being able to customize eloquent model for example. Let's say I have plugin called UserRoles and I want to add customRoles method to my user model without actual modification of source code.
User model is just an example. I will have base application which I want to make customizable by plugins without touching core files. Another idea crossed my mind:
class ExtendableModel extends Model
{
/**
* @var array
*/
protected static $extendedFillable = [];
/**
* @return array
*/
public function getFillable()
{
return array_merge($this->fillable, self::$extendedFillable);
}
/**
* @param string $fieldName
*
* @return $this
*/
public function addFillable($fieldName)
{
if (in_array($fieldName, self::$extendedFillable)) {
return $this;
}
self::$extendedFillable[] = $fieldName;
return $this;
}
}
class User extends ExtendableModel
Then I could bootstrap my extension with
app(\App\User::class)
->addFillable('role');
This allows me to add some fillable field, but I could use this idea to add closures too.