Ejemplo n.º 1
0
 /**
  * Load unaware entries, instead of having 1 call by entry to fetch component
  * you can add unaware entries. Component will be created or exception
  * will be thrown if it does not exist
  *
  * @throws \Exception
  * @return void
  */
 public function loadUnawareEntries()
 {
     if (!$this->actionManager) {
         return;
     }
     $unawareEntries = array();
     foreach ($this->coll as $context => $entries) {
         foreach ($entries as $entry) {
             if ($entry instanceof EntryUnaware) {
                 $unawareEntries[$entry->getIdent()] = $entry->getIdent();
             }
         }
     }
     if (empty($unawareEntries)) {
         return;
     }
     $components = $this->actionManager->findComponents($unawareEntries);
     $componentsIndexedByIdent = array();
     foreach ($components as $component) {
         $componentsIndexedByIdent[$component->getHash()] = $component;
     }
     unset($components);
     $nbComponentCreated = 0;
     foreach ($this->coll as $context => $entries) {
         foreach ($entries as $entry) {
             if ($entry instanceof EntryUnaware) {
                 $ident = $entry->getIdent();
                 // component fetched from database.
                 if (array_key_exists($ident, $componentsIndexedByIdent)) {
                     $entry->setSubject($componentsIndexedByIdent[$ident]);
                 } else {
                     if ($entry->isStrict()) {
                         throw new \Exception(sprintf('Component with ident "%s" is unknown', $entry->getIdent()));
                     }
                     // third argument ensures component is not flushed directly.
                     $component = $this->actionManager->createComponent($entry->getSubjectModel(), $entry->getSubjectId(), false);
                     $nbComponentCreated++;
                     if ($nbComponentCreated % $this->batchSize == 0) {
                         $this->actionManager->flushComponents();
                     }
                     if (null === $component) {
                         throw new \Exception(sprintf('Component with ident "%s" cannot be created', $entry->getIdent()));
                     }
                     $entry->setSubject($component);
                     $componentsIndexedByIdent[$component->getHash()] = $component;
                 }
             }
         }
     }
     if ($nbComponentCreated > 0) {
         $this->actionManager->flushComponents();
     }
 }