Skip to content

Instantly share code, notes, and snippets.

@mficzel
Created March 3, 2021 14:41
Show Gist options
  • Save mficzel/e33084bd0e6aefd33e91b1fd50cd22c9 to your computer and use it in GitHub Desktop.
Save mficzel/e33084bd0e6aefd33e91b1fd50cd22c9 to your computer and use it in GitHub Desktop.
Partial Node export
<?php
namespace Sitegeist\NodeShippingService\Command;
use Neos\ContentRepository\Domain\Service\ImportExport\NodeExportService;
use Neos\ContentRepository\Domain\Service\ImportExport\NodeImportService;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Neos\Exception as NeosException;
use Neos\Utility\Files;
use Neos\ContentRepository\Domain\Repository\NodeDataRepository;
/**
* The Site Command Controller
*
* @Flow\Scope("singleton")
*/
class NodeCommandController extends CommandController
{
/**
* @Flow\Inject
*
* @var NodeExportService
*/
protected $nodeExportService;
/**
* @Flow\Inject
*
* @var NodeImportService
*/
protected $nodeImportService;
/**
* @var NodeDataRepository
*/
protected $nodeDataRepository;
/**
* Import nodes from xml
**
* @param string $nodePath
* @param string $filename
* @return void
*/
public function importCommand($nodePath, $filename)
{
if (!file_exists($filename)) {
throw new NeosException(sprintf('Error: File "%s" does not exist.', $filename), 1540934412);
}
$xmlReader = new \XMLReader();
if ($xmlReader->open($filename, null, LIBXML_PARSEHUGE) === false) {
throw new NeosException(sprintf('Error: XMLReader could not open "%s".', $filename), 1540934199);
}
while ($xmlReader->read()) {
$this->nodeImportService->import($xmlReader, $nodePath, dirname($filename) . '/Resources');
}
}
/**
* Export nodes to xml
*
* @param string $identifier
* @param string $filename
* @param boolean $tidy
* @param string $nodeTypeFilter
* @return void
*/
public function exportCommand($nodePath, $filename, $tidy = true, $nodeTypeFilter = null)
{
$resourcesPath = Files::concatenatePaths([dirname($filename), 'Resources']);
Files::createDirectoryRecursively($resourcesPath);
$xmlWriter = new \XMLWriter();
$xmlWriter->openUri($filename);
$xmlWriter->setIndent($tidy);
$this->nodeExportService->export($nodePath, 'live', $xmlWriter, false, false, $resourcesPath, $nodeTypeFilter);
$xmlWriter->flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment