Skip to content

Commit

Permalink
Dispatchable events
Browse files Browse the repository at this point in the history
  • Loading branch information
chelout committed Mar 28, 2018
1 parent b20a34c commit cf35e10
Show file tree
Hide file tree
Showing 22 changed files with 682 additions and 565 deletions.
78 changes: 70 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@ Missing relationship events for Laravel

1. Install package with composer

```
composer require chelout/laravel-relationship-events:dev-master
```
#### Stable branch:
```
composer require chelout/laravel-relationship-events
```

#### Development branch:
```
composer require chelout/laravel-relationship-events:dev-master
```

2. Use necessary trait in your model.
#### Available traits:
- HasOneEvents
- HasBelongsToEvents
- HasManyEvents
- HasBelongsToManyEvents
- HasBelongsToManyEvents ()
> Note: should be used with HasAttributesMethods trait
- HasMorphOneEvents
- HasMorphToEvents
- HasMorphManyEvents
- HasMorphToManyEvents
> Note: should be used with HasAttributesMethods trait
- HasMorphedByManyEvents
> Note: should be used with HasAttributesMethods trait
```php
...

use Chelout\RelationshipEvents\Relationships\Concerns\HasOneEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
use HasOneEvents;
...

public static function boot()
{
parent::boot();
Expand All @@ -47,10 +56,63 @@ class User extends Model
dump('hasOneUpdated', $parent, $related);
});
}
...

}
```

```php

use Chelout\RelationshipEvents\Relationships\Concerns\HasMorphToManyEvents;
use Chelout\RelationshipEvents\Relationships\Traits\HasAttributesMethods;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasMorphToManyEvents; // should be used with HasAttributesMethods trait
use HasAttributesMethods;

public static function boot()
{
parent::boot();

/**
* Many To Many Polymorphic Relations Events.
*/
static::morphToManyAttached(function ($relation, $parent, $ids, $attributes) {
dump('morphToManyAttached', $relation, $parent, $ids, $attributes);
});

static::morphToManyDetached(function ($relation, $parent, $ids) {
dump('morphToManyDetached', $relation, $parent, $ids);
});
}

public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}

}
```

3. Dispatchable relationship events.
It is possible to fire event classes via $dispatchesEvents properties:

```php

use Chelout\RelationshipEvents\Relationships\Concerns\HasOneEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
use HasOneEvents;

protected $dispatchesEvents = [
'hasOneSaved' => HasOneSaved::class,
];

}
```

## Relationships
### One To One:
Expand Down Expand Up @@ -242,7 +304,7 @@ class User extends Model

- ~~Implement Many To Many polymorphic relations events.~~
- ~~Dive into Has Many Through and understand if there could be some events.~~
- Move fireModelRelationshipEvent() method to relation concerns in order to create dispatchable relationship events:
- ~~Move fireModelRelationshipEvent() method to relation concerns in order to create dispatchable relationship events:~~
```php
protected $dispatchesEvents = [
'hasOneSaving' => \App\Events\UserSaving::class,
Expand Down
59 changes: 9 additions & 50 deletions src/Relationships/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ class BelongsTo extends BelongsToBase implements EventDispatcher
/**
* Associate the model instance to the given parent.
*
* @param \Illuminate\Database\Eloquent\Model|int|string $model
* @param \Illuminate\Database\Eloquent\Model|int|string $model
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function associate($model)
{
$this->fireModelRelationshipEvent('associating', $model);
$this->parent->fireModelBelongsToEvent('associating', $this->relation, $model);

$result = parent::associate($model);

$this->fireModelRelationshipEvent('associated', $model);
$this->parent->fireModelBelongsToEvent('associated', $this->relation, $model);

return $result;
}
Expand All @@ -40,12 +40,12 @@ public function dissociate()
{
$parent = $this->getResults();

$this->fireModelRelationshipEvent('dissociating', $parent);
$this->parent->fireModelBelongsToEvent('dissociating', $this->relation, $parent);

$result = parent::dissociate();

if (! is_null($parent)) {
$this->fireModelRelationshipEvent('dissociated', $parent);
$this->parent->fireModelBelongsToEvent('dissociated', $this->relation, $parent);
}

return $result;
Expand All @@ -54,61 +54,20 @@ public function dissociate()
/**
* Update the parent model on the relationship.
*
* @param array $attributes
* @param array $attributes
*
* @return mixed
*/
public function update(array $attributes)
{
$related = $this->getResults();

$this->fireModelRelationshipEvent('updating', $related);
$this->parent->fireModelBelongsToEvent('updating', $this->relation, $related);

if ($result = $related->fill($attributes)->save()) {
$this->fireModelRelationshipEvent('updated', $related);
$this->parent->fireModelBelongsToEvent('updated', $this->relation, $related);
}

return $result;
}

/**
* Fire the given event for the model relationship.
*
* @param string $event
* @param \Illuminate\Database\Eloquent\Model|int|string $related
* @param bool $halt
*
* @return mixed
*/
protected function fireModelRelationshipEvent($event, $parent, $halt = true)
{
if (! isset(static::$dispatcher)) {
return true;
}

// First, we will get the proper method to call on the event dispatcher, and then we
// will attempt to fire a custom, object based event for the given event. If that
// returns a result we can return that result, or we'll call the string events.
$method = $halt ? 'until' : 'fire';

// $result = $this->filterModelEventResults(
// $this->fireCustomModelEvent($event, $method)
// );

// if ($result === false) {
// return false;
// }

// return ! empty($result) ? $result : static::$dispatcher->{$method}(
// "eloquent.{$event}: ".static::class, $this
// );

return static::$dispatcher->{$method}(
'eloquent.' . static::$relationEventName . ucfirst($event) . ': ' . get_class($this->child), [
$this->relation,
$this->child,
$parent,
]
);
}
}
Loading

0 comments on commit cf35e10

Please sign in to comment.