1 | <?php |
||
21 | abstract class AbstractManagerAwareCommand extends ContainerAwareCommand |
||
22 | { |
||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | protected function configure() |
||
27 | { |
||
28 | $this->addOption( |
||
29 | 'manager', |
||
30 | 'm', |
||
31 | InputOption::VALUE_REQUIRED, |
||
32 | 'Manager name', |
||
33 | 'default' |
||
34 | ); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Returns elasticsearch manager by name from service container. |
||
39 | * |
||
40 | * @param string $name Manager name defined in configuration. |
||
41 | * |
||
42 | * @return Manager |
||
43 | * |
||
44 | * @throws \RuntimeException If manager was not found. |
||
45 | */ |
||
46 | protected function getManager($name) |
||
47 | { |
||
48 | $id = $this->getManagerId($name); |
||
49 | |||
50 | if ($this->getContainer()->has($id)) { |
||
51 | return $this->getContainer()->get($id); |
||
52 | } |
||
53 | |||
54 | throw new \RuntimeException( |
||
55 | sprintf( |
||
56 | 'Manager named `%s` not found. Available: `%s`.', |
||
57 | $name, |
||
58 | implode('`, `', array_keys($this->getContainer()->getParameter('es.managers'))) |
||
59 | ) |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Formats manager service id from its name. |
||
65 | * |
||
66 | * @param string $name Manager name. |
||
67 | * |
||
68 | * @return string Service id. |
||
69 | */ |
||
70 | private function getManagerId($name) |
||
74 | } |
||
75 |