1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Storeman\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Storeman\Cli\ConflictHandler\ConsolePromptConflictHandler; |
6
|
|
|
use Storeman\Cli\ConsoleLogger; |
7
|
|
|
use Storeman\Cli\ConsoleStyle; |
8
|
|
|
use Storeman\Config\Configuration; |
9
|
|
|
use Storeman\Config\ConfigurationFileReader; |
10
|
|
|
use Storeman\Container; |
11
|
|
|
use Storeman\PathUtils; |
12
|
|
|
use Storeman\Storeman; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Abstract class which provides a storeman instance to the concrete command which is already configured adequately |
20
|
|
|
* to run in a CLI environment. |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractCommand extends Command |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var InputInterface |
26
|
|
|
*/ |
27
|
|
|
protected $input; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var OutputInterface |
31
|
|
|
*/ |
32
|
|
|
protected $output; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ConsoleStyle |
36
|
|
|
*/ |
37
|
|
|
protected $consoleStyle; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected function configure() |
43
|
|
|
{ |
44
|
|
|
parent::configure(); |
45
|
|
|
|
46
|
|
|
$defaultConfigFilePath = sprintf('./%s', Storeman::CONFIG_FILE_NAME); |
47
|
|
|
|
48
|
|
|
$this->addOption('config', 'c', InputOption::VALUE_REQUIRED, "Path to configuration file. Defaults to \"{$defaultConfigFilePath}\".", $defaultConfigFilePath); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
55
|
|
|
{ |
56
|
|
|
$this->setUpIO($input, $output); |
57
|
|
|
|
58
|
|
|
$container = $this->getContainer($output); |
59
|
|
|
$config = $this->getConfiguration($container, $input); |
60
|
|
|
|
61
|
|
|
if ($config === null) |
62
|
|
|
{ |
63
|
|
|
$output->writeln('<error>This does not seem to be an archive!</error>'); |
64
|
|
|
|
65
|
|
|
return 1; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$storeman = new Storeman($container->injectConfiguration($config)); |
69
|
|
|
|
70
|
|
|
return $this->executeConfigured($input, $output, $storeman); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function executeConfigured(InputInterface $input, OutputInterface $output, Storeman $storeman): int |
74
|
|
|
{ |
75
|
|
|
throw new \LogicException(sprintf('Either %s::execute() or %s::executeConfigured() has to be implemented.', __CLASS__, __CLASS__)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function setUpIO(InputInterface $input, OutputInterface $output): void |
79
|
|
|
{ |
80
|
|
|
$this->input = $input; |
81
|
|
|
$this->output = $output; |
82
|
|
|
$this->consoleStyle = new ConsoleStyle($input, $output); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Builds and returns container to be used in a CLI context. |
87
|
|
|
* |
88
|
|
|
* @param OutputInterface $output |
89
|
|
|
* @return Container |
90
|
|
|
*/ |
91
|
|
|
protected function getContainer(OutputInterface $output): Container |
92
|
|
|
{ |
93
|
|
|
$container = new Container(); |
94
|
|
|
|
95
|
|
|
$container->setLogger(new ConsoleLogger($output)); |
96
|
|
|
$container->addConflictHandler('consolePrompt', ConsolePromptConflictHandler::class)->withArgument($this->consoleStyle); |
97
|
|
|
|
98
|
|
|
return $container; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Tries to read in the archive configuration either from the default path or a user provided one. |
103
|
|
|
* |
104
|
|
|
* @param Container $container |
105
|
|
|
* @param InputInterface $input |
106
|
|
|
* @return Configuration |
107
|
|
|
*/ |
108
|
|
|
protected function getConfiguration(Container $container, InputInterface $input): ?Configuration |
109
|
|
|
{ |
110
|
|
|
if ($configFilePath = $input->getOption('config')) |
111
|
|
|
{ |
112
|
|
|
$configFilePath = PathUtils::expandTilde($configFilePath); |
113
|
|
|
|
114
|
|
|
if (is_dir($configFilePath)) |
115
|
|
|
{ |
116
|
|
|
$configFilePath = "{$configFilePath}/" . Storeman::CONFIG_FILE_NAME; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (is_file($configFilePath)) |
120
|
|
|
{ |
121
|
|
|
/** @var ConfigurationFileReader $reader */ |
122
|
|
|
$reader = $container->get('configurationFileReader'); |
123
|
|
|
|
124
|
|
|
assert($reader instanceof ConfigurationFileReader); |
125
|
|
|
|
126
|
|
|
$configurationDefaults = [ |
127
|
|
|
'path' => dirname($configFilePath), |
128
|
|
|
'identity' => sprintf('%s@%s', get_current_user(), gethostname()), |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
$vaultConfigurationDefaults = [ |
132
|
|
|
'conflictHandler' => 'consolePrompt', |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
return $reader->getConfiguration($configFilePath, $configurationDefaults, $vaultConfigurationDefaults); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return null; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|