/**
* Initialize the Laravel framework.
* @param SymfonyRequest $request
*/
private function initialize($request = null)
{
// Store a reference to the database object
// so the database connection can be reused during tests
$oldDb = null;
if ($this->app['db'] && $this->app['db']->connection()) {
$oldDb = $this->app['db'];
}
// The module can login a user with the $I->amLoggedAs() method,
// but this is not persisted between requests. Store a reference
// to the logged in user to simulate this.
$loggedInUser = null;
if ($this->app['auth'] && $this->app['auth']->check()) {
$loggedInUser = $this->app['auth']->user();
}
// Load the application object
$this->app = $this->kernel = $this->loadApplication();
// Set the request instance for the application
if (is_null($request)) {
$appConfig = (require $this->module->config['project_dir'] . 'config/app.php');
$request = SymfonyRequest::create($appConfig['url']);
}
$this->app->instance('request', Request::createFromBase($request));
$this->app->instance('middleware.disable', $this->module->config['disable_middleware']);
// Bootstrap the application
$this->app->make('Illuminate\\Contracts\\Http\\Kernel')->bootstrap();
// Restore the old database object if available
if ($oldDb) {
$this->app['db'] = $oldDb;
Model::setConnectionResolver($this->app['db']);
}
// If there was a user logged in restore this user.
// Also reload the user object from the user provider to prevent stale user data.
if ($loggedInUser) {
$refreshed = $this->app['auth']->getProvider()->retrieveById($loggedInUser->getAuthIdentifier());
$this->app['auth']->setUser($refreshed ?: $loggedInUser);
}
$this->module->setApplication($this->app);
}