-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathProxyReferenceRepository.php
121 lines (101 loc) · 3.13 KB
/
ProxyReferenceRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Doctrine\Common\DataFixtures;
use Doctrine\Common\Version;
use Doctrine\Common\Util\ClassUtils;
/**
* Proxy reference repository
*
* Allow data fixture references and identities to be persisted when cached data fixtures
* are pre-loaded, for example, by LiipFunctionalTestBundle\Test\WebTestCase loadFixtures().
*
* @author Guilherme Blanco <[email protected]>
* @author Anthon Pang <[email protected]>
*/
class ProxyReferenceRepository extends ReferenceRepository
{
/**
* Get real class name of a reference that could be a proxy
*
* @param string $className Class name of reference object
*
* @return string
*/
protected function getRealClass($className)
{
if (Version::compare('2.2.0') <= 0) {
return ClassUtils::getRealClass($className);
}
if (substr($className, -5) === 'Proxy') {
return substr($className, 0, -5);
}
return $className;
}
/**
* Serialize reference repository
*
* @return string
*/
public function serialize()
{
$unitOfWork = $this->getManager()->getUnitOfWork();
$simpleReferences = [];
foreach ($this->getReferences() as $name => $reference) {
$className = $this->getRealClass(get_class($reference));
$simpleReferences[$name] = [$className, $this->getIdentifier($reference, $unitOfWork)];
}
$serializedData = json_encode([
'references' => $simpleReferences,
'identities' => $this->getIdentities(),
]);
return $serializedData;
}
/**
* Unserialize reference repository
*
* @param string $serializedData Serialized data
*/
public function unserialize($serializedData)
{
$repositoryData = json_decode($serializedData, true);
$references = $repositoryData['references'];
foreach ($references as $name => $proxyReference) {
$this->setReference(
$name,
$this->getManager()->getReference(
$proxyReference[0], // entity class name
$proxyReference[1] // identifiers
)
);
}
$identities = $repositoryData['identities'];
foreach ($identities as $name => $identity) {
$this->setReferenceIdentity($name, $identity);
}
}
/**
* Load data fixture reference repository
*
* @param string $baseCacheName Base cache name
*
* @return boolean
*/
public function load($baseCacheName)
{
$filename = $baseCacheName . '.ser';
if ( ! file_exists($filename) || ($serializedData = file_get_contents($filename)) === false) {
return false;
}
$this->unserialize($serializedData);
return true;
}
/**
* Save data fixture reference repository
*
* @param string $baseCacheName Base cache name
*/
public function save($baseCacheName)
{
$serializedData = $this->serialize();
file_put_contents($baseCacheName . '.ser', $serializedData);
}
}