|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Neo4jBundle\DependencyInjection\Compiler; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Neo4jBundle\Exception\NoEntityDefinitionFoundException; |
|
7
|
|
|
use Innmind\Filesystem\{ |
|
8
|
|
|
Adapter\FilesystemAdapter, |
|
9
|
|
|
Exception\FileNotFoundException, |
|
10
|
|
|
DirectoryInterface |
|
11
|
|
|
}; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\{ |
|
13
|
|
|
Compiler\CompilerPassInterface, |
|
14
|
|
|
ContainerBuilder |
|
15
|
|
|
}; |
|
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
17
|
|
|
|
|
18
|
|
|
class InjectEntityDefinitionsPass implements CompilerPassInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
2 |
|
public function process(ContainerBuilder $container) |
|
24
|
|
|
{ |
|
25
|
2 |
|
$bundles = $container->getParameter('kernel.bundles'); |
|
26
|
2 |
|
$configs = []; |
|
27
|
|
|
|
|
28
|
2 |
|
foreach ($bundles as $bundle => $class) { |
|
29
|
|
|
try { |
|
30
|
2 |
|
$configs[] = $this->computeConfig($class); |
|
31
|
2 |
|
} catch (NoEntityDefinitionFoundException $e) { |
|
32
|
|
|
//pass |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$container |
|
37
|
2 |
|
->getDefinition('innmind_neo4j.metadata_builder') |
|
38
|
2 |
|
->addMethodCall( |
|
39
|
2 |
|
'inject', |
|
40
|
2 |
|
[$configs] |
|
41
|
|
|
); |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Load the entity definitions for the given bundle |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $class Bundle class FQCN |
|
48
|
|
|
* |
|
49
|
|
|
* @throws NoEntityDefinitionFoundException |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
2 |
|
private function computeConfig(string $class): array |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
2 |
|
$refl = new \ReflectionClass($class); |
|
57
|
2 |
|
$dir = (new FilesystemAdapter(dirname($refl->getFileName()))) |
|
|
|
|
|
|
58
|
2 |
|
->get('Resources') |
|
59
|
2 |
|
->get('config'); |
|
60
|
|
|
|
|
61
|
2 |
|
if ($dir->has('neo4j.yml')) { |
|
62
|
2 |
|
return Yaml::parse((string) $dir->get('neo4j.yml')->content()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
$dir = $dir->get('neo4j'); |
|
66
|
2 |
|
$config = []; |
|
67
|
|
|
|
|
68
|
2 |
|
foreach ($dir as $file) { |
|
69
|
2 |
|
if ($file instanceof DirectoryInterface) { |
|
70
|
2 |
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
$config = array_merge( |
|
74
|
|
|
$config, |
|
75
|
2 |
|
Yaml::parse((string) $file->content()) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
return $config; |
|
80
|
2 |
|
} catch (FileNotFoundException $e) { |
|
81
|
2 |
|
throw new NoEntityDefinitionFoundException; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: