1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 18.08.16 at 11:25 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\container\resolver; |
7
|
|
|
|
8
|
|
|
use samsonframework\container\metadata\ClassMetadata; |
9
|
|
|
use samsonframework\container\metadata\PropertyMetadata; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class property resolving trait. |
13
|
|
|
* |
14
|
|
|
* @author Vitaly Iegorov <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
trait PropertyResolverTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Generic class property resolver. |
20
|
|
|
* |
21
|
|
|
* @param \ReflectionProperty $property |
22
|
|
|
* @param ClassMetadata $classMetadata |
23
|
|
|
* |
24
|
|
|
* @return PropertyMetadata Resolved property metadata |
25
|
|
|
*/ |
26
|
|
|
protected function resolvePropertyMetadata(\ReflectionProperty $property, ClassMetadata $classMetadata) : PropertyMetadata |
27
|
|
|
{ |
28
|
|
|
// Create method metadata instance |
29
|
|
|
$propertyMetadata = $classMetadata->propertiesMetadata[$property->getName()] ?? new PropertyMetadata($classMetadata); |
30
|
|
|
$propertyMetadata->name = $property->getName(); |
31
|
|
|
$propertyMetadata->modifiers = $property->getModifiers(); |
32
|
|
|
$propertyMetadata->isPublic = $property->isPublic(); |
33
|
|
|
$propertyMetadata->typeHint = $this->getCommentTypeHint( |
34
|
|
|
is_string($property->getDocComment()) ? $property->getDocComment() : ''); |
35
|
|
|
|
36
|
|
|
// Store property metadata to class metadata |
37
|
|
|
return $classMetadata->propertiesMetadata[$propertyMetadata->name] = $propertyMetadata; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Parse property comments and return type hint if present. |
42
|
|
|
* |
43
|
|
|
* @param string $comments Property comments |
44
|
|
|
* |
45
|
|
|
* @return string Property type hint if present |
46
|
|
|
*/ |
47
|
|
|
protected function getCommentTypeHint(string $comments) : string |
48
|
|
|
{ |
49
|
|
|
// Parse property type hint if present |
50
|
|
|
if (preg_match('/@var\s+(?<class>[^\s]+)/', $comments, $matches)) { |
51
|
|
|
return $matches[1]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|