-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ArticleReferenceProvider and ArticleReferenceRefresher
- Loading branch information
1 parent
7568fc2
commit c433fff
Showing
4 changed files
with
168 additions
and
0 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
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,62 @@ | ||
<?php | ||
|
||
/* | ||
* 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\ArticleBundle\Reference\Provider; | ||
|
||
use Sulu\Bundle\ArticleBundle\Controller\ArticleController; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; | ||
use Sulu\Bundle\DocumentManagerBundle\Reference\Provider\AbstractDocumentReferenceProvider; | ||
use Sulu\Bundle\ReferenceBundle\Domain\Repository\ReferenceRepositoryInterface; | ||
use Sulu\Component\Content\Compat\StructureManagerInterface; | ||
use Sulu\Component\Content\ContentTypeManagerInterface; | ||
use Sulu\Component\Content\Document\Behavior\WebspaceBehavior; | ||
use Sulu\Component\Content\Extension\ExtensionManagerInterface; | ||
|
||
/** | ||
* @final | ||
* | ||
* @internal | ||
*/ | ||
class ArticleReferenceProvider extends AbstractDocumentReferenceProvider | ||
{ | ||
public function __construct( | ||
ContentTypeManagerInterface $contentTypeManager, | ||
StructureManagerInterface $structureManager, | ||
ExtensionManagerInterface $extensionManager, | ||
ReferenceRepositoryInterface $referenceRepository, | ||
) { | ||
parent::__construct( | ||
$contentTypeManager, | ||
$structureManager, | ||
$extensionManager, | ||
$referenceRepository, | ||
ArticleController::DOCUMENT_TYPE | ||
); | ||
} | ||
|
||
public static function getResourceKey(): string | ||
{ | ||
return ArticleDocument::RESOURCE_KEY; | ||
} | ||
|
||
protected function getReferenceRouterAttributes($document, string $locale): array | ||
{ | ||
$referenceRouterAttributes = parent::getReferenceRouterAttributes($document, $locale); | ||
|
||
if (!$document instanceof WebspaceBehavior) { | ||
return $referenceRouterAttributes; | ||
} | ||
|
||
return \array_merge($referenceRouterAttributes, [ | ||
'webspace' => $document->getWebspaceName(), | ||
]); | ||
} | ||
} |
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,79 @@ | ||
<?php | ||
|
||
/* | ||
* 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\ArticleBundle\Reference\Refresh; | ||
|
||
use Jackalope\Query\Row; | ||
use Jackalope\Query\RowIterator; | ||
use PHPCR\SessionInterface; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; | ||
use Sulu\Bundle\DocumentManagerBundle\Reference\Provider\DocumentReferenceProviderInterface; | ||
use Sulu\Bundle\ReferenceBundle\Application\Refresh\ReferenceRefresherInterface; | ||
use Sulu\Component\Content\Document\Behavior\StructureBehavior; | ||
use Sulu\Component\DocumentManager\Behavior\Mapping\TitleBehavior; | ||
use Sulu\Component\DocumentManager\Behavior\Mapping\UuidBehavior; | ||
use Sulu\Component\DocumentManager\DocumentManagerInterface; | ||
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface; | ||
|
||
/** | ||
* @internal your code should not depend on this class | ||
* | ||
* @final | ||
*/ | ||
class ArticleReferenceRefresher implements ReferenceRefresherInterface | ||
{ | ||
public function __construct( | ||
private SessionInterface $session, | ||
private WebspaceManagerInterface $webspaceManager, | ||
private DocumentManagerInterface $documentManager, | ||
private DocumentReferenceProviderInterface $documentReferenceProvider, | ||
private string $suluContext, | ||
) { | ||
} | ||
|
||
public static function getResourceKey(): string | ||
{ | ||
return ArticleDocument::RESOURCE_KEY; | ||
} | ||
|
||
public function refresh(): \Generator | ||
{ | ||
$sql2 = \sprintf("SELECT jcr:uuid FROM [nt:unstructured] as document WHERE document.[jcr:mixinTypes] = 'sulu:%s' AND isdescendantnode(document, '/cmf/%s')", 'article', 'articles'); | ||
|
||
$queryManager = $this->session->getWorkspace()->getQueryManager(); | ||
$query = $queryManager->createQuery($sql2, 'JCR-SQL2'); | ||
$queryResult = $query->execute(); | ||
|
||
/** @var RowIterator $rows */ | ||
$rows = $queryResult->getRows(); | ||
|
||
foreach ($this->webspaceManager->getAllLocalizations() as $localization) { | ||
$locale = $localization->getLocale(); | ||
/** @var Row $row */ | ||
foreach ($rows as $row) { | ||
/** @var string $uuid */ | ||
$uuid = $row->getValue('jcr:uuid'); | ||
/** @var (UuidBehavior&TitleBehavior&StructureBehavior)|null $document */ | ||
$document = $this->documentManager->find($uuid, $locale); | ||
|
||
if (!$document) { | ||
continue; | ||
} | ||
|
||
$this->documentReferenceProvider->updateReferences($document, $locale, $this->suluContext); | ||
|
||
yield $document; | ||
} | ||
|
||
$this->documentManager->clear(); // the cache is locale independent, so we need to clear between locale changes | ||
} | ||
} | ||
} |
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 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="sulu_article.reference_provider" class="Sulu\Bundle\ArticleBundle\Reference\Provider\ArticleReferenceProvider"> | ||
<argument type="service" id="sulu.content.type_manager"/> | ||
<argument type="service" id="sulu.content.structure_manager"/> | ||
<argument type="service" id="sulu_page.extension.manager"/> | ||
<argument type="service" id="sulu_reference.reference_repository"/> | ||
|
||
<tag name="sulu_document_manager.reference_provider" /> | ||
</service> | ||
|
||
<service id="sulu_article.article_reference_refresher" class="Sulu\Bundle\ArticleBundle\Reference\Refresh\ArticleReferenceRefresher"> | ||
<argument type="service" id="sulu_document_manager.default_session"/> | ||
<argument type="service" id="sulu_core.webspace.webspace_manager"/> | ||
<argument type="service" id="sulu_document_manager.document_manager"/> | ||
<argument type="service" id="sulu_article.reference_provider"/> | ||
<argument>%sulu.context%</argument> | ||
|
||
<tag name="sulu_reference.refresher"/> | ||
</service> | ||
</services> | ||
</container> |