Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1506 #1516

Merged
merged 8 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions app/Console/Commands/Optimize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Console\Commands;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Foundation\Console\OptimizeCommand;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\Console\Exception\ExceptionInterface as ConsoleException;

class Optimize extends OptimizeCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'optimize
{--clever : Only (re-)creates cache if cache has already been created before and if not in production mode}
{--force : Don\'t ask for confirmation, if cache shall be created in debug mode (same behaviour as original Laravel command)}';

/**
* Execute the console command.
*
* @return void
*
* @throws ConsoleException
* @throws BindingResolutionException
* @throws NotFoundExceptionInterface
* @throws ContainerExceptionInterface
*/
public function handle(): void
{
$shallBeClever = $this->hasOption('clever') && $this->option('no-clever') === true;
ildyria marked this conversation as resolved.
Show resolved Hide resolved
$shallEnforce = $this->hasOption('force') && $this->option('force') === true;
$hasPreviousCache = file_exists($this->laravel->getCachedConfigPath()) || file_exists($this->laravel->getCachedRoutesPath());

$this->call('optimize:clear');

if ($shallBeClever && !$hasPreviousCache) {
return;
}

if ($this->isNonProductive() && !$shallEnforce) {
$this->alert('Application In Production!');

$hasConfirmed = $this->confirm('Do you really wish to run this command?');

if (!$hasConfirmed) {
$this->comment('Command Canceled!');
kamil4 marked this conversation as resolved.
Show resolved Hide resolved

return;
}
}

parent::handle();
}

/**
* Checks whether Lychee is running in a non-prduction 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 ask for confirmation,
nagmat84 marked this conversation as resolved.
Show resolved Hide resolved
* and we rather ask one time too often than not.
*
* @return bool `true`, if the
nagmat84 marked this conversation as resolved.
Show resolved Hide resolved
*
* @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'));
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan optimize:clear",
"@php artisan package:discover",
"@install_files"
],
Expand Down
2 changes: 2 additions & 0 deletions scripts/post-merge
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ else
composer install --no-dev --prefer-dist
fi
if [ -f ".env" ]; then
echo "php artisan optimize:clear"
php artisan optimize:clear
echo "php artisan migrate --force"
php artisan migrate --force
fi
Expand Down