diff --git a/Command/AbstractManagerAwareCommand.php b/Command/AbstractManagerAwareCommand.php index 8a8905e8..3633f459 100644 --- a/Command/AbstractManagerAwareCommand.php +++ b/Command/AbstractManagerAwareCommand.php @@ -12,14 +12,54 @@ namespace ONGR\ElasticsearchBundle\Command; use ONGR\ElasticsearchBundle\Service\Manager; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * AbstractElasticsearchCommand class. */ -abstract class AbstractManagerAwareCommand extends ContainerAwareCommand +abstract class AbstractManagerAwareCommand extends Command { + + /** + * @var ContainerInterface|null + */ + private $container; + + public function __construct(ContainerInterface $container) + { + $this->container = $container; + parent::__construct(); + } + + /** + * @return ContainerInterface + * + * @throws \LogicException + */ + protected function getContainer() + { + if (null === $this->container) { + $application = $this->getApplication(); + if (null === $application) { + throw new \LogicException('The container cannot be retrieved as the application instance is not yet set.'); + } + + $this->container = $application->getKernel()->getContainer(); + } + + return $this->container; + } + + /** + * {@inheritdoc} + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + /** * {@inheritdoc} */ diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 0883bcad..b6f41acc 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -25,8 +25,13 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('ongr_elasticsearch'); + $treeBuilder = new TreeBuilder('ongr_elasticsearch'); + if (\method_exists($treeBuilder, 'getRootNode')) { + $rootNode = $treeBuilder->getRootNode(); + } else { + // backward compatibility for symfony/config <= 4.1 + $rootNode = $treeBuilder->root('ongr_elasticsearch'); + } $rootNode ->children() @@ -56,8 +61,13 @@ public function getConfigTreeBuilder() */ private function getAnalysisNode() { - $builder = new TreeBuilder(); - $node = $builder->root('analysis'); + $builder = new TreeBuilder('analysis'); + if (\method_exists($builder, 'getRootNode')) { + $node = $builder->getRootNode(); + } else { + // backward compatibility for symfony/config <= 4.1 + $node = $builder->root('analysis'); + } $node ->info('Defines analyzers, normalizers, tokenizers and filters') @@ -95,8 +105,13 @@ private function getAnalysisNode() */ private function getManagersNode() { - $builder = new TreeBuilder(); - $node = $builder->root('managers'); + $builder = new TreeBuilder('managers'); + if (\method_exists($builder, 'getRootNode')) { + $node = $builder->getRootNode(); + } else { + // backward compatibility for symfony/config <= 4.1 + $node = $builder->root('managers'); + } $node ->isRequired() diff --git a/Resources/config/services4.yaml b/Resources/config/services4.yaml index 5b480602..cb3fe995 100644 --- a/Resources/config/services4.yaml +++ b/Resources/config/services4.yaml @@ -2,5 +2,6 @@ services: ONGR\ElasticsearchBundle\Command\: resource: '../../Command' public: true + arguments: ["@service_container"] tags: - { name: console.command } \ No newline at end of file