1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\console\commands; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\FileHelper; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Health/Status informations about the Application itself. |
10
|
|
|
* |
11
|
|
|
* @author Basil Suter <[email protected]> |
12
|
|
|
* @since 1.0.0 |
13
|
|
|
*/ |
14
|
|
|
class HealthController extends \luya\console\Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array An array with all folders to check where the value for the key is whether it should be writeable or not. |
18
|
|
|
*/ |
19
|
|
|
public $folders = [ |
20
|
|
|
'public_html/assets' => true, |
21
|
|
|
'public_html/storage' => true, |
22
|
|
|
'migrations' => false, |
23
|
|
|
'vendor' => false, |
24
|
|
|
'runtime' => true, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array An array with files to check if they exists or not. |
29
|
|
|
*/ |
30
|
|
|
public $files = [ |
31
|
|
|
'configs/env.php', |
32
|
|
|
'public_html/index.php', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create all required directories an check whether they are writeable or not. |
37
|
|
|
* |
38
|
|
|
* @return string The action output. |
39
|
|
|
*/ |
40
|
|
|
public function actionIndex() |
41
|
|
|
{ |
42
|
|
|
// opcache reset should run in web context, but might be helpfull as well. |
43
|
|
|
if (function_exists('opcache_reset')) { |
44
|
|
|
@opcache_reset(); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$error = false; |
48
|
|
|
|
49
|
|
|
@chdir(Yii::getAlias('@app')); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->output('The directory the health commands is applying to: ' . Yii::getAlias('@app')); |
52
|
|
|
|
53
|
|
|
foreach ($this->folders as $folder => $writable) { |
54
|
|
|
$mode = ($writable) ? 0777 : 0775; |
55
|
|
|
if (!file_exists($folder)) { |
56
|
|
|
if (FileHelper::createDirectory($folder, $mode)) { |
57
|
|
|
$this->outputSuccess("$folder: successfully created directory"); |
58
|
|
|
} else { |
59
|
|
|
$error = true; |
60
|
|
|
$this->outputError("$folder: unable to create directory"); |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
$this->outputInfo("$folder: directory exists already"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($writable && !is_writable($folder)) { |
67
|
|
|
$this->outputInfo("$folder: is not writeable, try to set mode '$mode'."); |
68
|
|
|
@chmod($folder, $mode); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($writable) { |
72
|
|
|
if (!is_writable($folder)) { |
73
|
|
|
$error = true; |
74
|
|
|
$this->outputError("$folder: is not writable, please change permissions."); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
foreach ($this->files as $file) { |
80
|
|
|
if (file_exists($file)) { |
81
|
|
|
$this->outputInfo("$file: file exists."); |
82
|
|
|
} else { |
83
|
|
|
$error = true; |
84
|
|
|
$this->outputError("$file: file does not exists!"); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $error ? $this->outputError('Health check found errors!') : $this->outputSuccess('O.K.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test Mail-Component (Use --verbose=1 to enable smtp debug output) |
93
|
|
|
* |
94
|
|
|
* @return boolean Whether successfull or not. |
95
|
|
|
* @throws \Exception On smtp failure |
96
|
|
|
*/ |
97
|
|
|
public function actionMailer() |
98
|
|
|
{ |
99
|
|
|
try { |
100
|
|
|
if (Yii::$app->mail->smtpTest($this->verbose)) { |
101
|
|
|
return $this->outputSuccess("successfull connected to SMTP Server '".Yii::$app->mail->host."'"); |
102
|
|
|
} |
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
return $this->outputError($e->getMessage()); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: