blue-twilight/app/Http/Middleware/AppInstallation.php
2016-10-01 14:45:48 +01:00

78 lines
1.9 KiB
PHP

<?php
namespace App\Http\Middleware;
use App\Configuration;
use App\Helpers\MiscHelper;
use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
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);
}
if ($request->is('install/*'))
{
// Already in the installer
return $next($request);
}
try
{
if (Configuration::installCompleted())
{
return $next($request);
}
}
catch (\Exception $ex)
{
// Empty catch block to allow falling through to the redirect below
}
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]]);
}
}
}