Added Model Events

This commit is contained in:
李铭昕 2019-01-04 14:04:05 +08:00
parent d1f737fee4
commit 1a84f2a4f8
16 changed files with 253 additions and 45 deletions

View File

@ -10,10 +10,11 @@
"support": {
},
"require": {
"php": ">=7.1",
"php": ">=7.2",
"hyperf/utils": "dev-master",
"psr/container": "^1.0",
"nesbot/carbon": "^2.0"
"nesbot/carbon": "^2.0",
"psr/event-dispatcher": "^0.7"
},
"require-dev": {
"malukenho/docheader": "^0.1.6",

View File

@ -12,7 +12,10 @@ declare(strict_types=1);
namespace Hyperf\Database\Model\Concerns;
use Hyperf\Contracts\Events\Dispatcher;
use Hyperf\Database\Model\Events\Event;
use Hyperf\Utils\Arr;
use Hyperf\Utils\Str;
use Psr\EventDispatcher\EventDispatcherInterface;
trait HasEvents
{
@ -228,37 +231,6 @@ trait HasEvents
}
}
/**
* Get the event dispatcher instance.
*
* @return \Hyperf\Contracts\Events\Dispatcher
*/
public static function getEventDispatcher()
{
return static::$dispatcher;
}
/**
* Set the event dispatcher instance.
*
* @param \Hyperf\Contracts\Events\Dispatcher $dispatcher
* @return void
*/
public static function setEventDispatcher(Dispatcher $dispatcher)
{
static::$dispatcher = $dispatcher;
}
/**
* Unset the event dispatcher for models.
*
* @return void
*/
public static function unsetEventDispatcher()
{
static::$dispatcher = null;
}
/**
* Register a single observer with the model.
*
@ -302,29 +274,23 @@ trait HasEvents
* @param bool $halt
* @return mixed
*/
protected function fireModelEvent($event, $halt = true)
protected function fireModelEvent(string $event)
{
if (! isset(static::$dispatcher)) {
$dispatcher = $this->getEventDispatcher();
if (!$dispatcher instanceof EventDispatcherInterface) {
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' : 'dispatch';
$result = $this->filterModelEventResults(
$this->fireCustomModelEvent($event, $method)
$this->fireCustomModelEvent($event, 'dispatch')
);
if ($result === false) {
return false;
}
return ! empty($result) ? $result : static::$dispatcher->{$method}(
"eloquent.{$event}: ".static::class,
$this
);
$eventName = 'Hyperf\\Database\\Model\\Events\\' . Str::studly($event);
return !empty($result) ? $result : $dispatcher->dispatch(new $eventName($this, $event));
}
/**

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Created extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Creating extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Deleted extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Deleting extends Event
{
}

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
use Hyperf\Database\Model\Model;
abstract class Event
{
protected $model;
protected $method;
public function __construct(Model $model, string $method)
{
$this->model = $model;
$this->method = $method;
}
/**
* @return Model
*/
public function getModel(): Model
{
return $this->model;
}
public function handle()
{
$method = $this->method;
if (method_exists($this->model, $method)) {
return $this->model->$method();
}
}
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class ForceDeleted extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Restored extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Restoring extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Retrieved extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Saved extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Saving extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Updated extends Event
{
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Events;
class Updating extends Event
{
}

View File

@ -15,6 +15,7 @@ use Hyperf\Database\ConnectionInterface;
use Hyperf\Database\Model\Model as BaseModel;
use Hyperf\DbConnection\ConnectionResolver;
use Hyperf\Framework\ApplicationContext;
use Psr\EventDispatcher\EventDispatcherInterface;
class Model extends BaseModel
{
@ -29,4 +30,9 @@ class Model extends BaseModel
$resolver = ApplicationContext::getContainer()->get(ConnectionResolver::class);
return $resolver->connection($connectionName);
}
public function getEventDispatcher()
{
return ApplicationContext::getContainer()->get(EventDispatcherInterface::class);
}
}