|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hogosha\Monitor\Console\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Hogosha\Monitor\Configuration\ConfigurationDumper; |
|
6
|
|
|
use Hogosha\Monitor\Configuration\ConfigurationLoader; |
|
7
|
|
|
use Hogosha\Monitor\DependencyInjection\Exception\ConfigurationLoadingException; |
|
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
9
|
|
|
use Webmozart\Console\Api\Args\Args; |
|
10
|
|
|
use Webmozart\Console\Api\Command\Command; |
|
11
|
|
|
use Webmozart\Console\Api\IO\IO; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class InitHandler |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* $configurationLoader. |
|
20
|
|
|
* @var ConfigurationLoader |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $configurationLoader; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* $configurationDumper. |
|
26
|
|
|
* @var ConfigurationDumper |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $configurationDumper; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor. |
|
32
|
|
|
* @param ConfigurationLoader $configurationLoader |
|
33
|
|
|
* @param ConfigurationDumper $configurationDumper |
|
34
|
|
|
* @param Filesystem $filesystem |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
|
|
ConfigurationLoader $configurationLoader, |
|
38
|
|
|
ConfigurationDumper $configurationDumper, |
|
39
|
|
|
Filesystem $filesystem |
|
40
|
|
|
) { |
|
41
|
|
|
$this->configurationLoader = $configurationLoader; |
|
42
|
|
|
$this->configurationDumper = $configurationDumper; |
|
43
|
|
|
$this->filesystem = $filesystem; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* handle. |
|
48
|
|
|
* @param Args $args |
|
49
|
|
|
* @param IO $io |
|
50
|
|
|
* @param Command $command |
|
51
|
|
|
* @return integer |
|
52
|
|
|
*/ |
|
53
|
|
|
public function handle(Args $args, IO $io, Command $command) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
|
|
$configuration = $this->configurationLoader->loadConfiguration(); |
|
57
|
|
|
} catch (ConfigurationLoadingException $e) { |
|
58
|
|
|
$configuration = [ |
|
59
|
|
|
'server' => [ |
|
60
|
|
|
'hostname' => null, |
|
61
|
|
|
'port' => null, |
|
62
|
|
|
'use_ssl' => null, |
|
63
|
|
|
'auth' => [ |
|
64
|
|
|
'username' => null, |
|
65
|
|
|
'password' => null, |
|
66
|
|
|
], |
|
67
|
|
|
], |
|
68
|
|
|
'urls' => [ |
|
69
|
|
|
'google' => [ |
|
70
|
|
|
'url' => 'https://www.google.fr', |
|
71
|
|
|
'timeout' => 1, |
|
72
|
|
|
'status_code' => 200, |
|
73
|
|
|
'service_uid' => '', |
|
74
|
|
|
], |
|
75
|
|
|
], |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Dump configuration |
|
80
|
|
|
$content = $this->configurationDumper->dumpConfiguration($configuration); |
|
81
|
|
|
|
|
82
|
|
|
$this->filesystem->dumpFile( |
|
83
|
|
|
$this->configurationLoader->getConfigurationFilepath(), |
|
84
|
|
|
$content |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
$io->writeLine('<info>Creating monitor file</info>'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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: