|
1
|
|
|
<?php |
|
2
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
3
|
|
|
namespace SetBased\Audit\MySql\Command; |
|
4
|
|
|
|
|
5
|
|
|
use SetBased\Audit\MySql\Audit; |
|
6
|
|
|
use SetBased\Audit\MySql\AuditDataLayer; |
|
7
|
|
|
use SetBased\Exception\RuntimeException; |
|
8
|
|
|
use SetBased\Stratum\Style\StratumStyle; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
14
|
|
|
/** |
|
15
|
|
|
* Command for creating audit tables and audit triggers. |
|
16
|
|
|
*/ |
|
17
|
|
|
class AuditCommand extends MySqlBaseCommand |
|
18
|
|
|
{ |
|
19
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
20
|
|
|
/** |
|
21
|
|
|
* Tables metadata from config file. |
|
22
|
14 |
|
* |
|
23
|
|
|
* @var array |
|
24
|
14 |
|
*/ |
|
25
|
14 |
|
protected $configMetadata; |
|
26
|
14 |
|
|
|
27
|
14 |
|
/** |
|
28
|
|
|
* File name for config metadata. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $configMetadataFile; |
|
33
|
13 |
|
|
|
34
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
35
|
13 |
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
13 |
|
*/ |
|
38
|
13 |
|
protected function configure() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->setName('audit') |
|
41
|
13 |
|
->setDescription('Create (missing) audit table and (re)creates audit triggers') |
|
42
|
|
|
->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file'); |
|
43
|
13 |
|
} |
|
44
|
13 |
|
|
|
45
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
46
|
|
|
/** |
|
47
|
13 |
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
13 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
50
|
|
|
{ |
|
51
|
13 |
|
$this->io = new StratumStyle($input, $output); |
|
52
|
|
|
|
|
53
|
|
|
$this->configFileName = $input->getArgument('config file'); |
|
54
|
|
|
$this->readConfigFile(); |
|
55
|
|
|
|
|
56
|
|
|
$this->readMetadata(); |
|
57
|
|
|
|
|
58
|
|
|
// Create database connection with params from config file |
|
59
|
|
|
$this->connect($this->config); |
|
60
|
|
|
|
|
61
|
|
|
$audit = new Audit($this->config, $this->configMetadata, $this->io); |
|
62
|
|
|
$status = $audit->main(); |
|
63
|
|
|
|
|
64
|
|
|
// Drop database connection |
|
65
|
|
|
AuditDataLayer::disconnect(); |
|
66
|
|
|
|
|
67
|
|
|
$this->rewriteConfig(); |
|
68
|
|
|
|
|
69
|
|
|
return $status; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
73
|
|
|
/** |
|
74
|
|
|
* Rewrites the config file with updated data. |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function rewriteConfig() |
|
77
|
|
|
{ |
|
78
|
|
|
// Return immediately when the config file must not be rewritten. |
|
79
|
|
|
if (!$this->rewriteConfigFile) return; |
|
80
|
|
|
|
|
81
|
|
|
$this->writeTwoPhases($this->configFileName, json_encode($this->config, JSON_PRETTY_PRINT)); |
|
82
|
|
|
$this->writeTwoPhases($this->configMetadataFile, json_encode($this->configMetadata, JSON_PRETTY_PRINT)); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
86
|
|
|
/** |
|
87
|
|
|
* Read tables metadata from config file. |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function readMetadata() |
|
90
|
|
|
{ |
|
91
|
|
|
if (isset($this->config['metadata'])) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->configMetadataFile = $this->config['metadata']; |
|
94
|
|
|
$content = file_get_contents($this->configMetadataFile); |
|
95
|
|
|
|
|
96
|
|
|
$this->configMetadata = (array)json_decode($content, true); |
|
97
|
|
|
if (json_last_error()!=JSON_ERROR_NONE) |
|
98
|
|
|
{ |
|
99
|
|
|
throw new RuntimeException("Error decoding JSON: '%s'.", json_last_error_msg()); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
108
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: