|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Component\Meta; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @since Class available since Release 2.1.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class MapBuilder implements DataMapper |
|
13
|
|
|
{ |
|
14
|
|
|
private $manager; |
|
15
|
|
|
|
|
16
|
|
|
private $map = []; |
|
17
|
|
|
|
|
18
|
|
|
private $logger; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
EntityManager $manager, |
|
22
|
|
|
LoggerInterface $logger = null |
|
23
|
|
|
) { |
|
24
|
|
|
$this->manager = $manager; |
|
25
|
|
|
$this->logger = $logger; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getMap() : array |
|
29
|
|
|
{ |
|
30
|
|
|
if (!$this->map) { |
|
|
|
|
|
|
31
|
|
|
$this->rebuildRelationMap(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $this->map; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function forceCache(array $map) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->map = $map; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** @codeCoverageIgnore */ |
|
43
|
|
|
public static function relations( |
|
44
|
|
|
ClassMetadata $classMetadata, |
|
45
|
|
|
LoggerInterface $logger = null |
|
|
|
|
|
|
46
|
|
|
) { |
|
47
|
|
|
$encoded = json_encode($classMetadata); |
|
48
|
|
|
$decoded = json_decode($encoded, true); |
|
49
|
|
|
$relations = $decoded['associationMappings']; |
|
50
|
|
|
|
|
51
|
|
|
$relMap = []; |
|
52
|
|
|
|
|
53
|
|
|
foreach ($relations as $name => $meta) { |
|
54
|
|
|
$relMap[$name] = $meta['targetEntity']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $relMap; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function rebuildRelationMap() |
|
61
|
|
|
{ |
|
62
|
|
|
$allMetadata = $this->manager |
|
63
|
|
|
->getMetadataFactory() |
|
64
|
|
|
->getAllMetadata(); |
|
65
|
|
|
|
|
66
|
|
|
foreach ($allMetadata as $singleEntityMetadata) { |
|
67
|
|
|
// @codeCoverageIgnoreStart |
|
68
|
|
|
$this->map[$singleEntityMetadata->getName()]['relations'] = self::relations( |
|
69
|
|
|
$singleEntityMetadata, |
|
70
|
|
|
$this->logger |
|
71
|
|
|
); |
|
72
|
|
|
// @codeCoverageIgnoreEnd |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.