diff --git a/UPGRADE.md b/UPGRADE.md index 9b32247..6025cc0 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -4,6 +4,7 @@ ### Global Changes - Recommended folder structure by symfony adopted +- [IMPROVEMENT] Use Generator to index data which will increase processing speed significant *** diff --git a/src/Service/Builder/AssetListBuilder.php b/src/Service/Builder/AssetListBuilder.php index 6efd0bd..b5a6a6b 100644 --- a/src/Service/Builder/AssetListBuilder.php +++ b/src/Service/Builder/AssetListBuilder.php @@ -17,11 +17,15 @@ public function __construct( ) { } - public function buildByList(array $options): array + public function buildByList(array $options): \Generator { $list = $this->getList($options); - return $list->getAssets(); + foreach ($list->loadIdList() as $id) { + if ($asset = Asset::getById($id)) { + yield $asset; + } + } } public function buildByIdList(int $id, array $options): ?ElementInterface @@ -33,10 +37,6 @@ public function buildByIdList(int $id, array $options): ?ElementInterface $assets = $list->getAssets(); - if (!is_array($assets)) { - return null; - } - if (count($assets) === 0) { return null; } diff --git a/src/Service/Builder/DataBuilderInterface.php b/src/Service/Builder/DataBuilderInterface.php index 09877a2..06d1105 100644 --- a/src/Service/Builder/DataBuilderInterface.php +++ b/src/Service/Builder/DataBuilderInterface.php @@ -6,7 +6,7 @@ interface DataBuilderInterface { - public function buildByList(array $options): array; + public function buildByList(array $options): \Generator; public function buildByIdList(int $id, array $options): ?ElementInterface; diff --git a/src/Service/Builder/DocumentListBuilder.php b/src/Service/Builder/DocumentListBuilder.php index 96e3fcf..7c23683 100644 --- a/src/Service/Builder/DocumentListBuilder.php +++ b/src/Service/Builder/DocumentListBuilder.php @@ -18,11 +18,15 @@ public function __construct( ) { } - public function buildByList(array $options): array + public function buildByList(array $options): \Generator { $list = $this->getList($options); - return $list->getDocuments(); + foreach ($list->loadIdList() as $id) { + if ($doc = Document::getById($id)) { + yield $doc; + } + } } public function buildByIdList(int $id, array $options): ?ElementInterface @@ -34,10 +38,6 @@ public function buildByIdList(int $id, array $options): ?ElementInterface $documents = $list->getDocuments(); - if (!is_array($documents)) { - return null; - } - if (count($documents) === 0) { return null; } diff --git a/src/Service/Builder/ObjectListBuilder.php b/src/Service/Builder/ObjectListBuilder.php index 47095ab..2a7af7c 100644 --- a/src/Service/Builder/ObjectListBuilder.php +++ b/src/Service/Builder/ObjectListBuilder.php @@ -17,11 +17,15 @@ public function __construct( ) { } - public function buildByList(array $options): array + public function buildByList(array $options): \Generator { $list = $this->getList($options); - return $list->getObjects(); + foreach ($list->loadIdList() as $id) { + if ($object = DataObject::getById($id)) { + yield $object; + } + } } public function buildByIdList(int $id, array $options): ?ElementInterface @@ -33,10 +37,6 @@ public function buildByIdList(int $id, array $options): ?ElementInterface $objects = $list->getObjects(); - if (!is_array($objects)) { - return null; - } - if (count($objects) === 0) { return null; } diff --git a/src/Service/DataProviderService.php b/src/Service/DataProviderService.php index 2bd30af..4df33b4 100644 --- a/src/Service/DataProviderService.php +++ b/src/Service/DataProviderService.php @@ -136,7 +136,9 @@ protected function fetchByTypeAndId(string $type, string $providerBehaviour, int $element = $builder->buildById($id); - $this->dispatchData([$element], $providerBehaviour, $resourceMeta); + if ($element instanceof ElementInterface) { + $this->dispatchElement($element, $providerBehaviour, $resourceMeta); + } } protected function log(string $level, string $message): void @@ -144,16 +146,21 @@ protected function log(string $level, string $message): void $this->logger->log($level, $message, DsTrinityDataBundle::PROVIDER_NAME, $this->contextName); } - protected function dispatchData(array $elements, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null): void + protected function dispatchData(\Generator $elements, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null): void { foreach ($elements as $element) { - $newDataEvent = new NewDataEvent($this->contextDispatchType, $this->contextName, $element, $providerBehaviour, $resourceMeta); - $this->eventDispatcher->dispatch($newDataEvent, DynamicSearchEvents::NEW_DATA_AVAILABLE); - - $this->dispatchProcessControlSignal(); + $this->dispatchElement($element, $providerBehaviour, $resourceMeta); } } + protected function dispatchElement(ElementInterface $element, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null): void + { + $newDataEvent = new NewDataEvent($this->contextDispatchType, $this->contextName, $element, $providerBehaviour, $resourceMeta); + $this->eventDispatcher->dispatch($newDataEvent, DynamicSearchEvents::NEW_DATA_AVAILABLE); + + $this->dispatchProcessControlSignal(); + } + protected function addSignalListener(): void { if (php_sapi_name() !== 'cli') {