1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the StfalconApiBundle. |
4
|
|
|
* |
5
|
|
|
* (c) Stfalcon LLC <stfalcon.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace StfalconStudio\ApiBundle\Service\AnnotationProcessor; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\Annotations\Reader; |
16
|
|
|
use StfalconStudio\ApiBundle\Annotation\DTO; |
17
|
|
|
use StfalconStudio\ApiBundle\Annotation\DtoAnnotationInterface; |
18
|
|
|
use StfalconStudio\ApiBundle\Exception\RuntimeException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DtoAnnotationProcessor. |
22
|
|
|
*/ |
23
|
|
|
class DtoAnnotationProcessor |
24
|
|
|
{ |
25
|
|
|
/** @var array */ |
26
|
|
|
private $cachedClasses = []; |
27
|
|
|
|
28
|
|
|
/** @var Reader */ |
29
|
|
|
private $annotationReader; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Reader $annotationReader |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Reader $annotationReader) |
35
|
|
|
{ |
36
|
|
|
$this->annotationReader = $annotationReader; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $className |
41
|
|
|
* |
42
|
|
|
* @throws RuntimeException |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function processAnnotationForClass(string $className): string |
47
|
|
|
{ |
48
|
|
|
/** @var class-string<object> $className */ |
|
|
|
|
49
|
|
|
if (\array_key_exists($className, $this->cachedClasses)) { |
50
|
|
|
return $this->cachedClasses[$className]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$classAnnotation = $this->annotationReader->getClassAnnotation(new \ReflectionClass($className), DTO::class); |
54
|
|
|
if (!$classAnnotation instanceof DtoAnnotationInterface) { |
55
|
|
|
throw new RuntimeException(\sprintf('Missing DTO annotation for class %s', $className)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->cachedClasses[$className] = $classAnnotation->getClass(); |
59
|
|
|
|
60
|
|
|
return $classAnnotation->getClass(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.