Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added listener for model. #420

Merged
merged 24 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [#401](https://github.com/hyperf-cloud/hyperf/pull/401) Optimized server and Fixed middleware that user defined does not works.
- [#402](https://github.com/hyperf-cloud/hyperf/pull/402) Added Annotation AsyncQueueMessage.
- [#418](https://github.com/hyperf-cloud/hyperf/pull/418) Allows send WebSocket message to any fd in current server, even the worker process does not hold the fd
- [#420](https://github.com/hyperf-cloud/hyperf/pull/420) Added listener for model.
- [#441](https://github.com/hyperf-cloud/hyperf/pull/441) Automatically close the spare redis client when it is used in low frequency.

## Changed
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"Hyperf\\Logger\\": "src/logger/src/",
"Hyperf\\Memory\\": "src/memory/src/",
"Hyperf\\ModelCache\\": "src/model-cache/src/",
"Hyperf\\ModelListener\\": "src/model-listener/src/",
"Hyperf\\Paginator\\": "src/paginator/src/",
"Hyperf\\Pool\\": "src/pool/src/",
"Hyperf\\Process\\": "src/process/src/",
Expand Down Expand Up @@ -192,6 +193,7 @@
"HyperfTest\\LoadBalancer\\": "src/load-balancer/tests/",
"HyperfTest\\Logger\\": "src/logger/tests/",
"HyperfTest\\ModelCache\\": "src/model-cache/tests/",
"HyperfTest\\ModelListener\\": "src/model-listener/tests/",
"HyperfTest\\Paginator\\": "src/paginator/tests/",
"HyperfTest\\Pool\\": "src/pool/tests/",
"HyperfTest\\Process\\": "src/process/tests/",
Expand Down Expand Up @@ -240,6 +242,7 @@
"Hyperf\\Logger\\ConfigProvider",
"Hyperf\\Memory\\ConfigProvider",
"Hyperf\\ModelCache\\ConfigProvider",
"Hyperf\\ModelListener\\ConfigProvider",
"Hyperf\\Paginator\\ConfigProvider",
"Hyperf\\Pool\\ConfigProvider",
"Hyperf\\Process\\ConfigProvider",
Expand Down
2 changes: 1 addition & 1 deletion src/database/src/Model/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(Model $model, ?string $method = null)
public function handle()
{
if (method_exists($this->getModel(), $this->getMethod())) {
return $this->getModel()->{$this->getMethod()}($this);
$this->getModel()->{$this->getMethod()}($this);
}

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace Hyperf\DbConnection\Listener;
namespace HyperfTest\Database\Stubs;

use Hyperf\Database\Model\Events\Event;
use Hyperf\Event\Annotation\Listener;
Expand All @@ -19,7 +19,7 @@
/**
* @Listener
*/
class ModelEventListener implements ListenerInterface
class ModelEventListenerStub implements ListenerInterface
{
public function listen(): array
{
Expand Down
23 changes: 23 additions & 0 deletions src/database/tests/Stubs/ModelObserverStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace HyperfTest\Database\Stubs;

use Hyperf\Database\Model\Events\Updating;

class ModelObserverStub
{
public function updating(Updating $event)
{
$event->getModel()->foo = 'bar';
}
}
2 changes: 1 addition & 1 deletion src/db-connection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"hyperf/framework": "~1.1.0",
"hyperf/database": "~1.1.0",
"hyperf/di": "~1.1.0",
"hyperf/event": "~1.1.0",
"hyperf/model-listener": "~1.1.0",
"hyperf/pool": "~1.1.0",
"hyperf/utils": "~1.1.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/model-listener/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tests export-ignore
9 changes: 9 additions & 0 deletions src/model-listener/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) Hyperf

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
58 changes: 58 additions & 0 deletions src/model-listener/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "hyperf/model-listener",
"description": "A model listener for Hyperf.",
"license": "MIT",
"keywords": [
"php",
"swoole",
"hyperf",
"model-listener"
],
"support": {
},
"require": {
"php": ">=7.2",
"hyperf/contract": "~1.1.0",
"hyperf/database": "~1.1.0",
"hyperf/di": "~1.1.0",
"hyperf/event": "~1.1.0",
"hyperf/utils": "~1.1.0",
"psr/container": "^1.0",
"psr/log": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.9",
"malukenho/docheader": "^0.1.6",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^7.0"
},
"suggest": {
},
"autoload": {
"psr-4": {
"Hyperf\\ModelListener\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\ModelListener\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
},
"hyperf": {
"config": "Hyperf\\ModelListener\\ConfigProvider"
}
},
"bin": [
],
"scripts": {
"cs-fix": "php-cs-fixer fix $1",
"test": "phpunit --colors=always"
}
}
44 changes: 44 additions & 0 deletions src/model-listener/src/AbstractListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace Hyperf\ModelListener;

use Hyperf\Database\Model\Events\Created;
use Hyperf\Database\Model\Events\Creating;
use Hyperf\Database\Model\Events\Deleted;
use Hyperf\Database\Model\Events\Deleting;
use Hyperf\Database\Model\Events\ForceDeleted;
use Hyperf\Database\Model\Events\Restored;
use Hyperf\Database\Model\Events\Restoring;
use Hyperf\Database\Model\Events\Retrieved;
use Hyperf\Database\Model\Events\Saved;
use Hyperf\Database\Model\Events\Saving;
use Hyperf\Database\Model\Events\Updated;
use Hyperf\Database\Model\Events\Updating;

/**
* @method retrieved(Retrieved $event)
* @method creating(Creating $event)
* @method created(Created $event)
* @method updating(Updating $event)
* @method updated(Updated $event)
* @method saving(Saving $event)
* @method saved(Saved $event)
* @method restoring(Restoring $event)
* @method restored(Restored $event)
* @method deleting(Deleting $event)
* @method deleted(Deleted $event)
* @method forceDeleted(ForceDeleted $event)
*/
abstract class AbstractListener
{
}
51 changes: 51 additions & 0 deletions src/model-listener/src/Annotation/ModelListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace Hyperf\ModelListener\Annotation;

use Hyperf\Di\Annotation\AbstractAnnotation;
use Hyperf\ModelListener\Collector\ListenerCollector;
use Hyperf\Utils\Arr;

/**
* @Annotation
* @Target({"CLASS"})
*/
class ModelListener extends AbstractAnnotation
{
/**
* @var array
*/
public $models = [];

public function __construct($value = null)
{
parent::__construct($value);

if ($value = $value['value'] ?? null) {
if (is_string($value)) {
$this->models = [$value];
} elseif (is_array($value) && ! Arr::isAssoc($value)) {
$this->models = $value;
}
}
}

public function collectClass(string $className): void
{
parent::collectClass($className);

foreach ($this->models as $model) {
ListenerCollector::register($model, $className);
}
}
}
56 changes: 56 additions & 0 deletions src/model-listener/src/Collector/ListenerCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace Hyperf\ModelListener\Collector;

class ListenerCollector
{
/**
* User exposed listeners.
*
* These are extra user-defined events listeners may subscribe to.
*
* @var array
*/
protected static $listeners = [];

/**
* Register a single listener with the model.
*/
public static function register(string $model, string $listener): void
{
static::$listeners[$model] = array_unique(
array_merge(
static::$listeners[$model] ?? [],
[$listener]
)
);
}

public static function setListenersForModel(string $model, array $listeners): void
{
static::$listeners[$model] = $listeners;
}

public static function getListenersForModel(string $model): array
{
return static::$listeners[$model] ?? [];
}

/**
* Clear all registered listeners.
*/
public static function clearListeners(): void
{
static::$listeners = [];
}
}
31 changes: 31 additions & 0 deletions src/model-listener/src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace Hyperf\ModelListener;

class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => [
],
'commands' => [
],
'scan' => [
'paths' => [
__DIR__,
],
],
];
}
}