ProxyReferenceRepository.php 3.1 KB

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