Last active
February 3, 2016 19:32
-
-
Save jhaoda/2d6263449e7c7ec84fea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Model extends \Eloquent | |
{ | |
protected $observables = ['readed']; | |
protected static function boot() | |
{ | |
parent::boot(); | |
$hooks = ['before' => 'ing', 'after' => 'ed']; | |
$radicals = ['sav', 'creat', 'updat', 'delet']; | |
foreach ($radicals as $rad) { | |
foreach ($hooks as $hook => $event) { | |
$method = $hook . ucfirst($rad) . 'e'; | |
$eventMethod = $rad . $event; | |
static::$eventMethod(function($model) use ($method) { | |
return $model->$method($model); | |
}); | |
} | |
} | |
static::readed(function ($model) { | |
return $model->{'afterRead'}($model); | |
}); | |
} | |
public function setRawAttributes(array $attributes, $sync = false) | |
{ | |
parent::setRawAttributes($attributes, $sync); | |
$this->fireModelEvent('readed'); | |
} | |
public static function readed($callback) | |
{ | |
static::registerModelEvent('readed', $callback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment