2016-10-01 14:45:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
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]);
|
|
|
|
|
2017-09-12 20:23:48 +01:00
|
|
|
Artisan::call('config:cache');
|
2016-10-28 15:41:16 +01:00
|
|
|
Artisan::call('cache:clear');
|
2017-09-12 20:23:48 +01:00
|
|
|
Artisan::call('view:clear');
|
2016-10-28 15:41:16 +01:00
|
|
|
Artisan::call('migrate', ['--force' => true]);
|
2017-04-18 20:17:09 +01:00
|
|
|
Artisan::call('db:seed', ['--force' => true]);
|
2017-04-18 20:07:59 +01:00
|
|
|
|
2017-04-19 09:12:36 +01:00
|
|
|
// Run any data migrations relevant to the new version
|
|
|
|
$dataMigrationsFolder = join(DIRECTORY_SEPARATOR, [dirname(dirname(dirname(__DIR__))), 'database', 'data_migrations']);
|
|
|
|
$di = new \RecursiveDirectoryIterator($dataMigrationsFolder, \RecursiveDirectoryIterator::SKIP_DOTS);
|
|
|
|
$updateClasses = [];
|
|
|
|
|
|
|
|
// First load all data migration classes and examine the version number to decide if we need it or not
|
|
|
|
/** @var \SplFileInfo $fileInfo */
|
|
|
|
foreach ($di as $fileInfo)
|
2017-04-17 21:31:45 +01:00
|
|
|
{
|
2017-04-19 09:12:36 +01:00
|
|
|
if (!$di->isFile() || $di->getExtension() != 'php')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = substr($fileInfo->getFilename(), 0, strlen($fileInfo->getFilename()) - 4);
|
|
|
|
|
2017-04-17 21:31:45 +01:00
|
|
|
/** @var DataMigration $upgradeClass */
|
|
|
|
$upgradeClass = new $className;
|
2017-04-19 09:12:36 +01:00
|
|
|
if (version_compare($versionNumber->value, $upgradeClass->getVersion()) < 0)
|
|
|
|
{
|
|
|
|
$updateClasses[] = $upgradeClass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now run the migrations
|
|
|
|
foreach ($updateClasses as $upgradeClass)
|
|
|
|
{
|
2017-04-17 21:31:45 +01:00
|
|
|
$upgradeClass->run($versionNumber);
|
|
|
|
}
|
2016-10-28 15:41:16 +01:00
|
|
|
|
2017-04-19 09:12:36 +01:00
|
|
|
// Save the new version number
|
|
|
|
$versionNumber->value = $appVersionNumber;
|
|
|
|
$versionNumber->save();
|
|
|
|
}
|
2016-10-28 15:41:16 +01:00
|
|
|
}
|
2016-10-01 14:45:48 +01:00
|
|
|
}
|