1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the hogosha-monitor package |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Feel free to edit as you please, and have fun. |
12
|
|
|
* |
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Hogosha\Monitor\Console\Handler; |
17
|
|
|
|
18
|
|
|
use Hogosha\Monitor\Configuration\ConfigurationDumper; |
19
|
|
|
use Hogosha\Monitor\Configuration\ConfigurationLoader; |
20
|
|
|
use Hogosha\Monitor\DependencyInjection\Exception\ConfigurationLoadingException; |
21
|
|
|
use Hogosha\Monitor\Monitor; |
22
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
23
|
|
|
use Webmozart\Console\Api\Args\Args; |
24
|
|
|
use Webmozart\Console\Api\IO\IO; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Guillaume Cavana <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class InitHandler |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* $configurationLoader. |
33
|
|
|
* |
34
|
|
|
* @var ConfigurationLoader |
35
|
|
|
*/ |
36
|
|
|
protected $configurationLoader; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* $configurationDumper. |
40
|
|
|
* |
41
|
|
|
* @var ConfigurationDumper |
42
|
|
|
*/ |
43
|
|
|
protected $configurationDumper; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @param ConfigurationLoader $configurationLoader |
49
|
|
|
* @param ConfigurationDumper $configurationDumper |
50
|
|
|
* @param Filesystem $filesystem |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
ConfigurationLoader $configurationLoader, |
54
|
|
|
ConfigurationDumper $configurationDumper, |
55
|
|
|
Filesystem $filesystem |
56
|
|
|
) { |
57
|
|
|
$this->configurationLoader = $configurationLoader; |
58
|
|
|
$this->configurationDumper = $configurationDumper; |
59
|
|
|
$this->filesystem = $filesystem; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* handle. |
64
|
|
|
* |
65
|
|
|
* @param Args $args |
66
|
|
|
* @param IO $io |
67
|
|
|
* |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
public function handle(Args $args, IO $io) |
71
|
|
|
{ |
72
|
|
|
$configFileExist = true; |
73
|
|
|
$overwrite = is_string($args->getOption('force')); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$this->configurationLoader->setRootDirectory($args->getOption('config')); |
77
|
|
|
$configuration = $this->configurationLoader->loadConfiguration(); |
|
|
|
|
78
|
|
|
} catch (ConfigurationLoadingException $e) { |
79
|
|
|
$configFileExist = false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!$configFileExist || $overwrite) { |
83
|
|
|
$configuration = [ |
84
|
|
|
'urls' => [ |
85
|
|
|
'google' => [ |
86
|
|
|
'url' => 'https://www.google.fr', |
87
|
|
|
'method' => 'GET', |
88
|
|
|
'headers' => [], |
89
|
|
|
'timeout' => 1, |
90
|
|
|
'validator' => [], |
91
|
|
|
'status_code' => 200, |
92
|
|
|
'metric_uuid' => null, |
93
|
|
|
'service_uuid' => null, |
94
|
|
|
], |
95
|
|
|
], |
96
|
|
|
'hogosha_portal' => [ |
97
|
|
|
'username' => '', |
98
|
|
|
'password' => '', |
99
|
|
|
'base_uri' => 'http://localhost:8000/api/', |
100
|
|
|
'metric_update' => false, |
101
|
|
|
'incident_update' => false, |
102
|
|
|
'default_failed_incident_message' => 'An error as occured, we are investigating %service_name%', |
103
|
|
|
'default_resolved_incident_message' => 'The service %service_name% is back to normal', |
104
|
|
|
], |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
// Dump configuration |
108
|
|
|
$content = $this->configurationDumper->dumpConfiguration($configuration); |
109
|
|
|
$this->filesystem->dumpFile( |
110
|
|
|
$this->configurationLoader->getConfigurationFilepath(), |
111
|
|
|
$content |
112
|
|
|
); |
113
|
|
|
$io->writeLine('<info>Creating monitor file</info>'); |
114
|
|
|
} else { |
115
|
|
|
$io->writeLine( |
116
|
|
|
sprintf( |
117
|
|
|
'<info>You already have a configuration file in</info> "%s"', |
118
|
|
|
$this->configurationLoader->getConfigurationFilepath() |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: