Skip to content

Commit

Permalink
allow different checks for dev/production modes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Jul 5, 2018
1 parent 3743d38 commit 20b1721
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 22 deletions.
33 changes: 31 additions & 2 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<?php

return [


/*
* List of all the environment names that are considered as "production".
*/
'productionEnvironments' => [
'prod',
'production',
],

/*
* Common checks that will be performed on all environments.
*/
'checks' => [
\BeyondCode\SelfDiagnosis\Checks\AppKeyIsSet::class,
\BeyondCode\SelfDiagnosis\Checks\ComposerIsUpToDate::class,
Expand All @@ -13,6 +24,24 @@
\BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreSet::class,
\BeyondCode\SelfDiagnosis\Checks\DirectoriesHaveCorrectPermissions::class,
\BeyondCode\SelfDiagnosis\Checks\StorageDirectoryIsLinked::class,
]
],

/*
* Production environment specific checks.
*/
'production' => [
\BeyondCode\SelfDiagnosis\Checks\Production\ConfigurationIsCached::class,
\BeyondCode\SelfDiagnosis\Checks\Production\RoutesAreCached::class,
\BeyondCode\SelfDiagnosis\Checks\Production\XDebugIsNotEnabled::class,
\BeyondCode\SelfDiagnosis\Checks\Production\DebugModeIsNotEnabled::class,
],

/*
* Development environment specific checks.
*/
'development' => [
\BeyondCode\SelfDiagnosis\Checks\Development\ConfigurationIsNotCached::class,
\BeyondCode\SelfDiagnosis\Checks\Development\RoutesAreNotCached::class,
],

];
39 changes: 39 additions & 0 deletions src/Checks/Development/ConfigurationIsNotCached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace BeyondCode\SelfDiagnosis\Checks\Development;

use BeyondCode\SelfDiagnosis\Checks\Check;

class ConfigurationIsNotCached implements Check
{

/**
* The name of the check.
*
* @return string
*/
public function name(): string
{
return 'Configuration is not cached';
}

/**
* Perform the actual verification of this check.
*
* @return bool
*/
public function check(): bool
{
return app()->configurationIsCached() === false;
}

/**
* The error message to display in case the check does not pass.
*
* @return string
*/
public function message(): string
{
return 'Your configuration files should not be cached during development. Call "php artisan config:clear" to clear the config cache.';
}
}
39 changes: 39 additions & 0 deletions src/Checks/Development/RoutesAreNotCached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace BeyondCode\SelfDiagnosis\Checks\Development;

use BeyondCode\SelfDiagnosis\Checks\Check;

class RoutesAreNotCached implements Check
{

/**
* The name of the check.
*
* @return string
*/
public function name(): string
{
return 'Routes are not cached';
}

/**
* Perform the actual verification of this check.
*
* @return bool
*/
public function check(): bool
{
return app()->routesAreCached() === false;
}

/**
* The error message to display in case the check does not pass.
*
* @return string
*/
public function message(): string
{
return 'Your routes should not be cached during development. Call "php artisan route:clear" to clear the route cache.';
}
}
2 changes: 1 addition & 1 deletion src/Checks/MigrationsAreUpToDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function name(): string
public function check(): bool
{
try {
Artisan::call('migrate', ['--pretend' => 'true']);
Artisan::call('migrate', ['--pretend' => 'true', '--force' => 'true']);
$output = Artisan::output();
return strstr($output, 'Nothing to migrate.');
} catch (\PDOException $e) {
Expand Down
39 changes: 39 additions & 0 deletions src/Checks/Production/ConfigurationIsCached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace BeyondCode\SelfDiagnosis\Checks\Production;

use BeyondCode\SelfDiagnosis\Checks\Check;

class ConfigurationIsCached implements Check
{

/**
* The name of the check.
*
* @return string
*/
public function name(): string
{
return 'Configuration is cached';
}

/**
* Perform the actual verification of this check.
*
* @return bool
*/
public function check(): bool
{
return app()->configurationIsCached() === true;
}

/**
* The error message to display in case the check does not pass.
*
* @return string
*/
public function message(): string
{
return 'Your configuration files should be cached in production. Call "php artisan config:cache" to cache the configuration.';
}
}
39 changes: 39 additions & 0 deletions src/Checks/Production/DebugModeIsNotEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace BeyondCode\SelfDiagnosis\Checks\Production;

use BeyondCode\SelfDiagnosis\Checks\Check;

class DebugModeIsNotEnabled implements Check
{

/**
* The name of the check.
*
* @return string
*/
public function name(): string
{
return 'Debug mode is not enabled';
}

/**
* Perform the actual verification of this check.
*
* @return bool
*/
public function check(): bool
{
return config('app.debug') === false;
}

/**
* The error message to display in case the check does not pass.
*
* @return string
*/
public function message(): string
{
return 'You should not use debug mode in production. Set APP_DEBUG to false.';
}
}
39 changes: 39 additions & 0 deletions src/Checks/Production/RoutesAreCached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace BeyondCode\SelfDiagnosis\Checks\Production;

use BeyondCode\SelfDiagnosis\Checks\Check;

class RoutesAreCached implements Check
{

/**
* The name of the check.
*
* @return string
*/
public function name(): string
{
return 'Routes are cached';
}

/**
* Perform the actual verification of this check.
*
* @return bool
*/
public function check(): bool
{
return app()->routesAreCached() === true;
}

/**
* The error message to display in case the check does not pass.
*
* @return string
*/
public function message(): string
{
return 'Your routes should be cached in production. Call "php artisan route:cache" to create the route cache.';
}
}
39 changes: 39 additions & 0 deletions src/Checks/Production/XDebugIsNotEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace BeyondCode\SelfDiagnosis\Checks\Production;

use BeyondCode\SelfDiagnosis\Checks\Check;

class XDebugIsNotEnabled implements Check
{

/**
* The name of the check.
*
* @return string
*/
public function name(): string
{
return 'The xdebug extension is not active.';
}

/**
* Perform the actual verification of this check.
*
* @return bool
*/
public function check(): bool
{
return extension_loaded('xdebug') === true;
}

/**
* The error message to display in case the check does not pass.
*
* @return string
*/
public function message(): string
{
return 'You should not have the "xdebug" PHP extension activated in production.';
}
}
58 changes: 39 additions & 19 deletions src/SelfDiagnosisCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,63 @@ class SelfDiagnosisCommand extends Command
*/
protected $description = 'Perform application self diagnosis.';

private $messages = [];

public function handle()
{
$checks = config('self-diagnosis.checks');
$this->runChecks(config('self-diagnosis.checks', []), 'Running Common Checks');

$environmentChecks = config('self-diagnosis.development', []);
if (in_array(app()->environment(), config('self-diagnosis.productionEnvironments'))) {
$environmentChecks = config('self-diagnosis.production', []);
}

$this->runChecks($environmentChecks, 'Environment Specific Checks ('.app()->environment().')');

if (count($this->messages)) {
$this->output->writeln('The following checks failed:');

foreach ($this->messages as $message) {
$this->output->writeln('<fg=red>'.$message.'</fg=red>');
$this->output->writeln('');
}
} else {
$this->info('Good job, looks like you are all set up.');
}
}

protected function runChecks(array $checks, string $title)
{
$max = count($checks);
$current = 1;
$messages = [];

$this->output->writeln('|-------------------------------------');
$this->output->writeln('| '.$title);
$this->output->writeln('|-------------------------------------');

foreach ($checks as $check) {
/** @var Check $checkClass */
$checkClass = app($check);

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

if ($checkClass->check()) {
$this->output->write('<fg=green>✔</fg=green>');
} else {
$this->output->write('<fg=red>✘</fg=red>');

$messages[] = $checkClass->message();
}
$this->runCheck($checkClass);

$this->output->write(PHP_EOL);
$current++;
}

$this->output->writeln('');
}

if (count($messages)) {
$this->output->writeln('The following checks failed:');

foreach ($messages as $message) {
$this->output->writeln('<fg=red>'.$message.'</fg=red>');
$this->output->writeln('');
}
protected function runCheck(Check $check)
{
if ($check->check()) {
$this->output->write('<fg=green>✔</fg=green>');
} else {
$this->info('Good job, looks like you are all set up.');
$this->output->write('<fg=red>✘</fg=red>');

$this->messages[] = $check->message();
}

$this->output->write(PHP_EOL);
}
}

0 comments on commit 20b1721

Please sign in to comment.