1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Goodwix Doctrine JSON ODM. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Goodwix\DoctrineJsonOdm\Bridge\Symfony\DependencyInjection; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Annotations\Reader; |
12
|
|
|
use Goodwix\DoctrineJsonOdm\Annotation\ODM; |
13
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
|
16
|
|
|
class ODMTypeCompilerPass implements CompilerPassInterface |
17
|
|
|
{ |
18
|
|
|
public const ODM_AUTO_REGISTRAR = 'goodwix.doctrine_json_odm.odm_auto_registrar'; |
19
|
|
|
public const ODM_PATHS = 'goodwix.doctrine_json_odm.odm_paths'; |
20
|
|
|
|
21
|
|
|
/** @var Reader */ |
22
|
|
|
private $reader; |
23
|
|
|
|
24
|
1 |
|
public function process(ContainerBuilder $container): void |
25
|
|
|
{ |
26
|
1 |
|
if ($container->has(self::ODM_AUTO_REGISTRAR) && $container->hasParameter(self::ODM_PATHS)) { |
27
|
1 |
|
$this->reader = $container->get('annotation_reader'); |
28
|
|
|
|
29
|
1 |
|
$paths = $container->getParameter(self::ODM_PATHS); |
30
|
1 |
|
$entityClassList = $this->collectEntityClassListFromPaths($paths); |
31
|
|
|
|
32
|
1 |
|
$definition = $container->getDefinition(self::ODM_AUTO_REGISTRAR); |
33
|
1 |
|
$definition->replaceArgument(2, $entityClassList); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
private function collectEntityClassListFromPaths(array $paths): array |
38
|
|
|
{ |
39
|
1 |
|
$classes = $this->getReflectionClassesFromDirectories($paths); |
40
|
|
|
|
41
|
1 |
|
$entityClassList = []; |
42
|
|
|
|
43
|
|
|
/** @var \ReflectionClass $class */ |
44
|
1 |
|
foreach ($classes as $class) { |
45
|
1 |
|
$odmAnnotation = $this->reader->getClassAnnotation($class, ODM::class); |
46
|
|
|
|
47
|
1 |
|
if (null !== $odmAnnotation) { |
48
|
1 |
|
$entityClassList[] = $class->getName(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $entityClassList; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
private function getReflectionClassesFromDirectories(array $directories): \Iterator |
56
|
|
|
{ |
57
|
1 |
|
foreach ($directories as $path) { |
58
|
1 |
|
$iterator = new \RegexIterator( |
59
|
1 |
|
new \RecursiveIteratorIterator( |
60
|
1 |
|
new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS), |
61
|
|
|
\RecursiveIteratorIterator::LEAVES_ONLY |
62
|
|
|
), |
63
|
|
|
'/^.+\.php$/i', |
64
|
|
|
\RecursiveRegexIterator::GET_MATCH |
65
|
|
|
); |
66
|
|
|
|
67
|
1 |
|
foreach ($iterator as $file) { |
68
|
1 |
|
$sourceFile = $file[0]; |
69
|
|
|
|
70
|
1 |
|
if (!preg_match('(^phar:)i', $sourceFile)) { |
71
|
1 |
|
$sourceFile = realpath($sourceFile); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
require_once $sourceFile; |
75
|
|
|
|
76
|
1 |
|
$includedFiles[$sourceFile] = true; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$declared = array_merge(get_declared_classes(), get_declared_interfaces()); |
81
|
|
|
|
82
|
1 |
|
foreach ($declared as $className) { |
83
|
1 |
|
$reflectionClass = new \ReflectionClass($className); |
84
|
1 |
|
$sourceFile = $reflectionClass->getFileName(); |
85
|
1 |
|
if (isset($includedFiles[$sourceFile])) { |
86
|
1 |
|
yield $className => $reflectionClass; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|