1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Majora\Framework\Loader\Bridge\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
6
|
|
|
use Majora\Framework\Loader\LazyLoaderInterface; |
7
|
|
|
use Majora\Framework\Model\EntityCollection; |
8
|
|
|
use Majora\Framework\Model\LazyPropertiesInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Proxy class to dispatch Doctrine events to proper loaders only |
12
|
|
|
* (Doctrine notify all listeners at each loaded entities, so we have to proxy it here, and lazy load dependencies). |
13
|
|
|
*/ |
14
|
|
|
class DoctrineEventProxy |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EntityCollection |
18
|
|
|
*/ |
19
|
|
|
protected $loaders; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EntityCollection |
23
|
|
|
*/ |
24
|
|
|
protected $unsupportedClasses; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Construct. |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->loaders = new EntityCollection(); |
32
|
|
|
$this->unsupportedClasses = new EntityCollection(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Register a loader for given entity class. |
37
|
|
|
* |
38
|
|
|
* @param string $entityClass |
39
|
|
|
* @param string $loader |
40
|
|
|
*/ |
41
|
|
|
public function registerDoctrineLazyLoader($entityClass, LazyLoaderInterface $loader) |
42
|
|
|
{ |
43
|
|
|
if (!is_a($entityClass, LazyPropertiesInterface::class, true)) { |
44
|
|
|
throw new \InvalidArgumentException(sprintf( |
45
|
|
|
'Class %s has to implement %s to be able to lazy load her properties.', |
46
|
|
|
$entityClass, |
47
|
|
|
LazyPropertiesInterface::class |
48
|
|
|
)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->loaders->set($entityClass, $loader); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retrieve a lazy loader for given class (or not). |
56
|
|
|
* |
57
|
|
|
* @param object $entityClass |
|
|
|
|
58
|
|
|
* |
59
|
|
|
* @return |
60
|
|
|
*/ |
61
|
|
|
private function getLoader($entity) |
62
|
|
|
{ |
63
|
|
|
$entityClass = get_class($entity); |
64
|
|
|
|
65
|
|
|
if ($this->unsupportedClasses->containsKey($entityClass)) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
if ($this->loaders->containsKey($entityClass)) { |
69
|
|
|
return $this->loaders->get($entityClass); |
70
|
|
|
} |
71
|
|
|
foreach ($this->loaders as $handledClass => $loader) { |
72
|
|
|
if (is_a($entity, $handledClass)) { |
73
|
|
|
$this->loaders->set($entityClass, $loader); |
74
|
|
|
|
75
|
|
|
return $loader; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->unsupportedClasses->set($entityClass, false); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* "postLoad" Doctrine event handler, notify loaders if define for given event related entity. |
84
|
|
|
* |
85
|
|
|
* @param LifecycleEventArgs $event |
86
|
|
|
*/ |
87
|
|
|
public function postLoad(LifecycleEventArgs $event) |
88
|
|
|
{ |
89
|
|
|
if (!$loader = $this->getLoader($entity = $event->getEntity())) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// global handler (deprecated) |
|
|
|
|
94
|
|
View Code Duplication |
if (isset($proxies[$loaderClass = static::class])) { |
|
|
|
|
95
|
|
|
@trigger_error( |
|
|
|
|
96
|
|
|
sprintf('Global loader delegate is deprecated and will be removed in 2.0. Make "%s" loader invokable instead, entity to handle is given at first parameter.', |
97
|
|
|
static::class |
98
|
|
|
), |
99
|
|
|
E_USER_DEPRECATED |
100
|
|
|
); |
101
|
|
|
$proxies[$loaderClass]($entity); |
102
|
|
|
unset($proxies[$loaderClass]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// define delegates into object |
106
|
|
|
$entity->registerLoaders( |
107
|
|
|
$loader->getLoadingDelegates() |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
// global delegate if able to |
111
|
|
|
if (is_callable($loader)) { |
112
|
|
|
$loader($entity); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.