Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mficzel/a8d63476a5d57f9dc375f24a884738cb to your computer and use it in GitHub Desktop.
Save mficzel/a8d63476a5d57f9dc375f24a884738cb to your computer and use it in GitHub Desktop.
Resource publishing with limits ... to avoid memory errors
<?php
namespace Vendor\Site\Command;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\ResourceManagement\CollectionInterface;
use Neos\Flow\ResourceManagement\PersistentResource;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\ResourceManagement\ResourceRepository;
use Neos\Flow\ResourceManagement\Target\Exception as PublicationTargetException;
/**
* Command controller for additional resource commands
*/
class ResourceCommandController extends CommandController
{
/**
* @Flow\Inject
* @var ResourceManager
*/
protected $resourceManager;
/**
* @Flow\Inject
* @var ResourceRepository
*/
protected $resourceRepository;
public function publishStaticCommand(): void
{
$staticCollection = $this->resourceManager->getCollection('static');
$target = $staticCollection->getTarget();
$target->publishCollection($staticCollection);
$this->outputLine('Successfully published collection "static"');
}
/**
* @param int $from
* @param int $limit
* @param bool $noProgress
*/
public function publishPersistentCommand(int $from, int $limit = 100000, bool $noProgress = false): void
{
$persistentCollection = $this->resourceManager->getCollection('persistent');
$target = $persistentCollection->getTarget();
$numberOfPersistentResources = $this->getNumberOfPersistentResources($persistentCollection);
if ($from > $numberOfPersistentResources) {
$this->outputLine('Only ' . $numberOfPersistentResources . ' total, nothing to do');
return;
}
$this->outputLine('Publishing persistent resources ' . $from . ' to ' . (min($from + $limit, $numberOfPersistentResources)) . ' of ' . $numberOfPersistentResources . '.');
if (!$noProgress) {
$this->output->progressStart(min($limit, $numberOfPersistentResources - $from));
}
foreach ($this->getPersistentResources($persistentCollection, $from, $limit) as $persistentResource) {
try {
$target->publishResource($persistentResource, $persistentCollection);
} catch (PublicationTargetException $e) {
}
if (!$noProgress) {
$this->output->progressAdvance();
}
}
if (!$noProgress) {
$this->output->progressFinish();
}
$this->output->outputLine();
}
/**
* @param CollectionInterface $collection
* @param int $from
* @param int $limit
* @return array|PersistentResource[]
*/
public function getPersistentResources(CollectionInterface $collection, int $from, int $limit): array
{
$query = $this->resourceRepository->createQuery();
/** @var array|PersistentResource[] $resources */
$resources = $query->matching(
$query->equals('collectionName', $collection->getName())
)->setOffset($from)
->setLimit($limit)
->execute()
->toArray();
return $resources;
}
public function getNumberOfPersistentResources(CollectionInterface $collection): int
{
$query = $this->resourceRepository->createQuery();
return $query->matching(
$query->equals('collectionName', $collection->getName())
)->count();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment