Skip to content
This repository
Browse code

Initial functionality for storing the fixture references

  • Loading branch information...
commit 18dbe2a767c100aa79ad36b7e600ea13c5929c07 1 parent 374d037
Gediminas Morkevicius l3pp4rd authored
90 lib/Doctrine/Common/DataFixtures/AbstractFixture.php
... ... @@ -0,0 +1,90 @@
  1 +<?php
  2 +/*
  3 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14 + *
  15 + * This software consists of voluntary contributions made by many individuals
  16 + * and is licensed under the LGPL. For more information, see
  17 + * <http://www.doctrine-project.org>.
  18 + */
  19 +
  20 +namespace Doctrine\Common\DataFixtures;
  21 +
  22 +use Doctrine\Common\DataFixtures\ReferenceRepository;
  23 +
  24 +/**
  25 + * Abstract Fixture class helps to manage references
  26 + * between fixture classes in order to set relations
  27 + * among other fixtures
  28 + *
  29 + * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  30 + */
  31 +abstract class AbstractFixture implements SharedFixtureInterface
  32 +{
  33 + /**
  34 + * Fixture reference repository
  35 + *
  36 + * @var ReferenceRepository
  37 + */
  38 + private $referenceRepository;
  39 +
  40 + /**
  41 + * {@inheritdoc}
  42 + */
  43 + public function setReferenceRepository(ReferenceRepository $referenceRepository)
  44 + {
  45 + $this->referenceRepository = $referenceRepository;
  46 + }
  47 +
  48 + /**
  49 + * Set the reference entry identified by $name
  50 + * and referenced to managed $object. If $name
  51 + * already is set, it overrides it
  52 + *
  53 + * @param string $name
  54 + * @param object $object - managed object
  55 + * @see Doctrine\Common\DataFixtures\ReferenceRepository::setReference
  56 + * @return void
  57 + */
  58 + public function setReference($name, $object)
  59 + {
  60 + $this->referenceRepository->setReference($name, $object);
  61 + }
  62 +
  63 + /**
  64 + * Set the reference entry identified by $name
  65 + * and referenced to managed $object. If $name
  66 + * already is set, it overrides it
  67 + *
  68 + * @param string $name
  69 + * @param object $object - managed object
  70 + * @see Doctrine\Common\DataFixtures\ReferenceRepository::addReference
  71 + * @return void
  72 + */
  73 + public function addReference($name, $object)
  74 + {
  75 + $this->referenceRepository->addReference($name, $object);
  76 + }
  77 +
  78 + /**
  79 + * Loads an object using stored reference
  80 + * named by $name
  81 + *
  82 + * @param string $name
  83 + * @see Doctrine\Common\DataFixtures\ReferenceRepository::getReference
  84 + * @return object
  85 + */
  86 + public function getReference($name)
  87 + {
  88 + return $this->referenceRepository->getReference($name);
  89 + }
  90 +}
22 lib/Doctrine/Common/DataFixtures/Executor/AbstractExecutor.php
@@ -2,7 +2,9 @@
2 2
3 3 namespace Doctrine\Common\DataFixtures\Executor;
4 4
  5 +use Doctrine\Common\DataFixtures\SharedFixtureInterface;
5 6 use Doctrine\Common\DataFixtures\FixtureInterface;
  7 +use Doctrine\Common\DataFixtures\ReferenceRepository;
6 8
7 9 /**
8 10 * Abstract fixture executor.
@@ -18,6 +20,22 @@
18 20 protected $logger;
19 21
20 22 /**
  23 + * Fixture reference repository
  24 + * @var ReferenceRepository
  25 + */
  26 + protected $referenceRepository;
  27 +
  28 + /**
  29 + * Loads an instance of reference repository
  30 + *
  31 + * @param object $manager
  32 + */
  33 + public function __construct($manager)
  34 + {
  35 + $this->referenceRepository = new ReferenceRepository($manager);
  36 + }
  37 +
  38 + /**
21 39 * Sets the Purger instance to use for this exector instance.
22 40 *
23 41 * @param Purger $purger
@@ -59,6 +77,10 @@ public function load($manager, FixtureInterface $fixture)
59 77 if ($this->logger) {
60 78 $this->log('loading ' . get_class($fixture));
61 79 }
  80 + // additionaly pass the instance of reference repository to shared fixtures
  81 + if ($fixture instanceof SharedFixtureInterface) {
  82 + $fixture->setReferenceRepository($this->referenceRepository);
  83 + }
62 84 $fixture->load($manager);
63 85 $manager->clear();
64 86 }
1  lib/Doctrine/Common/DataFixtures/Executor/MongoDBExecutor.php
@@ -41,6 +41,7 @@ public function __construct(DocumentManager $dm, MongoDBPurger $purger = null)
41 41 $this->purger = $purger;
42 42 $this->purger->setDocumentManager($dm);
43 43 }
  44 + parent::__construct($dm);
44 45 }
45 46
46 47 /** @inheritDoc */
1  lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php
@@ -41,6 +41,7 @@ public function __construct(EntityManager $em, ORMPurger $purger = null)
41 41 $this->purger = $purger;
42 42 $this->purger->setEntityManager($em);
43 43 }
  44 + parent::__construct($em);
44 45 }
45 46
46 47 /** @inheritDoc */
33 lib/Doctrine/Common/DataFixtures/OrderedFixtureInterface.php
... ... @@ -0,0 +1,33 @@
  1 +<?php
  2 +/*
  3 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14 + *
  15 + * This software consists of voluntary contributions made by many individuals
  16 + * and is licensed under the LGPL. For more information, see
  17 + * <http://www.doctrine-project.org>.
  18 + */
  19 +
  20 +namespace Doctrine\Common\DataFixtures;
  21 +
  22 +/**
  23 + * Shared Fixture interface needs to be implemented
  24 + * by fixtures, which needs some references to be shared
  25 + * among other fixture classes in order to maintain
  26 + * relation mapping
  27 + *
  28 + * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  29 + */
  30 +interface OrderedFixtureInterface
  31 +{
  32 + public function getOrder();
  33 +}
147 lib/Doctrine/Common/DataFixtures/ReferenceRepository.php
... ... @@ -0,0 +1,147 @@
  1 +<?php
  2 +/*
  3 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14 + *
  15 + * This software consists of voluntary contributions made by many individuals
  16 + * and is licensed under the LGPL. For more information, see
  17 + * <http://www.doctrine-project.org>.
  18 + */
  19 +
  20 +namespace Doctrine\Common\DataFixtures;
  21 +
  22 +/**
  23 + * ReferenceRepository class manages references for
  24 + * fixtures in order to easily support the relations
  25 + * between fixtures
  26 + *
  27 + * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  28 + */
  29 +class ReferenceRepository
  30 +{
  31 + /**
  32 + * List of named references to the fixture objects
  33 + * gathered during loads of fixtures
  34 + *
  35 + * @var array
  36 + */
  37 + private $references = array();
  38 +
  39 + /**
  40 + * Currently used object manager
  41 + *
  42 + * @var object - object manager
  43 + */
  44 + private $manager;
  45 +
  46 + /**
  47 + * Initialize the ReferenceRepository
  48 + *
  49 + * @param object $manager
  50 + */
  51 + public function __construct($manager)
  52 + {
  53 + $this->manager = $manager;
  54 + }
  55 +
  56 + /**
  57 + * Set the reference entry identified by $name
  58 + * and referenced to managed $object. If $name
  59 + * already is set, it overrides it
  60 + *
  61 + * Notice: in case if identifier is generated after
  62 + * the record is inserted, be sure tu use this method
  63 + * after $object is flushed
  64 + *
  65 + * @param string $name
  66 + * @param object $object - managed object
  67 + * @throws LogicException - if object is not mapped or
  68 + * does not have identifier yet
  69 + * @return void
  70 + */
  71 + public function setReference($name, $object)
  72 + {
  73 + $objectClass = get_class($object);
  74 + if (!$this->manager->getMetadataFactory()->hasMetadataFor(get_class($object))) {
  75 + throw new \LogicException("While setting a reference object {$objectClass} - must be mapped or persisted before");
  76 + }
  77 + $meta = $this->manager->getClassMetadata(get_class($object));
  78 +
  79 + $identifier = array();
  80 + foreach ((array)$meta->identifier as $field) {
  81 + $id = $meta->getReflectionProperty($field)->getValue($object);
  82 + if (!$id) {
  83 + throw new \LogicException("Object: {$objectClass} - must be flushed first in order to have generated id");
  84 + }
  85 + $identifier[$field] = $id;
  86 + }
  87 + if (!is_array($meta->identifier)) {
  88 + $identifier = reset($identifier);
  89 + }
  90 + $this->references[$name] = array(
  91 + 'class' => $meta->name,
  92 + 'identifier' => $identifier
  93 + );
  94 + }
  95 +
  96 + /**
  97 + * Set the reference entry identified by $name
  98 + * and referenced to managed $object. $name must
  99 + * not be set yet
  100 + *
  101 + * Notice: in case if identifier is generated after
  102 + * the record is inserted, be sure tu use this method
  103 + * after $object is flushed
  104 + *
  105 + * @param string $name
  106 + * @param object $object - managed object
  107 + * @throws BadMethodCallException - if repository already has
  108 + * a reference by $name
  109 + * @return void
  110 + */
  111 + public function addReference($name, $object)
  112 + {
  113 + if (isset($this->references[$name])) {
  114 + throw new \BadMethodCallException("Reference to: ({$name}) already exists, use method setReference in order to override it");
  115 + }
  116 + $this->setReference($name, $object);
  117 + }
  118 +
  119 + /**
  120 + * Loads an object using stored reference
  121 + * named by $name
  122 + *
  123 + * @param string $name
  124 + * @return object
  125 + */
  126 + public function getReference($name)
  127 + {
  128 + $object = null;
  129 + if (isset($this->references[$name])) {
  130 + $object = $this->manager->getReference(
  131 + $this->references[$name]['class'],
  132 + $this->references[$name]['identifier']
  133 + );
  134 + }
  135 + return $object;
  136 + }
  137 +
  138 + /**
  139 + * Get all stored references
  140 + *
  141 + * @return array
  142 + */
  143 + public function getReferences()
  144 + {
  145 + return $this->references;
  146 + }
  147 +}
35 lib/Doctrine/Common/DataFixtures/SharedFixtureInterface.php
... ... @@ -0,0 +1,35 @@
  1 +<?php
  2 +/*
  3 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14 + *
  15 + * This software consists of voluntary contributions made by many individuals
  16 + * and is licensed under the LGPL. For more information, see
  17 + * <http://www.doctrine-project.org>.
  18 + */
  19 +
  20 +namespace Doctrine\Common\DataFixtures;
  21 +
  22 +use Doctrine\Common\DataFixtures\ReferenceRepository;
  23 +
  24 +/**
  25 + * Shared Fixture interface needs to be implemented
  26 + * by fixtures, which needs some references to be shared
  27 + * among other fixture classes in order to maintain
  28 + * relation mapping
  29 + *
  30 + * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  31 + */
  32 +interface SharedFixtureInterface extends FixtureInterface
  33 +{
  34 + public function setReferenceRepository(ReferenceRepository $referenceRepository);
  35 +}

0 comments on commit 18dbe2a

Please sign in to comment.
Something went wrong with that request. Please try again.