-
Notifications
You must be signed in to change notification settings - Fork 107
/
InjectParser.php
55 lines (49 loc) 路 1.46 KB
/
InjectParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php declare(strict_types=1);
namespace Swoft\Bean\Annotation\Parser;
use PhpDocReader\AnnotationException;
use PhpDocReader\PhpDocReader;
use ReflectionException;
use ReflectionProperty;
use Swoft\Annotation\Annotation\Mapping\AnnotationParser;
use Swoft\Annotation\Annotation\Parser\Parser;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Bean\Exception\BeanException;
/**
* Class InjectParser
*
* @AnnotationParser(Inject::class)
* @since 2.0
*/
class InjectParser extends Parser
{
/**
* Parse annotation
*
* @param int $type
* @param Inject $annotationObject
*
* @return array
* @throws BeanException
* @throws AnnotationException
* @throws ReflectionException
*/
public function parse(int $type, $annotationObject): array
{
// Only to parse property annotation with `@Inject`
if ($type != self::TYPE_PROPERTY) {
return [];
}
$inject = $annotationObject->getName();
if (!empty($inject)) {
return [$inject, true];
}
// Parse php document
$phpReader = new PhpDocReader();
$reflectProperty = new ReflectionProperty($this->className, $this->propertyName);
$docInject = $phpReader->getPropertyClass($reflectProperty);
if (empty($docInject)) {
throw new BeanException('`@Inject` must be define inejct value or `@var type` ');
}
return [$docInject, true];
}
}