Skip to content

Commit

Permalink
Add ArticleReferenceProvider and ArticleReferenceRefresher
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn authored and alexander-schranz committed Mar 29, 2024
1 parent 7568fc2 commit c433fff
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DependencyInjection/SuluArticleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ public function load(array $configs, ContainerBuilder $container)
if (\array_key_exists('SuluHeadlessBundle', $bundles)) {
$loader->load('services_headless.xml');
}
if (\array_key_exists('SuluReferenceBundle', $bundles)) {
$loader->load('services_reference.xml');
}

$this->appendDefaultAuthor($config, $container);
$this->appendArticlePageConfig($container);
Expand Down
62 changes: 62 additions & 0 deletions Reference/Provider/ArticleReferenceProvider.php
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(),
]);
}
}
79 changes: 79 additions & 0 deletions Reference/Refresh/ArticleReferenceRefresher.php
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
}
}
}
24 changes: 24 additions & 0 deletions Resources/config/services_reference.xml
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>

0 comments on commit c433fff

Please sign in to comment.