Lazily initialise the Application instance

See f403feb3b1
This commit is contained in:
Toby Zerner 2016-04-24 11:10:39 +09:30
parent 7bd3fa82b1
commit 3d812c287f
2 changed files with 10 additions and 4 deletions

View File

@ -62,8 +62,6 @@ abstract class AbstractServer
if (file_exists($file = $this->basePath.'/config.php')) {
$this->config = include $file;
}
$this->app = $this->getApp();
}
/**
@ -135,6 +133,10 @@ abstract class AbstractServer
*/
protected function getApp()
{
if ($this->app !== null) {
return $this->app;
}
date_default_timezone_set('UTC');
$app = new Application($this->basePath, $this->publicPath);
@ -182,6 +184,8 @@ abstract class AbstractServer
$app->boot();
$this->app = $app;
return $app;
}

View File

@ -44,9 +44,11 @@ abstract class AbstractServer extends BaseAbstractServer
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null)
{
$this->collectGarbage($this->app);
$app = $this->getApp();
$middleware = $this->getMiddleware($this->app);
$this->collectGarbage($app);
$middleware = $this->getMiddleware($app);
return $middleware($request, $response, $out);
}