Skip to content

Commit

Permalink
Feature: Define storage node by identifier
Browse files Browse the repository at this point in the history
Instead of defining the siteNode path and the storage node path,
in case the storage node already exists, you can use the
indetifier of the storage node.

See issue ttreeagency#24
  • Loading branch information
daniellienert committed Aug 25, 2017
1 parent 9dfc333 commit 909a08b
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions Classes/Importer/AbstractImporter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Ttree\ContentRepositoryImporter\Importer;

use Neos\Cache\Frontend\VariableFrontend;
Expand Down Expand Up @@ -29,6 +30,12 @@
*/
abstract class AbstractImporter implements ImporterInterface
{
/**
* The unique identifier of the storage node
* @var string
*/
protected $storageNodeIdentifier;

/**
* Node path (can be absolute or relative to the current site node) where the "storage node" (ie. the parent
* document node for nodes imported by the concrete importer) will be located. You can also use the identifier of the node.
Expand Down Expand Up @@ -312,12 +319,26 @@ public function initialize(DataProviderInterface $dataProvider)
$context = $this->contextFactory->create($contextConfiguration);
$this->rootNode = $context->getRootNode();

$this->applyOption($this->storageNodeNodePath, 'storageNodeNodePath');
$this->applyOption($this->nodeTypeName, 'nodeTypeName');
$this->applyOption($this->storageNodeIdentifier, 'storageNodeIdentifier');
$this->applyOption($this->storageNodeNodePath, 'storageNodeNodePath');

if (isset($this->options['siteNodePath']) || isset($this->options['siteNodeIdentifier'])) {
$siteNodePath = isset($this->options['siteNodePath']) ? trim($this->options['siteNodePath']) : null;
$siteNodeIdentifier = isset($this->options['siteNodeIdentifier']) ? trim($this->options['siteNodeIdentifier']) : null;
$siteNodePath = isset($this->options['siteNodePath']) ? trim($this->options['siteNodePath']) : null;
$siteNodeIdentifier = isset($this->options['siteNodeIdentifier']) ? trim($this->options['siteNodeIdentifier']) : null;

if (isset($this->storageNodeIdentifier)) {
$this->storageNode = $context->getNodeByIdentifier($this->storageNodeIdentifier);
if (!($this->storageNode instanceof NodeInterface)) {
throw new Exception(sprintf('The storage node with identifier "%s" was not found', $this->storageNodeIdentifier));
}

$pathParts = explode('/', ltrim($this->storageNode->getPath(), '/'));
$siteNodePath = '/sites/' . $pathParts[1];

$this->storageNodeNodePath = $this->storageNode->getPath();
}

if (isset($siteNodePath) || isset($siteNodeIdentifier)) {
$this->siteNode = $this->rootNode->getNode($siteNodePath) ?: $context->getNodeByIdentifier($siteNodeIdentifier);
if ($this->siteNode === null) {
throw new Exception(sprintf('Site node not found (%s)', $siteNodePath ?: $siteNodeIdentifier), 1425077201);
Expand Down Expand Up @@ -373,6 +394,7 @@ protected function processBatch(NodeTemplate $nodeTemplate = null)
$this->postProcessing($records);
}


public function withStorageNode(NodeInterface $storageNode, \Closure $closure)
{
$previousStorageNode = $this->storageNode;
Expand Down Expand Up @@ -582,7 +604,7 @@ protected function getExternalIdentifierFromRecordData(array $data)
if ($externalIdentifier === null) {
throw new \Exception('Could not determine external identifier from record data. See ' . self::class . ' for more information.', 1462968317292);
}
return (string)$externalIdentifier;
return (string) $externalIdentifier;
}

/**
Expand Down Expand Up @@ -612,7 +634,7 @@ protected function getLabelFromRecordData(array $data)
if ($label === null) {
throw new \Exception('Could not determine label from record data (key: ' . $this->labelDataKey . '). See ' . self::class . ' for more information.', 1462968958372);
}
return (string)$label;
return (string) $label;
}


Expand Down

0 comments on commit 909a08b

Please sign in to comment.