1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\JobQueue\Console\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command as BaseCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\Question; |
11
|
|
|
use Tarantool\JobQueue\DefaultConfigFactory; |
12
|
|
|
|
13
|
|
|
class Command extends BaseCommand |
14
|
|
|
{ |
15
|
|
|
private const DEFAULT_HOST = '127.0.0.1'; |
16
|
|
|
private const DEFAULT_PORT = 3301; |
17
|
|
|
private const ENV_USER = 'TNT_JOBQUEUE_USER'; |
18
|
|
|
private const ENV_PASSWORD = 'TNT_JOBQUEUE_PASSWORD'; |
19
|
|
|
|
20
|
|
|
protected function configure(): void |
21
|
|
|
{ |
22
|
|
|
$this |
23
|
|
|
->addArgument('queue', InputArgument::REQUIRED) |
24
|
|
|
->addOption('host', 'H', InputOption::VALUE_REQUIRED, '', self::DEFAULT_HOST) |
25
|
|
|
->addOption('port', 'p', InputOption::VALUE_REQUIRED, '', self::DEFAULT_PORT) |
26
|
|
|
->addOption('socket', 's', InputOption::VALUE_REQUIRED) |
27
|
|
|
->addOption('user', 'u', InputOption::VALUE_REQUIRED) |
28
|
|
|
->addOption('config', 'c', InputOption::VALUE_REQUIRED) |
29
|
|
|
; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
final protected function createConfigFactory(InputInterface $input, OutputInterface $output): DefaultConfigFactory |
33
|
|
|
{ |
34
|
|
|
$customConfigPath = $input->getOption('config'); |
35
|
|
|
if (null !== $customConfigPath && !is_readable($customConfigPath)) { |
|
|
|
|
36
|
|
|
throw new \RuntimeException("The given configuration file '$customConfigPath' does not exist or it's not readable."); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$configFactory = $customConfigPath ? include $customConfigPath : new DefaultConfigFactory(); |
40
|
|
|
|
41
|
|
|
if (null !== $socket = $input->getOption('socket')) { |
42
|
|
|
$configFactory->setConnectionUri("unix://$socket"); |
43
|
|
|
} elseif ((false !== $input->getParameterOption('--host')) || !$configFactory->getConnectionUri()) { |
44
|
|
|
$uri = sprintf('tcp://%s:%s', $input->getOption('host'), $input->getOption('port')); |
|
|
|
|
45
|
|
|
$configFactory->setConnectionUri($uri); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$queueName = $input->getArgument('queue'); |
49
|
|
|
$configFactory->setQueueName($queueName); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$user = $input->getOption('user') ?: getenv(self::ENV_USER); |
52
|
|
|
if ($user) { |
53
|
|
|
if (!$password = getenv(self::ENV_PASSWORD)) { |
54
|
|
|
$helper = $this->getHelper('question'); |
55
|
|
|
$question = new Question('Password: '); |
56
|
|
|
$question->setHidden(true); |
57
|
|
|
$question->setHiddenFallback(false); |
58
|
|
|
$password = $helper->ask($input, $output, $question); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$configFactory->setCredentials($user, $password); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $configFactory; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|