Beispiel #1
0
 /** @inheritDoc */
 public function setReferenceRepository(ReferenceRepository $referenceRepository)
 {
     $this->em->getEventManager()->removeEventListener($this->listener->getSubscribedEvents(), $this->listener);
     $this->referenceRepository = $referenceRepository;
     $this->listener = new ORMReferenceListener($this->referenceRepository);
     $this->em->getEventManager()->addEventSubscriber($this->listener);
 }
Beispiel #2
0
 /**
  * Boot the extensions
  */
 public function boot()
 {
     foreach ($this->registry->getManagers() as $em) {
         $this->em = $em;
         $this->evm = $this->em->getEventManager();
         $this->metadata = $this->em->getConfiguration();
         $this->reader = $this->driverChain->getReader();
         foreach ($this->extensions as $extension) {
             $this->bootExtension($extension);
         }
     }
 }
Beispiel #3
0
 /**
  * Boot the extensions
  */
 public function boot()
 {
     foreach ($this->registry->getManagers() as $em) {
         $this->em = $em;
         $this->evm = $this->em->getEventManager();
         $this->metadata = $this->em->getConfiguration();
         $this->reader = $this->driverChain->getReader();
         $hash = spl_object_hash($em);
         if (!isset($this->subscribedExtensions[$hash])) {
             $this->subscribedExtensions[$hash] = [];
         }
         foreach ($this->extensions as $extension) {
             $this->bootExtension($extension);
         }
     }
 }
 /**
  * Boot the extensions
  */
 public function boot()
 {
     foreach ($this->registry->getManagers() as $em) {
         $this->em = $em;
         $this->chain = new MappingDriverChain();
         $this->evm = $this->em->getEventManager();
         $this->metadata = $this->em->getConfiguration();
         $this->reader = method_exists($this->metadata->getMetadataDriverImpl(), 'getReader') ? $this->metadata->getMetadataDriverImpl()->getReader() : false;
         if ($this->gedmo['enabled']) {
             $this->bootGedmoExtensions($this->gedmo['namespace'], $this->gedmo['all']);
         }
         foreach ($this->extensions as $extenion) {
             $this->bootExtension($extenion);
         }
     }
 }
 /**
  * Initiates a row-by-row hydration.
  *
  * @param object $stmt
  * @param object $resultSetMapping
  * @param array  $hints
  *
  * @return IterableResult
  */
 public function iterate($stmt, $resultSetMapping, array $hints = array())
 {
     $this->_stmt = $stmt;
     $this->_rsm = $resultSetMapping;
     $this->_hints = $hints;
     $evm = $this->_em->getEventManager();
     $evm->addEventListener(array(Events::onClear), $this);
     $this->prepare();
     return new IterableResult($this);
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     $this->connection = new MysqlPersistentConnection();
     $params = $this->connection->getConnectionParams();
     $config = Setup::createAnnotationMetadataConfiguration([__DIR__ . '/Entity'], true, null, null, false);
     $this->em = EntityManager::create($params, $config);
     $event_manager = $this->em->getEventManager();
     // create tables in the database
     $metadata = $this->em->getMetadataFactory()->getAllMetadata();
     $schema_tool = new SchemaTool($this->em);
     $schema_tool->createSchema($metadata);
     // default doctrine annotation reader
     $annotation_reader = new AnnotationReader();
     // setup required providers
     $mutation_metadata_provider = new EntityMutationMetadataProvider($annotation_reader);
     $annotation_metadata_provider = new EntityAnnotationMetadataProvider($annotation_reader);
     // pre flush event listener that uses the @Tracked annotation
     $entity_changed_listener = new EntityChangedListener($annotation_metadata_provider, $mutation_metadata_provider);
     $event_manager->addEventListener('preFlush', $entity_changed_listener);
     $event_manager->addEventListener('prePersist', $entity_changed_listener);
     $event_manager->addEventListener('entityChanged', $this);
     $this->events = [];
 }
Beispiel #7
0
 /**
  * Removes the RevisionListener from the EventManager
  */
 public function disableRevisionListener()
 {
     foreach ($this->em->getEventManager()->getListeners() as $event => $listeners) {
         foreach ($listeners as $hash => $listener) {
             if ($listener instanceof RevisionListener) {
                 $this->revisionListener = $listener;
                 break 2;
             }
         }
     }
     if ($this->revisionListener) {
         $this->revisionListener->setActive(false);
     }
 }
Beispiel #8
0
 /**
  * Removes the RevisionListener from the EventManager
  */
 public function killRevisionListener()
 {
     $listenerInst = false;
     foreach ($this->em->getEventManager()->getListeners() as $event => $listeners) {
         foreach ($listeners as $hash => $listener) {
             if ($listener instanceof RevisionListener) {
                 $listenerInst = $listener;
                 break 2;
             }
         }
     }
     if ($listenerInst) {
         $this->em->getEventManager()->removeEventListener(array(Events::onFlush, Events::postPersist, Events::postUpdate, Events::postFlush, SoftDeleteableListener::PRE_SOFT_DELETE, SoftDeleteableListener::POST_SOFT_DELETE), $listenerInst);
     }
 }
Beispiel #9
0
 /**
  * Initializes a new UnitOfWork instance, bound to the given EntityManager.
  *
  * @param EntityManagerInterface $em
  */
 public function __construct(EntityManagerInterface $em)
 {
     $this->em = $em;
     $this->evm = $em->getEventManager();
     $this->listenersInvoker = new ListenersInvoker($em);
     $this->hasCache = $em->getConfiguration()->isSecondLevelCacheEnabled();
     $this->identifierFlattener = new IdentifierFlattener($this, $em->getMetadataFactory());
     $this->hydrationCompleteHandler = new HydrationCompleteHandler($this->listenersInvoker, $em);
     $this->reflectionPropertiesGetter = new ReflectionPropertiesGetter(new RuntimeReflectionService());
 }
 /**
  * {@inheritDoc}
  */
 protected function initialize()
 {
     $this->driver = $this->em->getConfiguration()->getMetadataDriverImpl();
     $this->evm = $this->em->getEventManager();
     $this->initialized = true;
 }
 /**
  * Creates a Schema instance from a given set of metadata classes.
  *
  * @param array $classes
  *
  * @return Schema
  *
  * @throws \Doctrine\ORM\ORMException
  */
 public function getSchemaFromMetadata(array $classes)
 {
     // Reminder for processed classes, used for hierarchies
     $processedClasses = array();
     $eventManager = $this->em->getEventManager();
     $schemaManager = $this->em->getConnection()->getSchemaManager();
     $metadataSchemaConfig = $schemaManager->createSchemaConfig();
     $metadataSchemaConfig->setExplicitForeignKeyIndexes(false);
     $schema = new Schema(array(), array(), $metadataSchemaConfig);
     $addedFks = array();
     $blacklistedFks = array();
     foreach ($classes as $class) {
         /** @var \Doctrine\ORM\Mapping\ClassMetadata $class */
         if ($this->processingNotRequired($class, $processedClasses)) {
             continue;
         }
         $table = $schema->createTable($this->quoteStrategy->getTableName($class, $this->platform));
         if ($class->isInheritanceTypeSingleTable()) {
             $this->gatherColumns($class, $table);
             $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
             // Add the discriminator column
             $this->addDiscriminatorColumnDefinition($class, $table);
             // Aggregate all the information from all classes in the hierarchy
             foreach ($class->parentClasses as $parentClassName) {
                 // Parent class information is already contained in this class
                 $processedClasses[$parentClassName] = true;
             }
             foreach ($class->subClasses as $subClassName) {
                 $subClass = $this->em->getClassMetadata($subClassName);
                 $this->gatherColumns($subClass, $table);
                 $this->gatherRelationsSql($subClass, $table, $schema, $addedFks, $blacklistedFks);
                 $processedClasses[$subClassName] = true;
             }
         } elseif ($class->isInheritanceTypeJoined()) {
             // Add all non-inherited fields as columns
             $pkColumns = array();
             foreach ($class->fieldMappings as $fieldName => $mapping) {
                 if (!isset($mapping['inherited'])) {
                     $columnName = $this->quoteStrategy->getColumnName($mapping['fieldName'], $class, $this->platform);
                     $this->gatherColumn($class, $mapping, $table);
                     if ($class->isIdentifier($fieldName)) {
                         $pkColumns[] = $columnName;
                     }
                 }
             }
             $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
             // Add the discriminator column only to the root table
             if ($class->name == $class->rootEntityName) {
                 $this->addDiscriminatorColumnDefinition($class, $table);
             } else {
                 // Add an ID FK column to child tables
                 $inheritedKeyColumns = array();
                 foreach ($class->identifier as $identifierField) {
                     $idMapping = $class->fieldMappings[$identifierField];
                     if (isset($idMapping['inherited'])) {
                         $this->gatherColumn($class, $idMapping, $table);
                         $columnName = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform);
                         // TODO: This seems rather hackish, can we optimize it?
                         $table->getColumn($columnName)->setAutoincrement(false);
                         $pkColumns[] = $columnName;
                         $inheritedKeyColumns[] = $columnName;
                     }
                 }
                 if (!empty($inheritedKeyColumns)) {
                     // Add a FK constraint on the ID column
                     $table->addForeignKeyConstraint($this->quoteStrategy->getTableName($this->em->getClassMetadata($class->rootEntityName), $this->platform), $inheritedKeyColumns, $inheritedKeyColumns, array('onDelete' => 'CASCADE'));
                 }
             }
             $table->setPrimaryKey($pkColumns);
         } elseif ($class->isInheritanceTypeTablePerClass()) {
             throw ORMException::notSupported();
         } else {
             $this->gatherColumns($class, $table);
             $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
         }
         $pkColumns = array();
         foreach ($class->identifier as $identifierField) {
             if (isset($class->fieldMappings[$identifierField])) {
                 $pkColumns[] = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform);
             } elseif (isset($class->associationMappings[$identifierField])) {
                 /* @var $assoc \Doctrine\ORM\Mapping\OneToOne */
                 $assoc = $class->associationMappings[$identifierField];
                 foreach ($assoc['joinColumns'] as $joinColumn) {
                     $pkColumns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
                 }
             }
         }
         if (!$table->hasIndex('primary')) {
             $table->setPrimaryKey($pkColumns);
         }
         if (isset($class->table['indexes'])) {
             foreach ($class->table['indexes'] as $indexName => $indexData) {
                 if (!isset($indexData['flags'])) {
                     $indexData['flags'] = array();
                 }
                 $table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, (array) $indexData['flags'], isset($indexData['options']) ? $indexData['options'] : array());
             }
         }
         if (isset($class->table['uniqueConstraints'])) {
             foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
                 $table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, isset($indexData['options']) ? $indexData['options'] : array());
             }
         }
         if (isset($class->table['options'])) {
             foreach ($class->table['options'] as $key => $val) {
                 $table->addOption($key, $val);
             }
         }
         $processedClasses[$class->name] = true;
         if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
             $seqDef = $class->sequenceGeneratorDefinition;
             $quotedName = $this->quoteStrategy->getSequenceName($seqDef, $class, $this->platform);
             if (!$schema->hasSequence($quotedName)) {
                 $schema->createSequence($quotedName, $seqDef['allocationSize'], $seqDef['initialValue']);
             }
         }
         if ($eventManager->hasListeners(ToolEvents::postGenerateSchemaTable)) {
             $eventManager->dispatchEvent(ToolEvents::postGenerateSchemaTable, new GenerateSchemaTableEventArgs($class, $schema, $table));
         }
     }
     if (!$this->platform->supportsSchemas() && !$this->platform->canEmulateSchemas()) {
         $schema->visit(new RemoveNamespacedAssets());
     }
     if ($eventManager->hasListeners(ToolEvents::postGenerateSchema)) {
         $eventManager->dispatchEvent(ToolEvents::postGenerateSchema, new GenerateSchemaEventArgs($this->em, $schema));
     }
     return $schema;
 }
 /**
  * @param array                  $settings
  * @param EntityManagerInterface $manager
  */
 protected function registerSubscribers(array $settings = [], EntityManagerInterface $manager)
 {
     if (isset($settings['events']['subscribers'])) {
         foreach ($settings['events']['subscribers'] as $subscriber) {
             try {
                 $resolvedSubscriber = $this->container->make($subscriber);
             } catch (ReflectionException $e) {
                 throw new InvalidArgumentException("Listener {$subscriber} could not be resolved: {$e->getMessage()}");
             }
             $manager->getEventManager()->addEventSubscriber($resolvedSubscriber);
         }
     }
 }
Beispiel #13
0
 /**
  * Initializes a new ListenersInvoker instance.
  *
  * @param EntityManagerInterface $em
  */
 public function __construct(EntityManagerInterface $em)
 {
     $this->eventManager = $em->getEventManager();
     $this->resolver = $em->getConfiguration()->getEntityListenerResolver();
 }
 /**
  * {@inheritdoc}
  */
 public function getEventManager()
 {
     return $this->wrapped->getEventManager();
 }
Beispiel #15
0
 /**
  * @param array                  $settings
  * @param EntityManagerInterface $manager
  */
 protected function registerSubscribers($settings = [], EntityManagerInterface $manager)
 {
     if (isset($settings['events']['subscribers'])) {
         foreach ($settings['events']['subscribers'] as $subscriber) {
             if (!class_exists($subscriber, false)) {
                 throw new InvalidArgumentException("Subscriber {$subscriber} does not exist");
             }
             $manager->getEventManager()->addEventSubscriber(new $subscriber());
         }
     }
 }