1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Majora\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Majora\Framework\Loader\Bridge\Doctrine\AbstractDoctrineLoader; |
6
|
|
|
use Majora\Framework\Loader\LoaderInterface; |
7
|
|
|
use Majora\Framework\Model\CollectionableInterface; |
8
|
|
|
use Majora\Framework\Model\LazyPropertiesInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Compiler pass to register loaders setUp. |
15
|
|
|
*/ |
16
|
|
|
class LoaderCompilerPass implements CompilerPassInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
* |
21
|
|
|
* Processes "majora.loader" tags |
22
|
|
|
*/ |
23
|
2 |
|
public function process(ContainerBuilder $container) |
24
|
|
|
{ |
25
|
2 |
|
$loaderTags = $container->findTaggedServiceIds('majora.loader'); |
26
|
|
|
|
27
|
2 |
|
foreach ($loaderTags as $loaderId => $tags) { |
28
|
|
|
$loaderDefinition = $container->getDefinition($loaderId); |
29
|
|
|
$loaderReflection = new \ReflectionClass($loaderDefinition->getClass()); |
30
|
|
|
|
31
|
|
|
foreach ($tags as $attributes) { |
32
|
|
|
$method = $loaderReflection->implementsInterface(LoaderInterface::class) ? |
33
|
|
|
'configureMetadata' : ($loaderReflection->hasMethod('setUp') ? |
34
|
|
|
'setUp' : '' |
35
|
|
|
) |
36
|
|
|
; |
37
|
|
|
if (isset($attributes['entityClass']) || isset($attributes['entityCollection'])) { |
38
|
|
|
@trigger_error('"entityClass" and "entityCollection" attributes for tag "majora.loader" are deprecated and will be removed in 2.0. Please "entity" and "collection" instead.', E_USER_DEPRECATED); |
39
|
|
|
} |
40
|
|
|
$entityClass = isset($attributes['entity']) ? $attributes['entity'] : $attributes['entityClass']; |
41
|
|
|
$collectionClass = isset($attributes['collection']) ? $attributes['collection'] : $attributes['entityCollection']; |
42
|
|
|
$entityReflection = new \ReflectionClass($entityClass); |
43
|
|
|
|
44
|
|
|
// configureMetadata() call configuration |
45
|
|
|
if ($method) { |
46
|
|
|
if (!$entityReflection->implementsInterface(CollectionableInterface::class)) { |
47
|
|
|
throw new \InvalidArgumentException(sprintf( |
48
|
|
|
'Cannot support "%s" class into "%s" : managed items have to be %s.', |
49
|
|
|
$entityClass, |
50
|
|
|
$loaderDefinition->getClass(), |
51
|
|
|
CollectionableInterface::class |
52
|
|
|
)); |
53
|
|
|
} |
54
|
|
|
$arguments = array( |
55
|
|
|
$entityClass, |
56
|
|
|
array_map( |
57
|
|
|
function ($property) { return $property->getName(); }, |
58
|
|
|
$entityReflection->getProperties() |
59
|
|
|
), |
60
|
|
|
$collectionClass, |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$loaderDefinition->addMethodCall($method, $arguments); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Doctrine case |
67
|
|
|
if ($loaderReflection->isSubclassOf(AbstractDoctrineLoader::class)) { |
68
|
|
|
|
69
|
|
|
// "repository" attribute key only supported for doctrine loaders |
70
|
|
|
// Repository is injected through mutator to avoid circular references |
71
|
|
|
// with Doctrine events and connection |
72
|
|
|
if (isset($attributes['repository'])) { |
73
|
|
|
$loaderDefinition->addMethodCall( |
74
|
|
|
'setEntityRepository', |
75
|
|
|
array(new Reference($attributes['repository'])) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// for Doctrine, loaders cannot self enable objects lazy loading |
80
|
|
|
// due to general event trigger into all listener for each entites |
81
|
|
|
// so we have to check class / attribute and register service into event proxy |
82
|
|
|
if ($container->hasDefinition('majora.doctrine.event_proxy') && !empty($attributes['lazy'])) { |
83
|
|
|
if (!$entityReflection->implementsInterface(LazyPropertiesInterface::class)) { |
84
|
|
|
throw new \InvalidArgumentException(sprintf( |
85
|
|
|
'Class %s has to implement %s to be able to lazy load her properties.', |
86
|
|
|
$entityClass, |
87
|
|
|
LazyPropertiesInterface::class |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
$container->getDefinition('majora.doctrine.event_proxy') |
91
|
|
|
->addMethodCall('registerDoctrineLazyLoader', array( |
92
|
|
|
$entityClass, |
93
|
|
|
new Reference($loaderId), |
94
|
|
|
)) |
95
|
|
|
; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
1 |
|
} |
100
|
2 |
|
} |
101
|
|
|
} |
102
|
|
|
|