1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchBundle\Command; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchBundle\Service\Manager; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* AbstractElasticsearchCommand class. |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractManagerAwareCommand extends Command |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ContainerInterface|null |
27
|
|
|
*/ |
28
|
|
|
private $container; |
29
|
|
|
|
30
|
|
|
public function __construct(ContainerInterface $container) |
31
|
|
|
{ |
32
|
|
|
$this->container = $container; |
33
|
|
|
parent::__construct(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return ContainerInterface |
38
|
|
|
* |
39
|
|
|
* @throws \LogicException |
40
|
|
|
*/ |
41
|
|
|
protected function getContainer() |
42
|
|
|
{ |
43
|
|
|
if (null === $this->container) { |
44
|
|
|
$application = $this->getApplication(); |
45
|
|
|
if (null === $application) { |
46
|
|
|
throw new \LogicException('The container cannot be retrieved as the application instance is not yet set.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->container = $application->getKernel()->getContainer(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->container; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function setContainer(ContainerInterface $container = null) |
59
|
|
|
{ |
60
|
|
|
$this->container = $container; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
protected function configure() |
67
|
|
|
{ |
68
|
|
|
$this->addOption( |
69
|
|
|
'manager', |
70
|
|
|
'm', |
71
|
|
|
InputOption::VALUE_REQUIRED, |
72
|
|
|
'Manager name', |
73
|
|
|
'default' |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns elasticsearch manager by name from service container. |
79
|
|
|
* |
80
|
|
|
* @param string $name Manager name defined in configuration. |
81
|
|
|
* |
82
|
|
|
* @return Manager |
83
|
|
|
* |
84
|
|
|
* @throws \RuntimeException If manager was not found. |
85
|
|
|
*/ |
86
|
|
|
protected function getManager($name) |
87
|
|
|
{ |
88
|
|
|
$id = $this->getManagerId($name); |
89
|
|
|
|
90
|
|
|
if ($this->getContainer()->has($id)) { |
91
|
|
|
return $this->getContainer()->get($id); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
throw new \RuntimeException( |
95
|
|
|
sprintf( |
96
|
|
|
'Manager named `%s` not found. Available: `%s`.', |
97
|
|
|
$name, |
98
|
|
|
implode('`, `', array_keys($this->getContainer()->getParameter('es.managers'))) |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Formats manager service id from its name. |
105
|
|
|
* |
106
|
|
|
* @param string $name Manager name. |
107
|
|
|
* |
108
|
|
|
* @return string Service id. |
109
|
|
|
*/ |
110
|
|
|
private function getManagerId($name) |
111
|
|
|
{ |
112
|
|
|
return sprintf('es.manager.%s', $name); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|