Skip to content

Commit

Permalink
Fix usage of deprecated ContainerAwareCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Dec 3, 2018
1 parent 5c18a0a commit 82f64cc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
25 changes: 22 additions & 3 deletions src/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

namespace Algolia\SearchBundle\Command;


use Algolia\SearchBundle\IndexManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use const E_USER_DEPRECATED;
use function trigger_error;

abstract class IndexCommand extends ContainerAwareCommand
abstract class IndexCommand extends Command implements ContainerAwareInterface
{
use ContainerAwareTrait;

protected $indexManager;

public function __construct(IndexManagerInterface $indexManager)
Expand All @@ -19,6 +25,19 @@ public function __construct(IndexManagerInterface $indexManager)
parent::__construct();
}

/**
* @return ContainerInterface
*/
protected function getContainer()
{
@trigger_error(
sprintf('The %s method is deprecated and should not be used. Please wire your dependencies explicitly.', __METHOD__),
E_USER_DEPRECATED
);

return $this->container;
}

protected function getEntitiesFromArgs(InputInterface $input, OutputInterface $output)
{
$entities = [];
Expand Down
35 changes: 32 additions & 3 deletions src/Command/SearchImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@

namespace Algolia\SearchBundle\Command;


use Algolia\SearchBundle\IndexManagerInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use const E_USER_DEPRECATED;
use function trigger_error;

class SearchImportCommand extends IndexCommand
{
protected static $defaultName = 'search:import';

/**
* @var ManagerRegistry|null
*/
private $managerRegistry;

public function __construct(IndexManagerInterface $indexManager, ManagerRegistry $managerRegistry = null)
{
parent::__construct($indexManager);

$this->managerRegistry = $managerRegistry;
if ($managerRegistry === null) {
@trigger_error('Instantiating the SearchImportCommand without a manager registry is deprecated', E_USER_DEPRECATED);
}
}

protected function configure()
{
$this
Expand All @@ -27,12 +45,11 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$doctrine = $this->getContainer()->get('doctrine');
$entitiesToIndex = $this->getEntitiesFromArgs($input, $output);
$config = $this->indexManager->getConfiguration();

foreach ($entitiesToIndex as $indexName => $entityClassName) {
$manager = $doctrine->getManagerForClass($entityClassName);
$manager = $this->getManagerRegistry()->getManagerForClass($entityClassName);
$repository = $manager->getRepository($entityClassName);

$page = 0;
Expand Down Expand Up @@ -63,4 +80,16 @@ protected function execute(InputInterface $input, OutputInterface $output)

$output->writeln('<info>Done!</info>');
}

/**
* @return ManagerRegistry
*/
private function getManagerRegistry()
{
if ($this->managerRegistry === null) {
$this->managerRegistry = $this->container->get('doctrine');
}

return $this->managerRegistry;
}
}

0 comments on commit 82f64cc

Please sign in to comment.