Skip to content

Commit 1ed735a

Browse files
authored
Merge pull request #28 from Namoshek/extract-check-configuration-to-config
Extract some check configuration to the improved package configuration
2 parents 9f68cbf + 48446ee commit 1ed735a

File tree

8 files changed

+136
-66
lines changed

8 files changed

+136
-66
lines changed

README.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ return [
7575
'environment_aliases' => [
7676
'prod' => 'production',
7777
'live' => 'production',
78+
'local' => 'development',
7879
],
7980

8081
/*
@@ -83,12 +84,31 @@ return [
8384
'checks' => [
8485
\BeyondCode\SelfDiagnosis\Checks\AppKeyIsSet::class,
8586
\BeyondCode\SelfDiagnosis\Checks\CorrectPhpVersionIsInstalled::class,
86-
\BeyondCode\SelfDiagnosis\Checks\DatabaseCanBeAccessed::class,
87-
\BeyondCode\SelfDiagnosis\Checks\DirectoriesHaveCorrectPermissions::class,
87+
\BeyondCode\SelfDiagnosis\Checks\DatabaseCanBeAccessed::class => [
88+
'default_connection' => true,
89+
'connections' => [],
90+
],
91+
\BeyondCode\SelfDiagnosis\Checks\DirectoriesHaveCorrectPermissions::class => [
92+
'directories' => [
93+
storage_path(),
94+
base_path('bootstrap/cache'),
95+
],
96+
],
8897
\BeyondCode\SelfDiagnosis\Checks\EnvFileExists::class,
8998
\BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreSet::class,
9099
\BeyondCode\SelfDiagnosis\Checks\MigrationsAreUpToDate::class,
91-
\BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreInstalled::class,
100+
\BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreInstalled::class => [
101+
'extensions' => [
102+
'openssl',
103+
'PDO',
104+
'mbstring',
105+
'tokenizer',
106+
'xml',
107+
'ctype',
108+
'json',
109+
],
110+
'include_composer_extensions' => true,
111+
],
92112
\BeyondCode\SelfDiagnosis\Checks\StorageDirectoryIsLinked::class,
93113
],
94114

@@ -105,14 +125,33 @@ return [
105125
\BeyondCode\SelfDiagnosis\Checks\ComposerWithoutDevDependenciesIsUpToDate::class,
106126
\BeyondCode\SelfDiagnosis\Checks\ConfigurationIsCached::class,
107127
\BeyondCode\SelfDiagnosis\Checks\DebugModeIsNotEnabled::class,
128+
\BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreDisabled::class => [
129+
'extensions' => [
130+
'xdebug',
131+
],
132+
],
108133
\BeyondCode\SelfDiagnosis\Checks\RoutesAreCached::class,
109-
\BeyondCode\SelfDiagnosis\Checks\XDebugIsNotEnabled::class,
110134
],
111135
],
112136

113137
];
114138
```
115139

140+
#### Available Configuration Options
141+
142+
The following options are available for the individual checks:
143+
144+
- [`BeyondCode\SelfDiagnosis\Checks\DatabaseCanBeAccessed`](src/Checks/DatabaseCanBeAccessed.php)
145+
- **default_connection** *(boolean, default: `true`)*: if the default connection should be checked
146+
- **connections** *(array, list of connection names like `['mysql', 'sqlsrv']`, default: `[]`)*: additional connections to check
147+
- [`BeyondCode\SelfDiagnosis\Checks\DirectoriesHaveCorrectPermissions`](src/Checks/DirectoriesHaveCorrectPermissions.php)
148+
- **directories** *(array, list of directory paths like `[storage_path(), base_path('bootstrap/cache')]`, default: `[]`)*: directories to check
149+
- [`BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreDisabled`](src/Checks/PhpExtensionsAreDisabled.php)
150+
- **extensions** *(array, list of extension names like `['xdebug', 'zlib']`, default: `[]`)*: extensions to check
151+
- [`BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreInstalled`](src/Checks/PhpExtensionsAreInstalled.php)
152+
- **extensions** *(array, list of extension names like `['openssl', 'PDO']`, default: `[]`)*: extensions to check
153+
- **include_composer_extensions** *(boolean, default: `false`)*: if required extensions defined in `composer.json` should be checked
154+
116155
### Custom Checks
117156

118157
You can create custom checks, by implementing the [`BeyondCode\SelfDiagnosis\Checks\Check`](src/Checks/Check.php) interface and adding the class to the config file.

config/config.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
'checks' => [
1818
\BeyondCode\SelfDiagnosis\Checks\AppKeyIsSet::class,
1919
\BeyondCode\SelfDiagnosis\Checks\CorrectPhpVersionIsInstalled::class,
20-
\BeyondCode\SelfDiagnosis\Checks\DatabaseCanBeAccessed::class,
20+
\BeyondCode\SelfDiagnosis\Checks\DatabaseCanBeAccessed::class => [
21+
'default_connection' => true,
22+
'connections' => [],
23+
],
2124
\BeyondCode\SelfDiagnosis\Checks\DirectoriesHaveCorrectPermissions::class => [
2225
'directories' => [
2326
storage_path(),
@@ -27,7 +30,18 @@
2730
\BeyondCode\SelfDiagnosis\Checks\EnvFileExists::class,
2831
\BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreSet::class,
2932
\BeyondCode\SelfDiagnosis\Checks\MigrationsAreUpToDate::class,
30-
\BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreInstalled::class,
33+
\BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreInstalled::class => [
34+
'extensions' => [
35+
'openssl',
36+
'PDO',
37+
'mbstring',
38+
'tokenizer',
39+
'xml',
40+
'ctype',
41+
'json',
42+
],
43+
'include_composer_extensions' => true,
44+
],
3145
\BeyondCode\SelfDiagnosis\Checks\StorageDirectoryIsLinked::class,
3246
],
3347

@@ -44,8 +58,12 @@
4458
\BeyondCode\SelfDiagnosis\Checks\ComposerWithoutDevDependenciesIsUpToDate::class,
4559
\BeyondCode\SelfDiagnosis\Checks\ConfigurationIsCached::class,
4660
\BeyondCode\SelfDiagnosis\Checks\DebugModeIsNotEnabled::class,
61+
\BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreDisabled::class => [
62+
'extensions' => [
63+
'xdebug',
64+
],
65+
],
4766
\BeyondCode\SelfDiagnosis\Checks\RoutesAreCached::class,
48-
\BeyondCode\SelfDiagnosis\Checks\XDebugIsNotEnabled::class,
4967
],
5068
],
5169

src/Checks/DatabaseCanBeAccessed.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ public function name(array $config): string
2828
public function check(array $config): bool
2929
{
3030
try {
31-
DB::connection()->getPdo();
31+
if (array_get($config, 'default_connection', true)) {
32+
DB::connection()->getPdo();
33+
}
34+
35+
foreach (array_get($config, 'connections', []) as $connection) {
36+
DB::connection($connection)->getPdo();
37+
}
3238

3339
return true;
3440
} catch (\Exception $e) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Checks;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class PhpExtensionsAreDisabled implements Check
8+
{
9+
10+
/** @var Collection */
11+
private $extensions;
12+
13+
/**
14+
* The name of the check.
15+
*
16+
* @param array $config
17+
* @return string
18+
*/
19+
public function name(array $config): string
20+
{
21+
return trans('self-diagnosis::checks.php_extensions_are_disabled.name');
22+
}
23+
24+
/**
25+
* Perform the actual verification of this check.
26+
*
27+
* @param array $config
28+
* @return bool
29+
*/
30+
public function check(array $config): bool
31+
{
32+
$this->extensions = Collection::make(array_get($config, 'extensions', []));
33+
$this->extensions = $this->extensions->reject(function ($ext) {
34+
return extension_loaded($ext) === false;
35+
});
36+
37+
return $this->extensions->isEmpty();
38+
}
39+
40+
/**
41+
* The error message to display in case the check does not pass.
42+
*
43+
* @param array $config
44+
* @return string
45+
*/
46+
public function message(array $config): string
47+
{
48+
return trans('self-dianosis::checks.php_extensions_are_disabled.message', [
49+
'extensions' => $this->extensions->implode(PHP_EOL),
50+
]);
51+
}
52+
}

src/Checks/PhpExtensionsAreInstalled.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,11 @@ public function message(array $config): string
5353
*/
5454
public function check(array $config): bool
5555
{
56-
$this->extensions = Collection::make([
57-
'openssl',
58-
'PDO',
59-
'mbstring',
60-
'tokenizer',
61-
'xml',
62-
'ctype',
63-
'json'
64-
]);
65-
$this->extensions = $this->extensions->merge($this->getExtensionsRequiredInComposerFile());
66-
$this->extensions = $this->extensions->unique();
56+
$this->extensions = Collection::make(array_get($config, 'extensions', []));
57+
if (array_get($config, 'include_composer_extensions', false)) {
58+
$this->extensions = $this->extensions->merge($this->getExtensionsRequiredInComposerFile());
59+
$this->extensions = $this->extensions->unique();
60+
}
6761
$this->extensions = $this->extensions->reject(function ($ext) {
6862
return extension_loaded($ext);
6963
});

src/Checks/XDebugIsNotEnabled.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

translations/de/checks.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
],
5353
'name' => 'Die Migrationen sind aktuell',
5454
],
55+
'php_extensions_are_disabled' => [
56+
'message' => 'Die folgenden Erweiterungen sind noch immer aktiviert:' . PHP_EOL . ':extensions',
57+
'name' => 'Unerwünschte PHP Erweiterungen sind deaktiviert',
58+
],
5559
'php_extensions_are_installed' => [
5660
'message' => 'Die folgenden Erweiterungen fehlen:' . PHP_EOL . ':extensions',
5761
'name' => 'Die benötigten PHP Erweiterungen sind installiert',
@@ -68,8 +72,4 @@
6872
'message' => 'Das Speicherverzeichnis ist nicht verlinkt. Nutze "php artisan storage:link", um eine symbolische Verknüpfung zu erstellen.',
6973
'name' => 'Das Speicherverzeichnis ist verlinkt',
7074
],
71-
'xdebug_is_not_enabled' => [
72-
'message' => 'Die "xdebug" PHP Erweiterung sollte im Produktivbetrieb nicht aktiviert sein.',
73-
'name' => 'Die xdebug PHP Erweiterung ist deaktiviert',
74-
],
7575
];

translations/en/checks.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
],
5353
'name' => 'The migrations are up to date',
5454
],
55+
'php_extensions_are_disabled' => [
56+
'message' => 'The following extensions are still enabled:' . PHP_EOL . ':extensions',
57+
'name' => 'Unwanted PHP extensions are disabled',
58+
],
5559
'php_extensions_are_installed' => [
5660
'message' => 'The following extensions are missing:' . PHP_EOL . ':extensions',
5761
'name' => 'The required PHP extensions are installed',
@@ -68,8 +72,4 @@
6872
'message' => 'The storage directory is not linked. Use "php artisan storage:link" to create a symbolic link.',
6973
'name' => 'The storage directory is linked',
7074
],
71-
'xdebug_is_not_enabled' => [
72-
'message' => 'You should not have the "xdebug" PHP extension enabled in production.',
73-
'name' => 'The xdebug extension is disabled',
74-
],
7575
];

0 commit comments

Comments
 (0)