-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
733 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Iterator; | ||
|
||
use Iterator; | ||
|
||
final class BatchIterator implements Iterator | ||
{ | ||
public const DEFAULT_BATCH_SIZE = 25; | ||
|
||
/** @var \eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter */ | ||
private $adapter; | ||
|
||
/** @var Iterator|null */ | ||
private $innerIterator; | ||
|
||
/** @var int */ | ||
private $batchSize; | ||
|
||
/** @var int */ | ||
private $position; | ||
|
||
public function __construct( | ||
BatchIteratorAdapter $adapter, | ||
int $batchSize = self::DEFAULT_BATCH_SIZE | ||
) { | ||
$this->adapter = $adapter; | ||
$this->batchSize = $batchSize; | ||
$this->position = 0; | ||
} | ||
|
||
public function current() | ||
{ | ||
if (!$this->isInitialized()) { | ||
$this->initialize(); | ||
} | ||
|
||
return $this->innerIterator->current(); | ||
} | ||
|
||
public function next(): void | ||
{ | ||
if (!$this->isInitialized()) { | ||
$this->initialize(); | ||
} | ||
|
||
++$this->position; | ||
$this->innerIterator->next(); | ||
if (!$this->innerIterator->valid() && ($this->position % $this->batchSize) === 0) { | ||
$this->innerIterator = $this->fetch(); | ||
} | ||
} | ||
|
||
public function key(): int | ||
{ | ||
return $this->position; | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
if (!$this->isInitialized()) { | ||
$this->initialize(); | ||
} | ||
|
||
return $this->innerIterator->valid(); | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->initialize(); | ||
} | ||
|
||
public function setBatchSize(int $batchSize): void | ||
{ | ||
$this->batchSize = $batchSize; | ||
} | ||
|
||
private function initialize(): void | ||
{ | ||
$this->position = 0; | ||
$this->innerIterator = $this->fetch(); | ||
} | ||
|
||
private function isInitialized(): bool | ||
{ | ||
return isset($this->innerIterator); | ||
} | ||
|
||
private function fetch(): Iterator | ||
{ | ||
return $this->adapter->fetch($this->position, $this->batchSize); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
eZ/Publish/API/Repository/Iterator/BatchIteratorAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Iterator; | ||
|
||
use Iterator; | ||
|
||
interface BatchIteratorAdapter | ||
{ | ||
public function fetch(int $offset, int $limit): Iterator; | ||
} |
53 changes: 53 additions & 0 deletions
53
eZ/Publish/API/Repository/Iterator/BatchIteratorAdapter/AbstractSearchAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter; | ||
|
||
use eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter; | ||
use eZ\Publish\API\Repository\SearchService; | ||
use eZ\Publish\API\Repository\Values\Content\Query; | ||
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult; | ||
use Iterator; | ||
|
||
abstract class AbstractSearchAdapter implements BatchIteratorAdapter | ||
{ | ||
/** @var \eZ\Publish\API\Repository\SearchService */ | ||
protected $searchService; | ||
|
||
/** @var \eZ\Publish\API\Repository\Values\Content\Query */ | ||
protected $query; | ||
|
||
/** @var string[] */ | ||
protected $languageFilter; | ||
|
||
/** @var bool */ | ||
protected $filterOnUserPermissions; | ||
|
||
public function __construct( | ||
SearchService $searchService, | ||
Query $query, | ||
array $languageFilter = [], | ||
bool $filterOnUserPermissions = true | ||
) { | ||
$this->searchService = $searchService; | ||
$this->query = $query; | ||
$this->languageFilter = $languageFilter; | ||
$this->filterOnUserPermissions = $filterOnUserPermissions; | ||
} | ||
|
||
final public function fetch(int $offset, int $limit): Iterator | ||
{ | ||
$query = clone $this->query; | ||
$query->offset = $offset; | ||
$query->limit = $limit; | ||
|
||
return $this->executeSearch($query)->getIterator(); | ||
} | ||
|
||
abstract protected function executeSearch(Query $query): SearchResult; | ||
} |
41 changes: 41 additions & 0 deletions
41
eZ/Publish/API/Repository/Iterator/BatchIteratorAdapter/ContentFilteringAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter; | ||
|
||
use eZ\Publish\API\Repository\ContentService; | ||
use eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter; | ||
use eZ\Publish\API\Repository\Values\Filter\Filter; | ||
use Iterator; | ||
|
||
final class ContentFilteringAdapter implements BatchIteratorAdapter | ||
{ | ||
/** @var \eZ\Publish\API\Repository\ContentService */ | ||
private $contentService; | ||
|
||
/** @var \eZ\Publish\API\Repository\Values\Filter\Filter */ | ||
private $filter; | ||
|
||
/** @var string[]|null */ | ||
private $languages; | ||
|
||
public function __construct(ContentService $contentService, Filter $filter, ?array $languages = null) | ||
{ | ||
$this->contentService = $contentService; | ||
$this->filter = $filter; | ||
$this->languages = $languages; | ||
} | ||
|
||
public function fetch(int $offset, int $limit): Iterator | ||
{ | ||
$filter = clone $this->filter; | ||
$filter->sliceBy($limit, $offset); | ||
|
||
return $this->contentService->find($filter, $this->languages)->getIterator(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
eZ/Publish/API/Repository/Iterator/BatchIteratorAdapter/ContentInfoSearchAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Query; | ||
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult; | ||
|
||
final class ContentInfoSearchAdapter extends AbstractSearchAdapter | ||
{ | ||
protected function executeSearch(Query $query): SearchResult | ||
{ | ||
return $this->searchService->findContentInfo( | ||
$query, | ||
$this->languageFilter, | ||
$this->filterOnUserPermissions | ||
); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
eZ/Publish/API/Repository/Iterator/BatchIteratorAdapter/ContentSearchAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Query; | ||
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult; | ||
|
||
final class ContentSearchAdapter extends AbstractSearchAdapter | ||
{ | ||
protected function executeSearch(Query $query): SearchResult | ||
{ | ||
return $this->searchService->findContent( | ||
$query, | ||
$this->languageFilter, | ||
$this->filterOnUserPermissions | ||
); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
eZ/Publish/API/Repository/Iterator/BatchIteratorAdapter/LocationFilteringAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter; | ||
|
||
use eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter; | ||
use eZ\Publish\API\Repository\LocationService; | ||
use eZ\Publish\API\Repository\Values\Filter\Filter; | ||
use Iterator; | ||
|
||
final class LocationFilteringAdapter implements BatchIteratorAdapter | ||
{ | ||
/** @var \eZ\Publish\API\Repository\LocationService */ | ||
private $locationService; | ||
|
||
/** @var \eZ\Publish\API\Repository\Values\Filter\Filter */ | ||
private $filter; | ||
|
||
/** @var string[]|null */ | ||
private $languages; | ||
|
||
public function __construct(LocationService $locationService, Filter $filter, ?array $languages = null) | ||
{ | ||
$this->locationService = $locationService; | ||
$this->filter = $filter; | ||
$this->languages = $languages; | ||
} | ||
|
||
public function fetch(int $offset, int $limit): Iterator | ||
{ | ||
$filter = clone $this->filter; | ||
$filter->sliceBy($limit, $offset); | ||
|
||
return $this->locationService->find($filter, $this->languages)->getIterator(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
eZ/Publish/API/Repository/Iterator/BatchIteratorAdapter/LocationSearchAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Iterator\BatchIteratorAdapter; | ||
|
||
use eZ\Publish\API\Repository\SearchService; | ||
use eZ\Publish\API\Repository\Values\Content\LocationQuery; | ||
use eZ\Publish\API\Repository\Values\Content\Query; | ||
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult; | ||
|
||
final class LocationSearchAdapter extends AbstractSearchAdapter | ||
{ | ||
public function __construct( | ||
SearchService $searchService, | ||
LocationQuery $query, | ||
array $languageFilter = [], | ||
bool $filterOnUserPermissions = true | ||
) { | ||
parent::__construct($searchService, $query, $languageFilter, $filterOnUserPermissions); | ||
} | ||
|
||
protected function executeSearch(Query $query): SearchResult | ||
{ | ||
return $this->searchService->findLocations( | ||
$query, | ||
$this->languageFilter, | ||
$this->filterOnUserPermissions | ||
); | ||
} | ||
} |
Oops, something went wrong.