Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
changes per stof review; added backward compatibility to pre-Doctrine…
… 2.2.0
  • Loading branch information
robocoder committed May 8, 2012
1 parent 814ee3b commit 3a8a303
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php
Expand Up @@ -19,30 +19,49 @@

namespace Doctrine\Common\DataFixtures;

use Doctrine\Common\DataFixtures\ReferenceRepository;

/**
* 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().
*
* @since Doctrine ORM 2.2
*
* @author Anthon Pang <anthonp@nationalfibre.net>
*/
class ProxyReferenceRepository extends ReferenceRepository
{
/**
* Get real class name of a reference that could be a proxy
*
* @param mixed $reference Reference
*
* @return string
*/
protected function getRealClass($reference)
{
$className = get_class($reference);

if ( ! $reference instanceof \Doctrine\ORM\Proxy\Proxy) {
return $className;
}

if (\Doctrine\ORM\Version::compare('2.2.0') >= 0) {
return \Doctrine\Common\Util\ClassUtils::getRealClass($className);
}

return substr($className, 0, -5);
}

/**
* Serialize reference repository
*
* @return string
*/
public function serialize() {
public function serialize()
{
$simpleReferences = array();

foreach ($this->getReferences() as $name => $reference) {
$className = str_replace('Proxies\\__CG__\\', '', get_class($reference));
$className = $this->getRealClass($reference);

$simpleReferences[$name] = array($className, $reference->getId());
}
Expand All @@ -60,7 +79,8 @@ public function serialize() {
*
* @param string $serializedData Serialized data
*/
public function unserialize($serializedData) {
public function unserialize($serializedData)
{
$repositoryData = json_decode($serializedData, true);
$references = $repositoryData['references'];

Expand Down Expand Up @@ -88,7 +108,11 @@ public function unserialize($serializedData) {
*/
public function load($baseCacheName)
{
$serializedData = file_get_contents($baseCacheName . '.ser');
$filename = $baseCacheName . '.ser';

if ( ! file_exists($filename) || ($serializedData = file_get_contents($filename)) === false) {
return;
}

$this->unserialize($serializedData);
}
Expand All @@ -104,4 +128,4 @@ public function save($baseCacheName)

file_put_contents($baseCacheName . '.ser', $serializedData);
}
}
}

0 comments on commit 3a8a303

Please sign in to comment.