Application.php 2.0 KB

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