/** * 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(); } }
/** * @param array $data data * @param ActionManagerInterface $actionManager actionManager * @throws \Exception * @return $this */ public function fromArray(array $data, ActionManagerInterface $actionManager = null) { if (isset($data['criterias']) && isset($data['criterias']['type'])) { $criterias = $data['criterias']; $type = $criterias['type']; if ('operator' == $type) { $method = 'createOperatorFromArray'; } elseif ('expr' == $type) { $method = 'createAsserterFromArray'; } else { throw new \Exception('Invalid array, cannot be unserialized'); } $this->setCriterias($this->factory->{$method}($criterias)); } if (isset($data['page'])) { $this->setPage($data['page']); } if (isset($data['max_per_page'])) { $this->setMaxPerPage($data['max_per_page']); } if (isset($data['sort'])) { list($field, $order) = $data['sort']; $this->orderBy($field, $order); } if (isset($data['subject']) && !empty($data['subject'])) { $subjects = $data['subject']; if (!$actionManager) { throw new \Exception('Please provide the actionManager to retrieve components'); } $components = $actionManager->findComponents($subjects); if (count($components) != count($subjects)) { foreach ($components as $component) { // remove existing components from subjects to keep only new one components unset($subjects[array_search($component->getHash(), $subjects)]); } // create new components foreach ($subjects as $subject) { list($model, $identifier) = explode('#', $subject); $components[] = $actionManager->createComponent($model, unserialize($identifier)); } } foreach ($components as $component) { $this->addSubject($component); } } return $this; }