|
1
|
|
|
<?php |
|
2
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
3
|
|
|
namespace SetBased\Audit\Command; |
|
4
|
|
|
|
|
5
|
|
|
use SetBased\Stratum\Style\StratumStyle; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
10
|
|
|
use Symfony\Component\Console\Question\Question; |
|
11
|
|
|
|
|
12
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
13
|
|
|
/** |
|
14
|
|
|
* Command for editing data in config file. |
|
15
|
|
|
*/ |
|
16
|
|
|
class ConfigCommand extends BaseCommand |
|
17
|
|
|
{ |
|
18
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
19
|
|
|
/** |
|
20
|
|
|
* The helper instance by name. |
|
21
|
|
|
*/ |
|
22
|
|
|
private $helper; |
|
23
|
|
|
|
|
24
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
1 |
|
protected function configure() |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->setName('config') |
|
31
|
1 |
|
->setDescription('Create or edit config file') |
|
32
|
1 |
|
->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file', 'etc/audit.json'); |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->io = new StratumStyle($input, $output); |
|
42
|
|
|
|
|
43
|
|
|
$this->configFileName = $input->getArgument('config file'); |
|
44
|
|
|
$this->readConfigFile(); |
|
45
|
|
|
$this->configFileName = sprintf('%s%s', $input->getArgument('config file'), '.new'); |
|
46
|
|
|
|
|
47
|
|
|
$this->helper = $this->getHelper('question'); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($this->config as $configPart => $partData) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->setConfigPart($input, $output, $configPart); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->writeTwoPhases($this->configFileName, json_encode($this->config, JSON_PRETTY_PRINT)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
58
|
|
|
/** |
|
59
|
|
|
* Controls config parts. |
|
60
|
|
|
* |
|
61
|
|
|
* @param InputInterface $input |
|
62
|
|
|
* @param OutputInterface $output |
|
63
|
|
|
* @param string $configPart Part of config file. |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function setConfigPart(InputInterface $input, OutputInterface $output, $configPart) |
|
66
|
|
|
{ |
|
67
|
|
|
switch ($configPart) |
|
68
|
|
|
{ |
|
69
|
|
|
case 'database': |
|
70
|
|
|
$this->setDatabasePart($input, $output, $configPart); |
|
71
|
|
|
break; |
|
72
|
|
|
case 'tables': |
|
73
|
|
|
foreach ($this->config[$configPart] as $tableName => $tableData) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->setTableParams($input, $output, $configPart, $tableName); |
|
76
|
|
|
} |
|
77
|
|
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
82
|
|
|
/** |
|
83
|
|
|
* Configure each table for audit or not. |
|
84
|
|
|
* |
|
85
|
|
|
* @param InputInterface $input |
|
86
|
|
|
* @param OutputInterface $output |
|
87
|
|
|
* @param string $configPart Part of config file. |
|
88
|
|
|
* @param string $tableName The table name. |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function setTableParams(InputInterface $input, OutputInterface $output, $configPart, $tableName) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->io->logInfo('Please input data for table <dbo>\'%s\'</dbo>.', $tableName); |
|
93
|
|
|
$question = sprintf('Audit table \'%s\' or not (y|(n)): ', $tableName); |
|
94
|
|
|
$question = new ConfirmationQuestion($question, false); |
|
95
|
|
|
|
|
96
|
|
|
$this->config[$configPart][$tableName]['audit'] = $this->helper->ask($input, $output, $question); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
100
|
|
|
/** |
|
101
|
|
|
* Configure database part. |
|
102
|
|
|
* |
|
103
|
|
|
* @param InputInterface $input |
|
104
|
|
|
* @param OutputInterface $output |
|
105
|
|
|
* @param string $configPart Part of config file. |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function setDatabasePart(InputInterface $input, OutputInterface $output, $configPart) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->io->logInfo('Please input data for <dbo>\'%s\'</dbo> part.', $configPart); |
|
110
|
|
|
foreach ($this->config[$configPart] as $parameter => $value) |
|
111
|
|
|
{ |
|
112
|
|
|
$question = sprintf('Please enter the \'%s\' (%s): ', $parameter, $value); |
|
113
|
|
|
$question = new Question($question, $value); |
|
114
|
|
|
$this->config[$configPart][$parameter] = $this->helper->ask($input, $output, $question); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
122
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: