Support collect models.

This commit is contained in:
李铭昕 2019-08-23 15:00:43 +08:00
parent 06ef33dc3c
commit 73c1f470b8
5 changed files with 122 additions and 5 deletions

View File

@ -15,7 +15,6 @@
"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"

View File

@ -15,9 +15,9 @@
"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/event-dispatcher": "^1.0",
"psr/log": "^1.0"
},
"require-dev": {

View File

@ -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
{
}

View File

@ -14,6 +14,7 @@ namespace Hyperf\ModelListener;
use Hyperf\Di\Annotation\AbstractAnnotation;
use Hyperf\ModelListener\Collector\ObserverCollector;
use Hyperf\Utils\Arr;
/**
* @Annotation
@ -22,14 +23,27 @@ use Hyperf\ModelListener\Collector\ObserverCollector;
class Observer extends AbstractAnnotation
{
/**
* @var string
* @var array
*/
public $model;
public $models = [];
public function __construct($value = null)
{
if (is_string($value)) {
$this->models = [$value];
} elseif (is_array($value) && ! Arr::isAssoc($value)) {
$this->models = $value;
} else {
parent::__construct($value);
}
}
public function collectClass(string $className): void
{
parent::collectClass($className);
ObserverCollector::set($this->model, $className);
foreach ($this->models as $model) {
ObserverCollector::register($model, $className);
}
}
}

View File

@ -0,0 +1,60 @@
<?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\ModelListener;
use Hyperf\ModelListener\Collector\ObserverCollector;
use Hyperf\ModelListener\Observer;
use HyperfTest\ModelListener\Stub\ModelStub;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class AnnotationTest extends TestCase
{
protected function tearDown()
{
parent::tearDown();
ObserverCollector::clearObservables();
}
public function testAnnotationCollect()
{
$annotation = new Observer(ModelStub::class);
$annotation->collectClass('Foo');
$this->assertSame(['Foo'], ObserverCollector::getObservables(ModelStub::class));
// $annotation = new Observer(['model' => ModelStub::class]);
// $annotation->collectClass('Foo');
//
// $this->assertSame(['Foo'], ObserverCollector::getObservables(ModelStub::class));
}
public function testAnnotationCollectAssocArray()
{
$annotation = new Observer(['models' => [ModelStub::class]]);
$annotation->collectClass('Foo');
$this->assertSame(['Foo'], ObserverCollector::getObservables(ModelStub::class));
}
public function testAnnotationCollectArray()
{
$annotation = new Observer([ModelStub::class, 'ModelStub']);
$annotation->collectClass('Foo');
$this->assertSame(['Foo'], ObserverCollector::getObservables(ModelStub::class));
$this->assertSame(['Foo'], ObserverCollector::getObservables('ModelStub'));
}
}