1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Locastic\Loggastic\Metadata\LoggableContext\Factory; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
6
|
|
|
use Locastic\Loggastic\Annotation\Loggable; |
7
|
|
|
use Locastic\Loggastic\Metadata\LoggableContext\LoggableContextCollection; |
8
|
|
|
use Locastic\Loggastic\Util\RecursiveClassIterator; |
9
|
|
|
|
10
|
|
|
final class AnnotationLoggableContextCollectionFactory implements LoggableContextCollectionFactoryInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string[] $loggablePaths |
14
|
|
|
*/ |
15
|
|
|
public function __construct(private readonly LoggableContextCollectionFactoryInterface $decorated, private readonly Reader $reader, private readonly array $loggablePaths) |
16
|
|
|
{ |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function create(): LoggableContextCollection |
20
|
|
|
{ |
21
|
|
|
if (count($this->loggablePaths) === 0) { |
22
|
|
|
return new LoggableContextCollection([]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$classes = []; |
26
|
|
|
|
27
|
|
|
if ($this->decorated) { |
28
|
|
|
foreach ($this->decorated->create() as $loggableClass => $config) { |
29
|
|
|
$classes[$loggableClass] = $config; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
foreach (RecursiveClassIterator::getReflectionClasses($this->loggablePaths) as $className => $reflectionClass) { |
34
|
|
|
if ($loggable = $this->getLoggableAttribute($reflectionClass)) { |
35
|
|
|
$classes[$className] = ['groups' => $loggable->getGroups()]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (null !== $this->reader && $loggable = $this->reader->getClassAnnotation($reflectionClass, Loggable::class)) { |
39
|
|
|
$classes[$className] = $loggable->getGroups(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return new LoggableContextCollection($classes); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function getLoggableAttribute(\ReflectionClass $reflectionClass): ?Loggable |
47
|
|
|
{ |
48
|
|
|
foreach ($reflectionClass->getAttributes() as $attribute) { |
49
|
|
|
if (is_a($attribute->getName(), Loggable::class, true)) { |
50
|
|
|
return $attribute->newInstance(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|