mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 11:48:08 +08:00
Added Model Events
This commit is contained in:
parent
d1f737fee4
commit
1a84f2a4f8
@ -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",
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
16
src/database/src/Model/Events/Created.php
Normal file
16
src/database/src/Model/Events/Created.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Creating.php
Normal file
16
src/database/src/Model/Events/Creating.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Deleted.php
Normal file
16
src/database/src/Model/Events/Deleted.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Deleting.php
Normal file
16
src/database/src/Model/Events/Deleting.php
Normal 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
|
||||
{
|
||||
}
|
43
src/database/src/Model/Events/Event.php
Normal file
43
src/database/src/Model/Events/Event.php
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
16
src/database/src/Model/Events/ForceDeleted.php
Normal file
16
src/database/src/Model/Events/ForceDeleted.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Restored.php
Normal file
16
src/database/src/Model/Events/Restored.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Restoring.php
Normal file
16
src/database/src/Model/Events/Restoring.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Retrieved.php
Normal file
16
src/database/src/Model/Events/Retrieved.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Saved.php
Normal file
16
src/database/src/Model/Events/Saved.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Saving.php
Normal file
16
src/database/src/Model/Events/Saving.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Updated.php
Normal file
16
src/database/src/Model/Events/Updated.php
Normal 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
|
||||
{
|
||||
}
|
16
src/database/src/Model/Events/Updating.php
Normal file
16
src/database/src/Model/Events/Updating.php
Normal 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
|
||||
{
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user