Skip to content

Commit

Permalink
Remove usages of ContainerAwareCommand
Browse files Browse the repository at this point in the history
Deprecated AbstractManagerAwareCommand
  • Loading branch information
pmishev committed Aug 15, 2019
1 parent d274466 commit 28e85de
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Command/AbstractManagerAwareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* Class providing common methods for commands working with an index manager
*
* @deprecated Inject IndexManagerRegistry into your own command and get the index manager from it
*/
abstract class AbstractManagerAwareCommand extends ContainerAwareCommand
{
Expand Down
32 changes: 27 additions & 5 deletions Command/IndexBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@

namespace Sineflow\ElasticsearchBundle\Command;

use Sineflow\ElasticsearchBundle\Manager\IndexManager;
use Sineflow\ElasticsearchBundle\Manager\IndexManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command for (re)building elasticsearch index.
*/
class IndexBuildCommand extends AbstractManagerAwareCommand
class IndexBuildCommand extends Command
{
protected static $defaultName = 'sineflow:es:index:build';

/**
* @var IndexManagerRegistry
*/
private $indexManagerRegistry;

/**
* @param IndexManagerRegistry $indexManagerRegistry
*/
public function __construct(IndexManagerRegistry $indexManagerRegistry)
{
$this->indexManagerRegistry = $indexManagerRegistry;

parent::__construct();
}

/**
* {@inheritdoc}
*/
Expand All @@ -20,8 +39,12 @@ protected function configure()
parent::configure();

$this
->setName('sineflow:es:index:build')
->setDescription('(Re)builds elasticsearch index.')
->addArgument(
'index',
InputArgument::REQUIRED,
'The identifier of the index'
)
->addOption(
'delete-old',
null,
Expand All @@ -42,8 +65,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$indexManagerName = $input->getArgument('index');
/** @var IndexManager $indexManager */
$indexManager = $this->getManager($indexManagerName);
$indexManager = $this->indexManagerRegistry->get($indexManagerName);

$deleteOldIndex = (bool) $input->getOption('delete-old');
$cancelCurrent = (bool) $input->getOption('cancel-current');
Expand Down
32 changes: 28 additions & 4 deletions Command/IndexCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@

namespace Sineflow\ElasticsearchBundle\Command;

use Sineflow\ElasticsearchBundle\Manager\IndexManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command for creating elasticsearch index.
*/
class IndexCreateCommand extends AbstractManagerAwareCommand
class IndexCreateCommand extends Command
{
protected static $defaultName = 'sineflow:es:index:create';

/**
* @var IndexManagerRegistry
*/
private $indexManagerRegistry;

/**
* @param IndexManagerRegistry $indexManagerRegistry
*/
public function __construct(IndexManagerRegistry $indexManagerRegistry)
{
$this->indexManagerRegistry = $indexManagerRegistry;

parent::__construct();
}

/**
* {@inheritdoc}
*/
Expand All @@ -18,8 +38,12 @@ protected function configure()
parent::configure();

$this
->setName('sineflow:es:index:create')
->setDescription('Creates elasticsearch index.');
->setDescription('Creates elasticsearch index.')
->addArgument(
'index',
InputArgument::REQUIRED,
'The identifier of the index'
);
}

/**
Expand All @@ -28,7 +52,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$indexManagerName = $input->getArgument('index');
$indexManager = $this->getManager($indexManagerName);
$indexManager = $this->indexManagerRegistry->get($indexManagerName);
try {
$indexManager->createIndex();
$output->writeln(
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- { resource: legacy_aliases.yml }
- { resource: services_commands.yml }

parameters:
sfes.logging.path: "%kernel.logs_dir%/%kernel.environment%.sf_elasticsearch.log"
Expand Down
15 changes: 15 additions & 0 deletions Resources/config/services_commands.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
_defaults:
public: false

Sineflow\ElasticsearchBundle\Command\IndexCreateCommand:
arguments:
- '@Sineflow\ElasticsearchBundle\Manager\IndexManagerRegistry'
tags:
- { name: 'console.command' }

Sineflow\ElasticsearchBundle\Command\IndexBuildCommand:
arguments:
- '@Sineflow\ElasticsearchBundle\Manager\IndexManagerRegistry'
tags:
- { name: 'console.command' }
4 changes: 2 additions & 2 deletions Tests/Functional/Command/IndexBuildCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function testExecute()
*/
protected function getCommand()
{
$command = new IndexBuildCommand();
$command->setContainer($this->getContainer());
$registry = $this->getContainer()->get('sfes.index_manager_registry');
$command = new IndexBuildCommand($registry);

return $command;
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/Functional/Command/IndexCreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ public function testExecuteWithExistingIndex()
}

/**
* Returns create index command with assigned container.
* Returns create index command
*
* @return IndexCreateCommand
*/
protected function getCommand()
{
$command = new IndexCreateCommand();
$command->setContainer($this->getContainer());
$registry = $this->getContainer()->get('sfes.index_manager_registry');
$command = new IndexCreateCommand($registry);

return $command;
}
Expand Down

0 comments on commit 28e85de

Please sign in to comment.