FOSElasticaBundle/Command/ResetCommand.php

79 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2013-04-26 14:15:57 +02:00
<?php
namespace FOS\ElasticaBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use FOS\ElasticaBundle\IndexManager;
use FOS\ElasticaBundle\Resetter;
/**
2015-03-12 11:20:00 +01:00
* Reset search indexes.
2013-04-26 14:15:57 +02:00
*/
2013-05-08 09:10:40 +02:00
class ResetCommand extends ContainerAwareCommand
2013-04-26 14:15:57 +02:00
{
/**
* @var IndexManager
*/
private $indexManager;
/**
* @var Resetter
*/
private $resetter;
/**
* @see Symfony\Component\Console\Command\Command::configure()
*/
protected function configure()
{
$this
2013-05-08 09:10:40 +02:00
->setName('fos:elastica:reset')
->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to reset')
->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to reset')
->addOption('force', null, InputOption::VALUE_NONE, 'Force index deletion if same name as alias')
2013-05-08 09:10:40 +02:00
->setDescription('Reset search indexes')
2013-04-26 14:15:57 +02:00
;
}
/**
* @see Symfony\Component\Console\Command\Command::initialize()
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->indexManager = $this->getContainer()->get('fos_elastica.index_manager');
$this->resetter = $this->getContainer()->get('fos_elastica.resetter');
}
/**
* @see Symfony\Component\Console\Command\Command::execute()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$index = $input->getOption('index');
$type = $input->getOption('type');
$force = (bool) $input->getOption('force');
2013-04-26 14:15:57 +02:00
if (null === $index && null !== $type) {
throw new \InvalidArgumentException('Cannot specify type option without an index.');
}
if (null !== $type) {
2013-06-18 09:52:44 +02:00
$output->writeln(sprintf('<info>Resetting</info> <comment>%s/%s</comment>', $index, $type));
$this->resetter->resetIndexType($index, $type);
2013-04-26 14:15:57 +02:00
} else {
$indexes = null === $index
? array_keys($this->indexManager->getAllIndexes())
: array($index)
;
foreach ($indexes as $index) {
$output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
$this->resetter->resetIndex($index, false, $force);
2013-04-26 14:15:57 +02:00
}
}
}
}