-
Notifications
You must be signed in to change notification settings - Fork 19
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
26 changed files
with
1,869 additions
and
38 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,142 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentIndexer; | ||
|
||
use Massive\Bundle\SearchBundle\Search\QueryHit; | ||
use Massive\Bundle\SearchBundle\Search\SearchManager; | ||
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Exception\ContentNotFoundException; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface; | ||
|
||
class ContentIndexer implements ContentIndexerInterface | ||
{ | ||
/** | ||
* @var SearchManager | ||
*/ | ||
private $searchManager; | ||
|
||
/** | ||
* @var ContentResolverInterface | ||
*/ | ||
private $contentResolver; | ||
|
||
/** | ||
* @param SearchManager $searchManager | ||
*/ | ||
public function __construct(SearchManagerInterface $searchManager, ContentResolverInterface $contentResolver) | ||
{ | ||
$this->searchManager = $searchManager; | ||
$this->contentResolver = $contentResolver; | ||
} | ||
|
||
public function index(ContentRichEntityInterface $contentRichEntity, array $dimensionAttributes): DimensionContentInterface | ||
{ | ||
$dimensionContent = $this->loadDimensionContent($contentRichEntity, $dimensionAttributes); | ||
|
||
$this->indexDimensionContent($dimensionContent); | ||
|
||
return $dimensionContent; | ||
} | ||
|
||
public function indexDimensionContent(DimensionContentInterface $dimensionContent): void | ||
{ | ||
if (!$dimensionContent->isMerged()) { | ||
return; | ||
} | ||
|
||
$this->searchManager->index($dimensionContent); | ||
} | ||
|
||
public function deindex(string $resourceKey, $id, array $dimensionAttributes = []): void | ||
{ | ||
$locale = $dimensionAttributes['locale'] ?? null; | ||
$stage = $dimensionAttributes['stage'] ?? null; | ||
|
||
$search = $this->searchManager->createSearch(sprintf('__id:"%s"', $id)) | ||
->indexes($this->getIndexes($resourceKey, $stage)); | ||
|
||
if ($locale) { | ||
$search->locale($locale); | ||
} | ||
|
||
$searchResult = $search->execute(); | ||
|
||
/** @var QueryHit $hit */ | ||
foreach ($searchResult as $hit) { | ||
$document = $hit->getDocument(); | ||
$this->searchManager->deindex($document, $document->getLocale()); | ||
} | ||
} | ||
|
||
public function deindexDimensionContent(DimensionContentInterface $dimensionContent): void | ||
{ | ||
if (!$dimensionContent->isMerged()) { | ||
return; | ||
} | ||
|
||
$this->searchManager->deindex($dimensionContent, $dimensionContent->getDimension()->getLocale()); | ||
} | ||
|
||
/** | ||
* @param mixed[] $dimensionAttributes | ||
*/ | ||
private function loadDimensionContent( | ||
ContentRichEntityInterface $contentRichEntity, | ||
array $dimensionAttributes | ||
): DimensionContentInterface { | ||
$locale = $dimensionAttributes['locale'] ?? null; | ||
$stage = $dimensionAttributes['stage'] ?? null; | ||
|
||
if (null === $locale || null === $stage) { | ||
throw new ContentNotFoundException($contentRichEntity, $dimensionAttributes); | ||
} | ||
|
||
$dimensionContent = $this->contentResolver->resolve($contentRichEntity, $dimensionAttributes); | ||
|
||
if ($locale !== $dimensionContent->getDimension()->getLocale() | ||
|| $stage !== $dimensionContent->getDimension()->getStage()) { | ||
throw new ContentNotFoundException($contentRichEntity, $dimensionAttributes); | ||
} | ||
|
||
return $dimensionContent; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
private function getIndexes(string $resourceKey, ?string $stage): array | ||
{ | ||
return array_filter( | ||
$this->searchManager->getIndexNames(), | ||
function ($indexName) use ($resourceKey, $stage) { | ||
if (null === $stage) { | ||
return $resourceKey === $indexName || $resourceKey . '_published' === $indexName; | ||
} | ||
|
||
if (DimensionInterface::STAGE_DRAFT === $stage) { | ||
return $resourceKey === $indexName; | ||
} | ||
|
||
if (DimensionInterface::STAGE_LIVE === $stage) { | ||
return $resourceKey . '_published' === $indexName; | ||
} | ||
|
||
return false; | ||
} | ||
); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Content/Application/ContentIndexer/ContentIndexerInterface.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 | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentIndexer; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; | ||
|
||
interface ContentIndexerInterface | ||
{ | ||
/** | ||
* @param mixed[] $dimensionAttributes | ||
*/ | ||
public function index(ContentRichEntityInterface $contentRichEntity, array $dimensionAttributes): DimensionContentInterface; | ||
|
||
public function indexDimensionContent(DimensionContentInterface $dimensionContent): void; | ||
|
||
/** | ||
* @param mixed $id | ||
* @param mixed[] $dimensionAttributes | ||
*/ | ||
public function deindex(string $resourceKey, $id, array $dimensionAttributes = []): void; | ||
|
||
public function deindexDimensionContent(DimensionContentInterface $dimensionContent): void; | ||
} |
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
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
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
Oops, something went wrong.