|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\SearchBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Kunstmaan\SearchBundle\Configuration\SearchConfigurationChain; |
|
6
|
|
|
use Kunstmaan\SearchBundle\Configuration\SearchConfigurationInterface; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
8
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Command to populate all indexes. Use the 'full' argument when you want to delete and add all indexes again |
|
15
|
|
|
* |
|
16
|
|
|
* It will load the SearchConfigurationChain and call the populateIndex() method on each SearchConfiguration |
|
17
|
|
|
* |
|
18
|
|
|
* @final since 5.1 |
|
19
|
|
|
* NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages |
|
20
|
|
|
*/ |
|
21
|
|
|
class PopulateIndexCommand extends ContainerAwareCommand |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var SearchConfigurationChain |
|
25
|
|
|
*/ |
|
26
|
|
|
private $configurationChain; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(/* SearchConfigurationChain */ $configurationChain = null) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct(); |
|
31
|
|
|
|
|
32
|
|
|
if (!$configurationChain instanceof SearchConfigurationChain) { |
|
33
|
|
|
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$this->setName(null === $configurationChain ? 'kuma:search:populate' : $configurationChain); |
|
36
|
|
|
|
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->configurationChain = $configurationChain; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function configure() |
|
44
|
|
|
{ |
|
45
|
|
|
$this |
|
46
|
|
|
->setName('kuma:search:populate') |
|
47
|
|
|
->addArgument('full', InputArgument::OPTIONAL, 'Delete and create new index(es) before populating') |
|
48
|
|
|
->setDescription('Populate the index(es)'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param InputInterface $input |
|
53
|
|
|
* @param OutputInterface $output |
|
54
|
|
|
* |
|
55
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
|
56
|
|
|
* @throws \LogicException |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($input->getArgument('full')) { |
|
61
|
|
|
$deleteCommand = $this->getApplication()->find('kuma:search:delete'); |
|
62
|
|
|
$deleteCommand->run(new ArrayInput([]), $output); |
|
63
|
|
|
|
|
64
|
|
|
$setupCommand = $this->getApplication()->find('kuma:search:setup'); |
|
65
|
|
|
$setupCommand->setHelperSet($this->getHelperSet()); |
|
66
|
|
|
$setupCommand->run(new ArrayInput([]), $output); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (null === $this->configurationChain) { |
|
70
|
|
|
$this->configurationChain = $this->getContainer()->get('kunstmaan_search.search_configuration_chain'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var string $alias |
|
75
|
|
|
* @var SearchConfigurationInterface $searchConfiguration |
|
76
|
|
|
*/ |
|
77
|
|
|
foreach ($this->configurationChain->getConfigurations() as $alias => $searchConfiguration) { |
|
78
|
|
|
$searchConfiguration->populateIndex(); |
|
79
|
|
|
$output->writeln('Index populated : ' . $alias); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: