|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace T3G\Elasticorn\Commands; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Log\LogLevel; |
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Question\Question; |
|
12
|
|
|
use T3G\Elasticorn\Service\ConfigurationService; |
|
13
|
|
|
use T3G\Elasticorn\Service\IndexService; |
|
14
|
|
|
|
|
15
|
|
|
class BaseCommand extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var IndexService |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $indexService; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ConfigurationService |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $configurationService; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \T3G\Elasticorn\Service\DocumentTypeService |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $documentTypeService; |
|
32
|
|
|
|
|
33
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
34
|
|
|
{ |
|
35
|
|
|
global $container; |
|
|
|
|
|
|
36
|
|
|
$verbosityLevelMap = [ |
|
37
|
|
|
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL, |
|
38
|
|
|
LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL, |
|
39
|
|
|
]; |
|
40
|
|
|
$container->setParameter('logger.output', $output); |
|
41
|
|
|
$container->setParameter('logger.verbosityMap', $verbosityLevelMap); |
|
42
|
|
|
if ($input->hasOption('config-path') && !empty($input->getOption('config-path'))) { |
|
43
|
|
|
putenv('configurationPath=' . $input->getOption('config-path')); |
|
44
|
|
|
} |
|
45
|
|
|
if ($input->hasArgument('indexName')) { |
|
46
|
|
|
$container->setParameter('index.name', $input->getArgument('indexName')); |
|
47
|
|
|
} else { |
|
48
|
|
|
$container->setParameter('index.name', null); |
|
49
|
|
|
} |
|
50
|
|
|
if ($input->hasArgument('documentType')) { |
|
51
|
|
|
$container->setParameter('type.name', $input->getArgument('documentType')); |
|
52
|
|
|
$this->documentTypeService = $container->get('documentTypeService'); |
|
53
|
|
|
} |
|
54
|
|
|
$this->indexService = $container->get('indexService'); |
|
55
|
|
|
$this->configurationService = $container->get('configurationService'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function configure() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->addOption( |
|
61
|
|
|
'config-path', |
|
62
|
|
|
'c', |
|
63
|
|
|
InputArgument::OPTIONAL, |
|
64
|
|
|
'The full path to the configuration directory (may be relative)' |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
69
|
|
|
{ |
|
70
|
|
|
while (!(getenv('configurationPath') && file_exists(getenv('configurationPath')))) { |
|
71
|
|
|
$this->askForConfigDir($input, $output); |
|
72
|
|
|
} |
|
73
|
|
|
$path = rtrim(getenv('configurationPath'), DIRECTORY_SEPARATOR) . '/'; |
|
74
|
|
|
putenv('configurationPath=' . $path); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function askForConfigDir(InputInterface $input, OutputInterface $output) |
|
78
|
|
|
{ |
|
79
|
|
|
$helper = $this->getHelper('question'); |
|
80
|
|
|
$question = new Question('Please enter a valid path to your configuration directory:' . "\n"); |
|
81
|
|
|
|
|
82
|
|
|
$answer = $helper->ask($input, $output, $question); |
|
83
|
|
|
putenv('configurationPath=' . $answer); |
|
84
|
|
|
} |
|
85
|
|
|
} |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state