annotate vendor/doctrine/common/lib/Doctrine/Common/Persistence/PersistentObject.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 7a779792577d
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@0 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@0 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@0 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@0 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@0 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@0 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@0 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@0 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 14 *
Chris@0 15 * This software consists of voluntary contributions made by many individuals
Chris@0 16 * and is licensed under the MIT license. For more information, see
Chris@0 17 * <http://www.doctrine-project.org>.
Chris@0 18 */
Chris@0 19
Chris@0 20 namespace Doctrine\Common\Persistence;
Chris@0 21
Chris@0 22 use Doctrine\Common\Collections\ArrayCollection;
Chris@0 23 use Doctrine\Common\Collections\Collection;
Chris@0 24 use Doctrine\Common\Persistence\Mapping\ClassMetadata;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * PersistentObject base class that implements getter/setter methods for all mapped fields and associations
Chris@0 28 * by overriding __call.
Chris@0 29 *
Chris@0 30 * This class is a forward compatible implementation of the PersistentObject trait.
Chris@0 31 *
Chris@0 32 * Limitations:
Chris@0 33 *
Chris@0 34 * 1. All persistent objects have to be associated with a single ObjectManager, multiple
Chris@0 35 * ObjectManagers are not supported. You can set the ObjectManager with `PersistentObject#setObjectManager()`.
Chris@0 36 * 2. Setters and getters only work if a ClassMetadata instance was injected into the PersistentObject.
Chris@0 37 * This is either done on `postLoad` of an object or by accessing the global object manager.
Chris@0 38 * 3. There are no hooks for setters/getters. Just implement the method yourself instead of relying on __call().
Chris@0 39 * 4. Slower than handcoded implementations: An average of 7 method calls per access to a field and 11 for an association.
Chris@0 40 * 5. Only the inverse side associations get autoset on the owning side as well. Setting objects on the owning side
Chris@0 41 * will not set the inverse side associations.
Chris@0 42 *
Chris@0 43 * @example
Chris@0 44 *
Chris@0 45 * PersistentObject::setObjectManager($em);
Chris@0 46 *
Chris@0 47 * class Foo extends PersistentObject
Chris@0 48 * {
Chris@0 49 * private $id;
Chris@0 50 * }
Chris@0 51 *
Chris@0 52 * $foo = new Foo();
Chris@0 53 * $foo->getId(); // method exists through __call
Chris@0 54 *
Chris@0 55 * @author Benjamin Eberlei <kontakt@beberlei.de>
Chris@0 56 */
Chris@0 57 abstract class PersistentObject implements ObjectManagerAware
Chris@0 58 {
Chris@0 59 /**
Chris@0 60 * @var ObjectManager|null
Chris@0 61 */
Chris@0 62 private static $objectManager = null;
Chris@0 63
Chris@0 64 /**
Chris@0 65 * @var ClassMetadata|null
Chris@0 66 */
Chris@0 67 private $cm = null;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Sets the object manager responsible for all persistent object base classes.
Chris@0 71 *
Chris@0 72 * @param ObjectManager|null $objectManager
Chris@0 73 *
Chris@0 74 * @return void
Chris@0 75 */
Chris@0 76 static public function setObjectManager(ObjectManager $objectManager = null)
Chris@0 77 {
Chris@0 78 self::$objectManager = $objectManager;
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * @return ObjectManager|null
Chris@0 83 */
Chris@0 84 static public function getObjectManager()
Chris@0 85 {
Chris@0 86 return self::$objectManager;
Chris@0 87 }
Chris@0 88
Chris@0 89 /**
Chris@0 90 * Injects the Doctrine Object Manager.
Chris@0 91 *
Chris@0 92 * @param ObjectManager $objectManager
Chris@0 93 * @param ClassMetadata $classMetadata
Chris@0 94 *
Chris@0 95 * @return void
Chris@0 96 *
Chris@0 97 * @throws \RuntimeException
Chris@0 98 */
Chris@0 99 public function injectObjectManager(ObjectManager $objectManager, ClassMetadata $classMetadata)
Chris@0 100 {
Chris@0 101 if ($objectManager !== self::$objectManager) {
Chris@0 102 throw new \RuntimeException("Trying to use PersistentObject with different ObjectManager instances. " .
Chris@0 103 "Was PersistentObject::setObjectManager() called?");
Chris@0 104 }
Chris@0 105
Chris@0 106 $this->cm = $classMetadata;
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * Sets a persistent fields value.
Chris@0 111 *
Chris@0 112 * @param string $field
Chris@0 113 * @param array $args
Chris@0 114 *
Chris@0 115 * @return void
Chris@0 116 *
Chris@0 117 * @throws \BadMethodCallException When no persistent field exists by that name.
Chris@0 118 * @throws \InvalidArgumentException When the wrong target object type is passed to an association.
Chris@0 119 */
Chris@0 120 private function set($field, $args)
Chris@0 121 {
Chris@0 122 if ($this->cm->hasField($field) && !$this->cm->isIdentifier($field)) {
Chris@0 123 $this->$field = $args[0];
Chris@0 124 } else if ($this->cm->hasAssociation($field) && $this->cm->isSingleValuedAssociation($field)) {
Chris@0 125 $targetClass = $this->cm->getAssociationTargetClass($field);
Chris@0 126 if (!($args[0] instanceof $targetClass) && $args[0] !== null) {
Chris@0 127 throw new \InvalidArgumentException("Expected persistent object of type '".$targetClass."'");
Chris@0 128 }
Chris@0 129 $this->$field = $args[0];
Chris@0 130 $this->completeOwningSide($field, $targetClass, $args[0]);
Chris@0 131 } else {
Chris@0 132 throw new \BadMethodCallException("no field with name '".$field."' exists on '".$this->cm->getName()."'");
Chris@0 133 }
Chris@0 134 }
Chris@0 135
Chris@0 136 /**
Chris@0 137 * Gets a persistent field value.
Chris@0 138 *
Chris@0 139 * @param string $field
Chris@0 140 *
Chris@0 141 * @return mixed
Chris@0 142 *
Chris@0 143 * @throws \BadMethodCallException When no persistent field exists by that name.
Chris@0 144 */
Chris@0 145 private function get($field)
Chris@0 146 {
Chris@0 147 if ( $this->cm->hasField($field) || $this->cm->hasAssociation($field) ) {
Chris@0 148 return $this->$field;
Chris@0 149 }
Chris@12 150
Chris@12 151 throw new \BadMethodCallException("no field with name '".$field."' exists on '".$this->cm->getName()."'");
Chris@0 152 }
Chris@0 153
Chris@0 154 /**
Chris@0 155 * If this is an inverse side association, completes the owning side.
Chris@0 156 *
Chris@0 157 * @param string $field
Chris@0 158 * @param ClassMetadata $targetClass
Chris@0 159 * @param object $targetObject
Chris@0 160 *
Chris@0 161 * @return void
Chris@0 162 */
Chris@0 163 private function completeOwningSide($field, $targetClass, $targetObject)
Chris@0 164 {
Chris@0 165 // add this object on the owning side as well, for obvious infinite recursion
Chris@0 166 // reasons this is only done when called on the inverse side.
Chris@0 167 if ($this->cm->isAssociationInverseSide($field)) {
Chris@0 168 $mappedByField = $this->cm->getAssociationMappedByTargetField($field);
Chris@0 169 $targetMetadata = self::$objectManager->getClassMetadata($targetClass);
Chris@0 170
Chris@0 171 $setter = ($targetMetadata->isCollectionValuedAssociation($mappedByField) ? "add" : "set").$mappedByField;
Chris@0 172 $targetObject->$setter($this);
Chris@0 173 }
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * Adds an object to a collection.
Chris@0 178 *
Chris@0 179 * @param string $field
Chris@0 180 * @param array $args
Chris@0 181 *
Chris@0 182 * @return void
Chris@0 183 *
Chris@0 184 * @throws \BadMethodCallException
Chris@0 185 * @throws \InvalidArgumentException
Chris@0 186 */
Chris@0 187 private function add($field, $args)
Chris@0 188 {
Chris@0 189 if ($this->cm->hasAssociation($field) && $this->cm->isCollectionValuedAssociation($field)) {
Chris@0 190 $targetClass = $this->cm->getAssociationTargetClass($field);
Chris@0 191 if (!($args[0] instanceof $targetClass)) {
Chris@0 192 throw new \InvalidArgumentException("Expected persistent object of type '".$targetClass."'");
Chris@0 193 }
Chris@0 194 if (!($this->$field instanceof Collection)) {
Chris@0 195 $this->$field = new ArrayCollection($this->$field ?: []);
Chris@0 196 }
Chris@0 197 $this->$field->add($args[0]);
Chris@0 198 $this->completeOwningSide($field, $targetClass, $args[0]);
Chris@0 199 } else {
Chris@0 200 throw new \BadMethodCallException("There is no method add".$field."() on ".$this->cm->getName());
Chris@0 201 }
Chris@0 202 }
Chris@0 203
Chris@0 204 /**
Chris@0 205 * Initializes Doctrine Metadata for this class.
Chris@0 206 *
Chris@0 207 * @return void
Chris@0 208 *
Chris@0 209 * @throws \RuntimeException
Chris@0 210 */
Chris@0 211 private function initializeDoctrine()
Chris@0 212 {
Chris@0 213 if ($this->cm !== null) {
Chris@0 214 return;
Chris@0 215 }
Chris@0 216
Chris@0 217 if (!self::$objectManager) {
Chris@0 218 throw new \RuntimeException("No runtime object manager set. Call PersistentObject#setObjectManager().");
Chris@0 219 }
Chris@0 220
Chris@0 221 $this->cm = self::$objectManager->getClassMetadata(get_class($this));
Chris@0 222 }
Chris@0 223
Chris@0 224 /**
Chris@0 225 * Magic methods.
Chris@0 226 *
Chris@0 227 * @param string $method
Chris@0 228 * @param array $args
Chris@0 229 *
Chris@0 230 * @return mixed
Chris@0 231 *
Chris@0 232 * @throws \BadMethodCallException
Chris@0 233 */
Chris@0 234 public function __call($method, $args)
Chris@0 235 {
Chris@12 236 $this->initializeDoctrine();
Chris@12 237
Chris@0 238 $command = substr($method, 0, 3);
Chris@0 239 $field = lcfirst(substr($method, 3));
Chris@0 240 if ($command == "set") {
Chris@0 241 $this->set($field, $args);
Chris@0 242 } else if ($command == "get") {
Chris@0 243 return $this->get($field);
Chris@0 244 } else if ($command == "add") {
Chris@0 245 $this->add($field, $args);
Chris@0 246 } else {
Chris@0 247 throw new \BadMethodCallException("There is no method ".$method." on ".$this->cm->getName());
Chris@0 248 }
Chris@0 249 }
Chris@0 250 }