Skip to content

Commit 393fb3d

Browse files
committed
Repository支持钩子事件监听功能
1 parent 120738b commit 393fb3d

10 files changed

+337
-14
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Swoft\Admin\Bean\Annotation;
4+
5+
/**
6+
*
7+
* @Annotation
8+
* @Target({"CLASS"})
9+
*/
10+
class AdminRepositoryListener
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $listener = '';
16+
17+
public function __construct(array $values)
18+
{
19+
if (!empty($values['value'])) {
20+
$this->listener = $values['value'];
21+
}
22+
if (!empty($values['listener'])) {
23+
$this->listener = $values['listener'];
24+
}
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getValue()
31+
{
32+
return $this->listener;
33+
}
34+
35+
}

src/Bean/Collector/AdminRepositoryCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function collect(
3737
$value = $objectAnnotation->getValue();
3838

3939
if ($value) {
40-
Admin::registerRepository($value, new $className);
40+
Admin::registerRepository($value, $className);
4141
}
4242
}
4343

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Swoft\Admin\Bean\Collector;
4+
5+
use Swoft\Admin\Bean\Annotation\AdminRepositoryListener;
6+
use Swoft\Bean\CollectorInterface;
7+
8+
/**
9+
* The collector AdminRepositoryListener
10+
*/
11+
class AdminRepositoryListenerCollector implements CollectorInterface
12+
{
13+
/**
14+
* @var array
15+
*/
16+
private static $values = [];
17+
18+
/**
19+
* @param string $className
20+
* @param object $objectAnnotation
21+
* @param string $propertyName
22+
* @param string $methodName
23+
* @param null $propertyValue
24+
*
25+
* @return mixed
26+
*/
27+
public static function collect(
28+
string $className,
29+
$objectAnnotation = null,
30+
string $propertyName = '',
31+
string $methodName = '',
32+
$propertyValue = null
33+
)
34+
{
35+
if ($objectAnnotation instanceof AdminRepositoryListener) {
36+
$value = $objectAnnotation->getValue();
37+
if (!isset(self::$values[$value])) {
38+
self::$values[$value] = [];
39+
}
40+
41+
self::$values[$value][] = $className;
42+
}
43+
44+
return null;
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
public static function getCollector(string $repository = null)
51+
{
52+
if ($repository) {
53+
return isset(self::$values[$repository]) ? self::$values[$repository] : [];
54+
}
55+
56+
return self::$values;
57+
}
58+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Swoft\Admin\Bean\Parser;
4+
5+
use Swoft\Admin\Bean\Collector\AdminRepositoryListenerCollector;
6+
use Swoft\Bean\Parser\AbstractParser;
7+
8+
/**
9+
* The parser of AdminRepositoryListener
10+
*/
11+
class AdminRepositoryListenerParser extends AbstractParser
12+
{
13+
/**
14+
* @param string $className
15+
* @param mixed $objectAnnotation
16+
* @param string $propertyName
17+
* @param string $methodName
18+
* @param null $propertyValue
19+
* @return null
20+
*/
21+
public function parser(
22+
string $className,
23+
$objectAnnotation = null,
24+
string $propertyName = '',
25+
string $methodName = '',
26+
$propertyValue = null
27+
)
28+
{
29+
AdminRepositoryListenerCollector::collect($className, $objectAnnotation, $propertyName, $methodName, $propertyValue);
30+
31+
return [$className, Scope::SINGLETON, ''];
32+
}
33+
}

src/Bean/Parser/AdminRepositoryParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Swoft\Admin\Bean\Collector\AdminRepositoryCollector;
66
use Swoft\Bean\Parser\AbstractParser;
7+
use Swoft\Bean\Annotation\Scope;
78

89
/**
910
* The parser of Repository
@@ -27,6 +28,7 @@ public function parser(
2728
)
2829
{
2930
AdminRepositoryCollector::collect($className, $objectAnnotation, $propertyName, $methodName, $propertyValue);
30-
return null;
31+
32+
return [$className, Scope::SINGLETON, ''];
3133
}
3234
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Swoft\Admin\Bean\Wrapper;
4+
5+
use Swoft\Bean\Annotation\Inject;
6+
use Swoft\Bean\Annotation\Value;
7+
use Swoft\Bean\Wrapper\AbstractWrapper;
8+
use Swoft\Admin\Bean\Annotation\AdminRepositoryListener;
9+
use Swoft\Rpc\Client\Bean\Annotation\Reference;
10+
11+
class AdminRepositoryListenerWrapper extends AbstractWrapper
12+
{
13+
/**
14+
* 类注解
15+
*
16+
* @var array
17+
*/
18+
protected $classAnnotations = [
19+
AdminRepositoryListener::class,
20+
];
21+
22+
/**
23+
* 属性注解
24+
*
25+
* @var array
26+
*/
27+
protected $propertyAnnotations = [
28+
Inject::class,
29+
Value::class,
30+
Reference::class,
31+
];
32+
33+
/**
34+
* 方法注解
35+
*
36+
* @var array
37+
*/
38+
protected $methodAnnotations = [
39+
];
40+
41+
/**
42+
* 是否解析类注解
43+
*
44+
* @param array $annotations
45+
* @return bool
46+
*/
47+
public function isParseClassAnnotations(array $annotations): bool
48+
{
49+
return isset($annotations[AdminRepositoryListener::class]);
50+
}
51+
52+
/**
53+
* 是否解析属性注解
54+
*
55+
* @param array $annotations
56+
* @return bool
57+
*/
58+
public function isParsePropertyAnnotations(array $annotations): bool
59+
{
60+
return isset($annotations[Inject::class])
61+
|| isset($annotations[Value::class])
62+
|| isset($annotations[Reference::class]);
63+
}
64+
65+
/**
66+
* 是否解析方法注解
67+
*
68+
* @param array $annotations
69+
* @return bool
70+
*/
71+
public function isParseMethodAnnotations(array $annotations): bool
72+
{
73+
return false;
74+
}
75+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Swoft\Admin\Repository;
4+
5+
class InvalidRepositoryListenerException extends \Exception
6+
{
7+
}

src/Repository/Repository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function repository()
3333
* @param string $controllerClass
3434
* @param RepositoryInterface $repository
3535
*/
36-
public static function registerRepository(string $controllerClass, RepositoryInterface $repository)
36+
public static function registerRepository(string $controllerClass, string $repository)
3737
{
3838
static::$repositories[$controllerClass] = new RepositoryProxy($repository);
3939
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Swoft\Admin\Repository;
4+
5+
use Swoft\Admin\Form;
6+
7+
interface RepositoryEventInterface
8+
{
9+
/**
10+
* @param Form $form
11+
*/
12+
public function beforeInsert(Form $form);
13+
14+
/**
15+
* @param Form $form
16+
* @param mixed $result 新增数据返回结果
17+
*/
18+
public function afterInsert(Form $form, $result);
19+
20+
/**
21+
* @param Form $form
22+
*/
23+
public function beforeUpdate(Form $form);
24+
25+
/**
26+
* @param Form $form
27+
* @param mixed $result 编辑数据返回结果
28+
*/
29+
public function afterUpdate(Form $form, $result);
30+
31+
/**
32+
* @param Form $form
33+
*/
34+
public function beforeDelete(Form $form);
35+
36+
/**
37+
* @param Form $form
38+
* @param $result
39+
*/
40+
public function afterDelete(Form $form, $result);
41+
42+
}

0 commit comments

Comments
 (0)