Skip to content

Commit 20b1721

Browse files
committed
allow different checks for dev/production modes
1 parent 3743d38 commit 20b1721

File tree

9 files changed

+305
-22
lines changed

9 files changed

+305
-22
lines changed

config/config.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
<?php
22

33
return [
4-
4+
5+
/*
6+
* List of all the environment names that are considered as "production".
7+
*/
8+
'productionEnvironments' => [
9+
'prod',
10+
'production',
11+
],
12+
13+
/*
14+
* Common checks that will be performed on all environments.
15+
*/
516
'checks' => [
617
\BeyondCode\SelfDiagnosis\Checks\AppKeyIsSet::class,
718
\BeyondCode\SelfDiagnosis\Checks\ComposerIsUpToDate::class,
@@ -13,6 +24,24 @@
1324
\BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreSet::class,
1425
\BeyondCode\SelfDiagnosis\Checks\DirectoriesHaveCorrectPermissions::class,
1526
\BeyondCode\SelfDiagnosis\Checks\StorageDirectoryIsLinked::class,
16-
]
27+
],
28+
29+
/*
30+
* Production environment specific checks.
31+
*/
32+
'production' => [
33+
\BeyondCode\SelfDiagnosis\Checks\Production\ConfigurationIsCached::class,
34+
\BeyondCode\SelfDiagnosis\Checks\Production\RoutesAreCached::class,
35+
\BeyondCode\SelfDiagnosis\Checks\Production\XDebugIsNotEnabled::class,
36+
\BeyondCode\SelfDiagnosis\Checks\Production\DebugModeIsNotEnabled::class,
37+
],
38+
39+
/*
40+
* Development environment specific checks.
41+
*/
42+
'development' => [
43+
\BeyondCode\SelfDiagnosis\Checks\Development\ConfigurationIsNotCached::class,
44+
\BeyondCode\SelfDiagnosis\Checks\Development\RoutesAreNotCached::class,
45+
],
1746

1847
];
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Checks\Development;
4+
5+
use BeyondCode\SelfDiagnosis\Checks\Check;
6+
7+
class ConfigurationIsNotCached implements Check
8+
{
9+
10+
/**
11+
* The name of the check.
12+
*
13+
* @return string
14+
*/
15+
public function name(): string
16+
{
17+
return 'Configuration is not cached';
18+
}
19+
20+
/**
21+
* Perform the actual verification of this check.
22+
*
23+
* @return bool
24+
*/
25+
public function check(): bool
26+
{
27+
return app()->configurationIsCached() === false;
28+
}
29+
30+
/**
31+
* The error message to display in case the check does not pass.
32+
*
33+
* @return string
34+
*/
35+
public function message(): string
36+
{
37+
return 'Your configuration files should not be cached during development. Call "php artisan config:clear" to clear the config cache.';
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Checks\Development;
4+
5+
use BeyondCode\SelfDiagnosis\Checks\Check;
6+
7+
class RoutesAreNotCached implements Check
8+
{
9+
10+
/**
11+
* The name of the check.
12+
*
13+
* @return string
14+
*/
15+
public function name(): string
16+
{
17+
return 'Routes are not cached';
18+
}
19+
20+
/**
21+
* Perform the actual verification of this check.
22+
*
23+
* @return bool
24+
*/
25+
public function check(): bool
26+
{
27+
return app()->routesAreCached() === false;
28+
}
29+
30+
/**
31+
* The error message to display in case the check does not pass.
32+
*
33+
* @return string
34+
*/
35+
public function message(): string
36+
{
37+
return 'Your routes should not be cached during development. Call "php artisan route:clear" to clear the route cache.';
38+
}
39+
}

src/Checks/MigrationsAreUpToDate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function name(): string
2626
public function check(): bool
2727
{
2828
try {
29-
Artisan::call('migrate', ['--pretend' => 'true']);
29+
Artisan::call('migrate', ['--pretend' => 'true', '--force' => 'true']);
3030
$output = Artisan::output();
3131
return strstr($output, 'Nothing to migrate.');
3232
} catch (\PDOException $e) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Checks\Production;
4+
5+
use BeyondCode\SelfDiagnosis\Checks\Check;
6+
7+
class ConfigurationIsCached implements Check
8+
{
9+
10+
/**
11+
* The name of the check.
12+
*
13+
* @return string
14+
*/
15+
public function name(): string
16+
{
17+
return 'Configuration is cached';
18+
}
19+
20+
/**
21+
* Perform the actual verification of this check.
22+
*
23+
* @return bool
24+
*/
25+
public function check(): bool
26+
{
27+
return app()->configurationIsCached() === true;
28+
}
29+
30+
/**
31+
* The error message to display in case the check does not pass.
32+
*
33+
* @return string
34+
*/
35+
public function message(): string
36+
{
37+
return 'Your configuration files should be cached in production. Call "php artisan config:cache" to cache the configuration.';
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Checks\Production;
4+
5+
use BeyondCode\SelfDiagnosis\Checks\Check;
6+
7+
class DebugModeIsNotEnabled implements Check
8+
{
9+
10+
/**
11+
* The name of the check.
12+
*
13+
* @return string
14+
*/
15+
public function name(): string
16+
{
17+
return 'Debug mode is not enabled';
18+
}
19+
20+
/**
21+
* Perform the actual verification of this check.
22+
*
23+
* @return bool
24+
*/
25+
public function check(): bool
26+
{
27+
return config('app.debug') === false;
28+
}
29+
30+
/**
31+
* The error message to display in case the check does not pass.
32+
*
33+
* @return string
34+
*/
35+
public function message(): string
36+
{
37+
return 'You should not use debug mode in production. Set APP_DEBUG to false.';
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Checks\Production;
4+
5+
use BeyondCode\SelfDiagnosis\Checks\Check;
6+
7+
class RoutesAreCached implements Check
8+
{
9+
10+
/**
11+
* The name of the check.
12+
*
13+
* @return string
14+
*/
15+
public function name(): string
16+
{
17+
return 'Routes are cached';
18+
}
19+
20+
/**
21+
* Perform the actual verification of this check.
22+
*
23+
* @return bool
24+
*/
25+
public function check(): bool
26+
{
27+
return app()->routesAreCached() === true;
28+
}
29+
30+
/**
31+
* The error message to display in case the check does not pass.
32+
*
33+
* @return string
34+
*/
35+
public function message(): string
36+
{
37+
return 'Your routes should be cached in production. Call "php artisan route:cache" to create the route cache.';
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Checks\Production;
4+
5+
use BeyondCode\SelfDiagnosis\Checks\Check;
6+
7+
class XDebugIsNotEnabled implements Check
8+
{
9+
10+
/**
11+
* The name of the check.
12+
*
13+
* @return string
14+
*/
15+
public function name(): string
16+
{
17+
return 'The xdebug extension is not active.';
18+
}
19+
20+
/**
21+
* Perform the actual verification of this check.
22+
*
23+
* @return bool
24+
*/
25+
public function check(): bool
26+
{
27+
return extension_loaded('xdebug') === true;
28+
}
29+
30+
/**
31+
* The error message to display in case the check does not pass.
32+
*
33+
* @return string
34+
*/
35+
public function message(): string
36+
{
37+
return 'You should not have the "xdebug" PHP extension activated in production.';
38+
}
39+
}

src/SelfDiagnosisCommand.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,63 @@ class SelfDiagnosisCommand extends Command
2121
*/
2222
protected $description = 'Perform application self diagnosis.';
2323

24+
private $messages = [];
25+
2426
public function handle()
2527
{
26-
$checks = config('self-diagnosis.checks');
28+
$this->runChecks(config('self-diagnosis.checks', []), 'Running Common Checks');
29+
30+
$environmentChecks = config('self-diagnosis.development', []);
31+
if (in_array(app()->environment(), config('self-diagnosis.productionEnvironments'))) {
32+
$environmentChecks = config('self-diagnosis.production', []);
33+
}
34+
35+
$this->runChecks($environmentChecks, 'Environment Specific Checks ('.app()->environment().')');
36+
37+
if (count($this->messages)) {
38+
$this->output->writeln('The following checks failed:');
39+
40+
foreach ($this->messages as $message) {
41+
$this->output->writeln('<fg=red>'.$message.'</fg=red>');
42+
$this->output->writeln('');
43+
}
44+
} else {
45+
$this->info('Good job, looks like you are all set up.');
46+
}
47+
}
2748

49+
protected function runChecks(array $checks, string $title)
50+
{
2851
$max = count($checks);
2952
$current = 1;
30-
$messages = [];
53+
54+
$this->output->writeln('|-------------------------------------');
55+
$this->output->writeln('| '.$title);
56+
$this->output->writeln('|-------------------------------------');
3157

3258
foreach ($checks as $check) {
33-
/** @var Check $checkClass */
3459
$checkClass = app($check);
3560

3661
$this->output->write("<fg=yellow>Running check {$current}/{$max}:</fg=yellow> {$checkClass->name()}... ");
3762

38-
if ($checkClass->check()) {
39-
$this->output->write('<fg=green>✔</fg=green>');
40-
} else {
41-
$this->output->write('<fg=red>✘</fg=red>');
42-
43-
$messages[] = $checkClass->message();
44-
}
63+
$this->runCheck($checkClass);
4564

46-
$this->output->write(PHP_EOL);
4765
$current++;
4866
}
4967

5068
$this->output->writeln('');
69+
}
5170

52-
if (count($messages)) {
53-
$this->output->writeln('The following checks failed:');
54-
55-
foreach ($messages as $message) {
56-
$this->output->writeln('<fg=red>'.$message.'</fg=red>');
57-
$this->output->writeln('');
58-
}
71+
protected function runCheck(Check $check)
72+
{
73+
if ($check->check()) {
74+
$this->output->write('<fg=green>✔</fg=green>');
5975
} else {
60-
$this->info('Good job, looks like you are all set up.');
76+
$this->output->write('<fg=red>✘</fg=red>');
77+
78+
$this->messages[] = $check->message();
6179
}
80+
81+
$this->output->write(PHP_EOL);
6282
}
6383
}

0 commit comments

Comments
 (0)