Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecation notices with Symfony 4.2 #273

Merged
merged 2 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
alcaeus marked this conversation as resolved.
Show resolved Hide resolved
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()
alcaeus marked this conversation as resolved.
Show resolved Hide resolved
{
if ($this->managerRegistry === null) {
$this->managerRegistry = $this->container->get('doctrine');
}

return $this->managerRegistry;
}
}
10 changes: 8 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use function method_exists;

/**
* This is the class that validates and merges configuration from your app/config files.
Expand All @@ -17,8 +18,13 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('algolia_search');
if (method_exists(TreeBuilder::class, 'getRootNode')) {
alcaeus marked this conversation as resolved.
Show resolved Hide resolved
$treeBuilder = new TreeBuilder('algolia_search');
$rootNode = $treeBuilder->getRootNode();
} else {
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('algolia_search');
}

$rootNode
->children()
Expand Down