|
@@ -2,54 +2,28 @@
|
|
|
|
|
|
namespace Benchmark;
|
|
|
|
|
|
-use Benchmark\Resources\DbResource;
|
|
|
-use Benchmark\Resources\FortuneResource;
|
|
|
-use Benchmark\Resources\HelloJsonResource;
|
|
|
-use Benchmark\Resources\HelloTextResource;
|
|
|
-use Benchmark\Resources\QueriesResource;
|
|
|
-use Benchmark\Resources\UpdateResource;
|
|
|
+use Benchmark\Resources\{DbResource, FortuneResource, HelloJsonResource, HelloTextResource, QueriesResource, UpdateResource};
|
|
|
use Cache\Adapter\PHPArray\ArrayCachePool;
|
|
|
-use Hamlet\Database\Database;
|
|
|
+use Hamlet\Database\{Database, Session};
|
|
|
use Hamlet\Http\Applications\AbstractApplication;
|
|
|
use Hamlet\Http\Requests\Request;
|
|
|
-use Hamlet\Http\Resources\HttpResource;
|
|
|
-use Hamlet\Http\Resources\NotFoundResource;
|
|
|
-use Hamlet\Http\Responses\Response;
|
|
|
-use Hamlet\Http\Responses\ServerErrorResponse;
|
|
|
+use Hamlet\Http\Resources\{HttpResource, NotFoundResource};
|
|
|
+use Hamlet\Http\Responses\{Response, ServerErrorResponse};
|
|
|
use Psr\Cache\CacheItemPoolInterface;
|
|
|
use Throwable;
|
|
|
|
|
|
class Application extends AbstractApplication
|
|
|
{
|
|
|
- /** @var CacheItemPoolInterface|null */
|
|
|
+ /** @var CacheItemPoolInterface */
|
|
|
private $cache;
|
|
|
|
|
|
- /** @var HttpResource */
|
|
|
- private $helloTextResource;
|
|
|
-
|
|
|
- /** @var HttpResource */
|
|
|
- private $helloJsonResource;
|
|
|
-
|
|
|
- /** @var HttpResource */
|
|
|
- private $dbResource;
|
|
|
-
|
|
|
- /** @var HttpResource */
|
|
|
- private $queriesResource;
|
|
|
-
|
|
|
- /** @var HttpResource */
|
|
|
- private $fortuneResource;
|
|
|
-
|
|
|
- /** @var HttpResource */
|
|
|
- private $updateResource;
|
|
|
+ /** @var Database */
|
|
|
+ private $database;
|
|
|
|
|
|
public function __construct(Database $database)
|
|
|
{
|
|
|
- $this->helloJsonResource = new HelloJsonResource();
|
|
|
- $this->helloTextResource = new HelloTextResource();
|
|
|
- $this->dbResource = new DbResource($database);
|
|
|
- $this->queriesResource = new QueriesResource($database);
|
|
|
- $this->fortuneResource = new FortuneResource($database);
|
|
|
- $this->updateResource = new UpdateResource($database);
|
|
|
+ $this->cache = new ArrayCachePool;
|
|
|
+ $this->database = $database;
|
|
|
}
|
|
|
|
|
|
public function run(Request $request): Response
|
|
@@ -57,7 +31,7 @@ class Application extends AbstractApplication
|
|
|
try {
|
|
|
return parent::run($request);
|
|
|
} catch (Throwable $e) {
|
|
|
- return new ServerErrorResponse();
|
|
|
+ return new ServerErrorResponse;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -65,26 +39,31 @@ class Application extends AbstractApplication
|
|
|
{
|
|
|
switch ($request->getPath()) {
|
|
|
case '/plaintext':
|
|
|
- return $this->helloTextResource;
|
|
|
+ return new HelloTextResource;
|
|
|
case '/json':
|
|
|
- return $this->helloJsonResource;
|
|
|
+ return new HelloJsonResource;
|
|
|
case '/db':
|
|
|
- return $this->dbResource;
|
|
|
+ return $this->database->withSession(function (Session $session) {
|
|
|
+ return new DbResource($session);
|
|
|
+ });
|
|
|
case '/queries':
|
|
|
- return $this->queriesResource;
|
|
|
+ return $this->database->withSession(function (Session $session) {
|
|
|
+ return new QueriesResource($session);
|
|
|
+ });
|
|
|
case '/fortunes':
|
|
|
- return $this->fortuneResource;
|
|
|
+ return $this->database->withSession(function (Session $session) {
|
|
|
+ return new FortuneResource($session);
|
|
|
+ });
|
|
|
case '/update':
|
|
|
- return $this->updateResource;
|
|
|
+ return $this->database->withSession(function (Session $session) {
|
|
|
+ return new UpdateResource($session);
|
|
|
+ });
|
|
|
}
|
|
|
- return new NotFoundResource();
|
|
|
+ return new NotFoundResource;
|
|
|
}
|
|
|
|
|
|
protected function getCache(Request $request): CacheItemPoolInterface
|
|
|
{
|
|
|
- if (!$this->cache) {
|
|
|
- $this->cache = new ArrayCachePool();
|
|
|
- }
|
|
|
return $this->cache;
|
|
|
}
|
|
|
}
|