Skip to content

Instantly share code, notes, and snippets.

@wachterjohannes
Created September 18, 2017 06:41
Show Gist options
  • Save wachterjohannes/b58f846f2f56e2453e9e84ab6ff74c1b to your computer and use it in GitHub Desktop.
Save wachterjohannes/b58f846f2f56e2453e9e84ab6ff74c1b to your computer and use it in GitHub Desktop.
Create article from website-kernel
<?php
namespace App\Course;
use PHPCR\NodeInterface;
use PHPCR\SessionInterface;
use PHPCR\Util\UUIDHelper;
use Sulu\Bundle\ArticleBundle\Document\Behavior\DateShardingBehavior;
use Sulu\Component\DocumentManager\Event\FlushEvent;
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Events;
use Sulu\Component\DocumentManager\PathBuilder;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CourseSubscriber implements EventSubscriberInterface
{
/**
* @var PathBuilder
*/
private $pathBuilder;
/**
* @var SessionInterface
*/
private $session;
/**
* @var SessionInterface
*/
private $liveSession;
public function __construct(PathBuilder $pathBuilder, SessionInterface $session, SessionInterface $liveSession)
{
$this->pathBuilder = $pathBuilder;
$this->session = $session;
$this->liveSession = $liveSession;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
Events::PERSIST => [
['handleSetParentNode', 482],
['createNodeInPublicWorkspace', 479],
],
Events::FLUSH => [
['handleFlush', 500],
],
];
}
public function handleSetParentNode(PersistEvent $event)
{
if (!$event->getDocument() instanceof DateShardingBehavior || $event->hasParentNode()) {
return;
}
$date = $event->getDocument()->getCreated();
if (null === $date) {
$date = new \DateTime();
}
$path = $this->pathBuilder->build(['%base%', '%articles%', $date->format('Y'), $date->format('m')]);
$event->setParentNode($this->createPath($path));
}
public function createNodeInPublicWorkspace(PersistEvent $event)
{
$node = $event->getNode();
if (!$node->isNew()) {
return;
}
$node->addMixin('mix:referenceable');
$node->setProperty('jcr:uuid', UUIDHelper::generateUUID());
$this->createNodesWithUuid($node);
}
public function handleFlush(FlushEvent $event)
{
$this->session->save();
$this->liveSession->save();
}
/**
* Create a path.
*
* @param string $path
*
* @return NodeInterface
*/
public function createPath($path)
{
$current = $this->session->getRootNode();
$segments = preg_split('#/#', $path, null, PREG_SPLIT_NO_EMPTY);
foreach ($segments as $segment) {
if ($current->hasNode($segment)) {
$current = $current->getNode($segment);
} else {
$current = $current->addNode($segment);
$current->addMixin('mix:referenceable');
$current->setProperty('jcr:uuid', UUIDHelper::generateUUID());
}
}
return $current;
}
/**
* Creates every node on the path to the given node. Also uses the same UUIDs for these nodes.
*
* @param NodeInterface $node
*/
private function createNodesWithUuid(NodeInterface $node)
{
$path = $node->getPath();
if ($this->liveSession->itemExists($path)) {
return;
}
$currentDefaultNode = $node->getSession()->getRootNode();
$currentLiveNode = $this->liveSession->getRootNode();
$pathSegments = explode('/', ltrim($path, '/'));
foreach ($pathSegments as $pathSegment) {
$currentDefaultNode = $currentDefaultNode->getNode($pathSegment);
if ($currentLiveNode->hasNode($pathSegment)) {
$currentLiveNode = $currentLiveNode->getNode($pathSegment);
continue;
}
$currentLiveNode = $currentLiveNode->addNode($pathSegment);
$currentLiveNode->setMixins(['mix:referenceable']);
$currentLiveNode->setProperty('jcr:uuid', $currentDefaultNode->getIdentifier());
}
}
}
services:
app.course.sharding:
class: App\Course\CourseSubscriber
arguments:
- '@sulu_document_manager.path_builder'
- '@doctrine_phpcr.default_session'
- '@doctrine_phpcr.live_session'
tags:
- {name: 'sulu.context', context: 'website'}
- {name: 'sulu_document_manager.event_subscriber'}
@RaphaelHofer
Copy link

If you want to publish the sites directly you need to overwrite the workflow_stage subscriber service like this:

        <service id="sulu_document_manager.document.subscriber.workflow_stage"
                 class="Sulu\Component\Content\Document\Subscriber\WorkflowStageSubscriber">
            <argument type="service" id="sulu_document_manager.property_encoder"/>
            <argument type="service" id="sulu_document_manager.document_inspector"/>
            <argument type="service" id="doctrine_phpcr.default_session"/>
            <argument type="service" id="doctrine_phpcr.live_session"/>
            <tag name="sulu_document_manager.event_subscriber" />
        </service>

The Essential lines are

            <argument type="service" id="doctrine_phpcr.default_session"/>
            <argument type="service" id="doctrine_phpcr.live_session"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment