2016-10-01 14:45:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use App\Configuration;
|
2017-04-17 21:31:45 +01:00
|
|
|
use App\DataMigration;
|
2016-10-28 15:41:16 +01:00
|
|
|
use App\Facade\UserConfig;
|
2016-10-01 14:45:48 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Foundation\Application;
|
|
|
|
use Illuminate\Http\Request;
|
2016-10-28 15:41:16 +01:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2016-10-01 14:45:48 +01:00
|
|
|
|
|
|
|
class AppInstallation
|
|
|
|
{
|
|
|
|
private $baseDirectory;
|
|
|
|
private $environmentFilePath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The application instance.
|
|
|
|
*
|
|
|
|
* @var \Illuminate\Foundation\Application
|
|
|
|
*/
|
|
|
|
protected $app;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new middleware instance.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Foundation\Application $app
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Application $app)
|
|
|
|
{
|
|
|
|
$this->app = $app;
|
|
|
|
$this->baseDirectory = dirname(dirname(dirname(__DIR__)));
|
|
|
|
$this->environmentFilePath = sprintf('%s/.env', $this->baseDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
|
|
|
// We always need an encryption key if not already present
|
|
|
|
$this->generateAppKey();
|
|
|
|
|
|
|
|
if ($this->app->runningInConsole())
|
|
|
|
{
|
|
|
|
// Allow the console to run successfully even if we're not installed
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2016-10-06 15:07:30 +01:00
|
|
|
// See if the successful flag has been written to the .env file
|
|
|
|
$isAppInstalled = MiscHelper::getEnvironmentSetting('APP_INSTALLED');
|
|
|
|
|
2016-10-01 14:45:48 +01:00
|
|
|
if ($request->is('install/*'))
|
|
|
|
{
|
|
|
|
// Already in the installer
|
2016-10-06 15:07:30 +01:00
|
|
|
// For security reasons, don't allow the installer to be used if it has been previously completed
|
|
|
|
if (boolval($isAppInstalled))
|
2016-10-01 14:45:48 +01:00
|
|
|
{
|
2016-10-06 15:07:30 +01:00
|
|
|
return redirect(route('home'));
|
2016-10-01 14:45:48 +01:00
|
|
|
}
|
2016-10-06 15:07:30 +01:00
|
|
|
|
|
|
|
return $next($request);
|
2016-10-01 14:45:48 +01:00
|
|
|
}
|
2016-10-06 15:07:30 +01:00
|
|
|
|
|
|
|
if ($isAppInstalled)
|
2016-10-01 14:45:48 +01:00
|
|
|
{
|
2016-10-28 15:41:16 +01:00
|
|
|
// See if an update is necessary
|
|
|
|
$this->updateDatabaseIfRequired();
|
|
|
|
|
2016-10-06 15:07:30 +01:00
|
|
|
// App is configured, continue on
|
|
|
|
return $next($request);
|
2016-10-01 14:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(route('install.check'));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateAppKey()
|
|
|
|
{
|
|
|
|
// Generate an application key and store to the .env file
|
|
|
|
if (!file_exists($this->environmentFilePath))
|
|
|
|
{
|
|
|
|
$key = MiscHelper::randomString(32);
|
|
|
|
file_put_contents($this->environmentFilePath, sprintf('APP_KEY=%s', $key) . PHP_EOL);
|
|
|
|
app('config')->set(['app' => ['key' => $key]]);
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 15:41:16 +01:00
|
|
|
|
|
|
|
private function updateDatabaseIfRequired()
|
|
|
|
{
|
|
|
|
$versionNumber = UserConfig::getOrCreateModel('app_version');
|
|
|
|
$appVersionNumber = config('app.version');
|
|
|
|
|
|
|
|
if (is_null($appVersionNumber) || $versionNumber->value != $appVersionNumber)
|
|
|
|
{
|
|
|
|
Log::info('Upgrading database', ['new_version' => $appVersionNumber]);
|
|
|
|
|
|
|
|
Artisan::call('cache:clear');
|
|
|
|
Artisan::call('migrate', ['--force' => true]);
|
2017-04-17 21:31:45 +01:00
|
|
|
|
2017-04-18 20:07:59 +01:00
|
|
|
Artisan::call('db:seed');
|
|
|
|
|
2017-04-17 21:31:45 +01:00
|
|
|
$className = sprintf('DataMigrationV%s', str_replace(['.', '-'], '_', $appVersionNumber));
|
|
|
|
if (class_exists($className))
|
|
|
|
{
|
|
|
|
/** @var DataMigration $upgradeClass */
|
|
|
|
$upgradeClass = new $className;
|
|
|
|
$upgradeClass->run($versionNumber);
|
|
|
|
}
|
2016-10-28 15:41:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$versionNumber->value = $appVersionNumber;
|
|
|
|
$versionNumber->save();
|
|
|
|
}
|
2016-10-01 14:45:48 +01:00
|
|
|
}
|