diff --git a/app/Console/Commands/Optimize.php b/app/Console/Commands/Optimize.php new file mode 100644 index 00000000000..a1505ecea0f --- /dev/null +++ b/app/Console/Commands/Optimize.php @@ -0,0 +1,112 @@ +hasOption('clever') && $this->option('clever') === true; + $confirmationDefault = match ($this->option('dont-confirm')) { + 'assume-yes' => true, + 'assume-no' => false, + null => null, + default => throw new InvalidOptionException(sprintf('Unexpected option value %s for --dont-confirm', $this->option('dont-confirm'))) + }; + $hasPreviousCache = file_exists($this->laravel->getCachedConfigPath()) || file_exists($this->laravel->getCachedRoutesPath()); + + $this->call('optimize:clear'); + + if ($shallBeClever && !$hasPreviousCache) { + return; + } + + if ($this->isNonProductive()) { + $this->alert('Application not in Production!'); + + $hasConfirmed = $confirmationDefault ?? $this->confirm('Do you really wish to run this command?'); + + if (!$hasConfirmed) { + $this->comment('Command Canceled!'); + + return; + } + } + + parent::handle(); + } + + /** + * Checks whether Lychee is running in a non-production environment. + * + * Note, this method deliberately tends to `true` in case of doubt. + * This means if anything indicates that the setup might be used for + * developing or testing purposes, the result is `true`. + * Such indicators are the environment setting, enabled debug mode or + * debug bar, installed PhpUnit or PhpStan. + * If we are not in production mode, this command asks for confirmation, + * and we rather ask one time too often than not. + * + * @return bool `true`, if Lychee is found to run in non-production mode + * + * @throws BindingResolutionException + * @throws NotFoundExceptionInterface + * @throws ContainerExceptionInterface + */ + protected function isNonProductive(): bool + { + return + 'production' !== $this->getLaravel()->environment() || + true === config('app.debug', false) || + true === config('debugbar.enabled', false) || + file_exists(base_path('vendor/bin/phpunit')) || + file_exists(base_path('vendor/bin/phpstan')) || + file_exists(base_path('vendor/bin/phpstan.phar')); + } +} diff --git a/composer.json b/composer.json index ff309d9b403..14ff3c7ea01 100644 --- a/composer.json +++ b/composer.json @@ -106,6 +106,7 @@ ], "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan optimize --clever --dont-confirm=assume-no", "@php artisan package:discover", "@install_files" ], diff --git a/scripts/post-merge b/scripts/post-merge index c91f2a90d8b..e4a84fd3d08 100755 --- a/scripts/post-merge +++ b/scripts/post-merge @@ -26,6 +26,8 @@ else composer install --no-dev --prefer-dist fi if [ -f ".env" ]; then + echo "php artisan optimize --clever --dont-confirm=assume-no" + php artisan optimize --clever --dont-confirm=assume-no echo "php artisan migrate --force" php artisan migrate --force fi