api.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. use Nyholm\Psr7\Response;
  4. use Nyholm\Psr7\Factory\Psr17Factory;
  5. use Spiral\RoadRunner\Worker;
  6. use Spiral\RoadRunner\Http\PSR7Worker;
  7. // Create new RoadRunner worker from global environment
  8. $worker = Worker::create();
  9. // Create common PSR-17 HTTP factory
  10. $factory = new Psr17Factory();
  11. $psr7 = new PSR7Worker($worker, $factory, $factory, $factory);
  12. $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
  13. $r->addGroup('/en', function (\FastRoute\RouteCollector $r) {
  14. $r->addRoute('GET', '/pluralize', [\morphos\Service\English::class, 'pluralize']);
  15. $r->addRoute('GET', '/cardinal', [\morphos\Service\English::class, 'cardinal']);
  16. $r->addRoute('GET', '/ordinal', [\morphos\Service\English::class, 'ordinal']);
  17. $r->addRoute('GET', '/time/spellDifference', [\morphos\Service\English::class, 'spellTimeDifference']);
  18. $r->addRoute('GET', '/time/spellInterval', [\morphos\Service\English::class, 'spellTimeInterval']);
  19. });
  20. $r->addGroup('/ru', function (\FastRoute\RouteCollector $r) {
  21. $r->addRoute('GET', '/cases', [\morphos\Service\Russian::class, 'cases']);
  22. $r->addRoute('GET', '/name', [\morphos\Service\Russian::class, 'name']);
  23. $r->addRoute('GET', '/detectGender', [\morphos\Service\Russian::class, 'detectGender']);
  24. $r->addRoute('GET', '/pluralize', [\morphos\Service\Russian::class, 'pluralize']);
  25. $r->addGroup('/noun', function (\FastRoute\RouteCollector $r) {
  26. $r->addGroup('/declension', function (\FastRoute\RouteCollector $r) {
  27. $r->addRoute('GET', '/cases', [\morphos\Service\Russian::class, 'nounDeclensionCases']);
  28. $r->addRoute('GET', '/detectGender', [\morphos\Service\Russian::class, 'nounDeclensionDetectGender']);
  29. $r->addRoute('GET', '/detect', [\morphos\Service\Russian::class, 'nounDeclensionDetect']);
  30. $r->addRoute('GET', '/isMutable', [\morphos\Service\Russian::class, 'nounDeclensionIsMutable']);
  31. });
  32. $r->addGroup('/pluralization', function (\FastRoute\RouteCollector $r) {
  33. $r->addRoute('GET', '/cases', [\morphos\Service\Russian::class, 'nounPluralizationCases']);
  34. $r->addRoute('GET', '/numeralForm', [\morphos\Service\Russian::class, 'nounPluralizationNumeralForm']);
  35. });
  36. });
  37. $r->addGroup('/cardinal', function (\FastRoute\RouteCollector $r) {
  38. $r->addRoute('GET', '/cases', [\morphos\Service\Russian::class, 'cardinalCases']);
  39. });
  40. $r->addGroup('/ordinal', function (\FastRoute\RouteCollector $r) {
  41. $r->addRoute('GET', '/cases', [\morphos\Service\Russian::class, 'ordinalCases']);
  42. });
  43. $r->addGroup('/geo', function (\FastRoute\RouteCollector $r) {
  44. $r->addRoute('GET', '/cases', [\morphos\Service\Russian::class, 'geoCases']);
  45. $r->addRoute('GET', '/isMutable', [\morphos\Service\Russian::class, 'geoIsMutable']);
  46. });
  47. $r->addRoute('GET', '/money/spell', [\morphos\Service\Russian::class, 'spellMoney']);
  48. $r->addRoute('GET', '/time/spellDifference', [\morphos\Service\Russian::class, 'spellTimeDifference']);
  49. $r->addRoute('GET', '/time/spellInterval', [\morphos\Service\Russian::class, 'spellTimeInterval']);
  50. $r->addGroup('/prep', function (\FastRoute\RouteCollector $r) {
  51. $r->addRoute('GET', '/in', [\morphos\Service\Russian::class, 'prepIn']);
  52. $r->addRoute('GET', '/with', [\morphos\Service\Russian::class, 'prepWith']);
  53. $r->addRoute('GET', '/about', [\morphos\Service\Russian::class, 'prepAbout']);
  54. });
  55. $r->addRoute('GET', '/verb/ending', [\morphos\Service\Russian::class, 'verbEnding']);
  56. });
  57. });
  58. $handlers = [];
  59. do {
  60. try {
  61. $request = $psr7->waitRequest();
  62. if ($request === null) {
  63. break;
  64. }
  65. } catch (\Throwable $e) {
  66. // Although the PSR-17 specification clearly states that there can be
  67. // no exceptions when creating a request, however, some implementations
  68. // may violate this rule. Therefore, it is recommended to process the
  69. // incoming request for errors.
  70. //
  71. // Send "Bad Request" response.
  72. $psr7->respond(new Response(400));
  73. continue;
  74. }
  75. try {
  76. $routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
  77. switch ($routeInfo[0]) {
  78. case FastRoute\Dispatcher::NOT_FOUND:
  79. // ... 404 Not Found
  80. $psr7->respond(new Response(404));
  81. break;
  82. case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
  83. $allowedMethods = $routeInfo[1];
  84. // ... 405 Method Not Allowed
  85. $psr7->respond(new Response(405));
  86. break;
  87. case FastRoute\Dispatcher::FOUND:
  88. if (is_string($routeInfo[1])) {
  89. $handlers = $routeInfo[1];
  90. } else if (is_array($routeInfo[1])) {
  91. if (!isset($handlers[$routeInfo[1][0]])) {
  92. $handlers[$routeInfo[1][0]] = new $routeInfo[1][0];
  93. }
  94. $handler = [$handlers[$routeInfo[1][0]], $routeInfo[1][1]];
  95. }
  96. parse_str($request->getUri()->getQuery(), $args);
  97. // Reply by the 200 OK response
  98. $psr7->respond(
  99. new Response(200, [
  100. 'Content-Type' => 'application/json',
  101. ], json_encode(
  102. [
  103. 'result' => call_user_func($handler, $args, $dispatcher)
  104. ]
  105. ))
  106. );
  107. break;
  108. }
  109. } catch (\Throwable $e) {
  110. // In case of any exceptions in the application code, you should handle
  111. // them and inform the client about the presence of a server error.
  112. //
  113. // Reply by the 500 Internal Server Error response
  114. $psr7->respond(new Response(500, [], 'Something Went Wrong!'));
  115. // Additionally, we can inform the RoadRunner that the processing
  116. // of the request failed.
  117. $psr7->getWorker()->error((string)$e);
  118. }
  119. } while (isset($request));