Skip to content

Commit

Permalink
The service provider is now using a new simplified technique for addi…
Browse files Browse the repository at this point in the history
…ng listeners to models
  • Loading branch information
shooka committed Sep 9, 2015
1 parent 3e10ef3 commit 3baaadd
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/ModelEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

abstract class ModelEvent
{
public abstract function handle($model);
//
}
20 changes: 1 addition & 19 deletions src/ModelEventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,11 @@ public function boot()
{
foreach ($this->listeners() as $model => $listeners) {
foreach ($listeners as $listener) {
$this->registerExternalModelEvent($model, $listener);
$model::observe($listener);
}
}
}

protected function registerExternalModelEvent($model, $listener)
{
$event = $this->extractEventFromListener($model, $listener);
call_user_func([$model, $event], function ($model) use ($listener) {
return (new $listener)->handle($model);
});
}

protected function extractEventFromListener($model, $listener)
{
$model_name = class_basename($model);
$listener_name = class_basename($listener);

$exploded_listener_name = explode($model_name, $listener_name, -1);

return camel_case(implode($model_name, $exploded_listener_name));
}

public function listeners()
{
return $this->listeners;
Expand Down
2 changes: 1 addition & 1 deletion tests/support/modelevents/CreatingPersonEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class CreatingPersonEvent extends ModelEvent
{
public function handle($person)
public function creating(Person $person)
{
if(!$person->name) {
return false;
Expand Down

0 comments on commit 3baaadd

Please sign in to comment.