Application.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Benchmark;
  3. use Benchmark\Entities\PlainTextEntity;
  4. use Benchmark\Resources\DbResource;
  5. use Benchmark\Resources\FortuneResource;
  6. use Benchmark\Resources\UpdateResource;
  7. use Cache\Adapter\Void\VoidCachePool;
  8. use Hamlet\Applications\AbstractApplication;
  9. use Hamlet\Database\Database;
  10. use Hamlet\Entities\JsonEntity;
  11. use Hamlet\Requests\Request;
  12. use Hamlet\Resources\EntityResource;
  13. use Hamlet\Resources\WebResource;
  14. use Psr\Cache\CacheItemPoolInterface;
  15. class Application extends AbstractApplication
  16. {
  17. /** @var CacheItemPoolInterface|null */
  18. private $cache;
  19. /** @var Database|null */
  20. private $database;
  21. protected function findResource(Request $request): WebResource
  22. {
  23. if ($request->pathMatches('/plaintext')) {
  24. return new EntityResource(new PlainTextEntity('Hello, World!'), 'GET');
  25. } elseif ($request->pathMatches('/json')) {
  26. return new EntityResource(new JsonEntity([
  27. 'message' => 'Hello, World!'
  28. ]), 'GET');
  29. } elseif ($request->pathMatches('/db') || $request->pathMatches('/queries')) {
  30. return new DbResource($this->database());
  31. } elseif ($request->pathMatches('/fortunes')) {
  32. return new FortuneResource($this->database());
  33. } elseif ($request->pathMatches('/update')) {
  34. return new UpdateResource($this->database());
  35. }
  36. }
  37. private function database(): Database
  38. {
  39. if (!$this->database) {
  40. $this->database = Database::mysql(
  41. 'p:tfb-database',
  42. 'benchmarkdbuser',
  43. 'benchmarkdbpass',
  44. 'hello_world'
  45. );
  46. }
  47. return $this->database;
  48. }
  49. protected function getCache(Request $request): CacheItemPoolInterface
  50. {
  51. if (!$this->cache) {
  52. $this->cache = new VoidCachePool();
  53. }
  54. return $this->cache;
  55. }
  56. }