FlexRouter.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Grav\Plugin\FlexObjects;
  3. use Grav\Framework\Route\Route;
  4. use Grav\Plugin\FlexObjects\Controllers\MediaController;
  5. use Grav\Plugin\FlexObjects\Controllers\ObjectController;
  6. use Psr\Http\Message\ResponseInterface;
  7. use Psr\Http\Message\ServerRequestInterface;
  8. use Psr\Http\Server\MiddlewareInterface;
  9. use Psr\Http\Server\RequestHandlerInterface;
  10. /**
  11. * Class FlexRouter
  12. * @package Grav\Plugin\FlexObjects
  13. */
  14. class FlexRouter implements MiddlewareInterface
  15. {
  16. /**
  17. * @param ServerRequestInterface $request
  18. * @param RequestHandlerInterface $handler
  19. * @return ResponseInterface
  20. */
  21. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  22. {
  23. $context = $request->getAttributes();
  24. /** @var Route $route */
  25. $route = $context['route'];
  26. $post = $request->getParsedBody();
  27. $task = $post['task'] ?? $route->getParam('task');
  28. if (\in_array($task, ['cropupload', 'filesupload'])) {
  29. $task = 'media.upload';
  30. }
  31. switch ($task) {
  32. case 'media.upload':
  33. case 'media.delete':
  34. case 'media.copy':
  35. case 'media.remove':
  36. case 'media.list':
  37. case 'media.add':
  38. case 'listmedia':
  39. case 'addmedia':
  40. case 'delmedia':
  41. return (new MediaController())->handle($request);
  42. case 'save':
  43. case 'create':
  44. case 'update':
  45. case 'delete':
  46. case 'reset':
  47. case 'preview':
  48. case 'move':
  49. return (new ObjectController())->handle($request);
  50. }
  51. // No handler found.
  52. return $handler->handle($request);
  53. }
  54. }