From 20b1721743da087ccede687aa829ce955ac6737a Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Thu, 5 Jul 2018 12:14:27 +0200 Subject: [PATCH] allow different checks for dev/production modes --- config/config.php | 33 ++++++++++- .../Development/ConfigurationIsNotCached.php | 39 +++++++++++++ src/Checks/Development/RoutesAreNotCached.php | 39 +++++++++++++ src/Checks/MigrationsAreUpToDate.php | 2 +- .../Production/ConfigurationIsCached.php | 39 +++++++++++++ .../Production/DebugModeIsNotEnabled.php | 39 +++++++++++++ src/Checks/Production/RoutesAreCached.php | 39 +++++++++++++ src/Checks/Production/XDebugIsNotEnabled.php | 39 +++++++++++++ src/SelfDiagnosisCommand.php | 58 +++++++++++++------ 9 files changed, 305 insertions(+), 22 deletions(-) create mode 100644 src/Checks/Development/ConfigurationIsNotCached.php create mode 100644 src/Checks/Development/RoutesAreNotCached.php create mode 100644 src/Checks/Production/ConfigurationIsCached.php create mode 100644 src/Checks/Production/DebugModeIsNotEnabled.php create mode 100644 src/Checks/Production/RoutesAreCached.php create mode 100644 src/Checks/Production/XDebugIsNotEnabled.php diff --git a/config/config.php b/config/config.php index bae3ac2..247d96a 100644 --- a/config/config.php +++ b/config/config.php @@ -1,7 +1,18 @@ [ + 'prod', + 'production', + ], + + /* + * Common checks that will be performed on all environments. + */ 'checks' => [ \BeyondCode\SelfDiagnosis\Checks\AppKeyIsSet::class, \BeyondCode\SelfDiagnosis\Checks\ComposerIsUpToDate::class, @@ -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, + ], ]; diff --git a/src/Checks/Development/ConfigurationIsNotCached.php b/src/Checks/Development/ConfigurationIsNotCached.php new file mode 100644 index 0000000..8856cc6 --- /dev/null +++ b/src/Checks/Development/ConfigurationIsNotCached.php @@ -0,0 +1,39 @@ +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.'; + } +} \ No newline at end of file diff --git a/src/Checks/Development/RoutesAreNotCached.php b/src/Checks/Development/RoutesAreNotCached.php new file mode 100644 index 0000000..43fa91e --- /dev/null +++ b/src/Checks/Development/RoutesAreNotCached.php @@ -0,0 +1,39 @@ +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.'; + } +} \ No newline at end of file diff --git a/src/Checks/MigrationsAreUpToDate.php b/src/Checks/MigrationsAreUpToDate.php index 2b3f9db..8d657c7 100644 --- a/src/Checks/MigrationsAreUpToDate.php +++ b/src/Checks/MigrationsAreUpToDate.php @@ -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) { diff --git a/src/Checks/Production/ConfigurationIsCached.php b/src/Checks/Production/ConfigurationIsCached.php new file mode 100644 index 0000000..7c9df09 --- /dev/null +++ b/src/Checks/Production/ConfigurationIsCached.php @@ -0,0 +1,39 @@ +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.'; + } +} \ No newline at end of file diff --git a/src/Checks/Production/DebugModeIsNotEnabled.php b/src/Checks/Production/DebugModeIsNotEnabled.php new file mode 100644 index 0000000..23f1f0f --- /dev/null +++ b/src/Checks/Production/DebugModeIsNotEnabled.php @@ -0,0 +1,39 @@ +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.'; + } +} \ No newline at end of file diff --git a/src/Checks/Production/XDebugIsNotEnabled.php b/src/Checks/Production/XDebugIsNotEnabled.php new file mode 100644 index 0000000..a78f7b1 --- /dev/null +++ b/src/Checks/Production/XDebugIsNotEnabled.php @@ -0,0 +1,39 @@ +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(''.$message.''); + $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("Running check {$current}/{$max}: {$checkClass->name()}... "); - if ($checkClass->check()) { - $this->output->write(''); - } else { - $this->output->write(''); - - $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(''.$message.''); - $this->output->writeln(''); - } + protected function runCheck(Check $check) + { + if ($check->check()) { + $this->output->write(''); } else { - $this->info('Good job, looks like you are all set up.'); + $this->output->write(''); + + $this->messages[] = $check->message(); } + + $this->output->write(PHP_EOL); } } \ No newline at end of file