TasksProcessor.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @package Grav\Common\Processors
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Processors;
  9. use Grav\Framework\RequestHandler\Exception\NotFoundException;
  10. use Psr\Http\Message\ResponseInterface;
  11. use Psr\Http\Message\ServerRequestInterface;
  12. use Psr\Http\Server\RequestHandlerInterface;
  13. /**
  14. * Class TasksProcessor
  15. * @package Grav\Common\Processors
  16. */
  17. class TasksProcessor extends ProcessorBase
  18. {
  19. /** @var string */
  20. public $id = 'tasks';
  21. /** @var string */
  22. public $title = 'Tasks';
  23. /**
  24. * @param ServerRequestInterface $request
  25. * @param RequestHandlerInterface $handler
  26. * @return ResponseInterface
  27. */
  28. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  29. {
  30. $this->startTimer();
  31. $task = $this->container['task'];
  32. $action = $this->container['action'];
  33. if ($task || $action) {
  34. $attributes = $request->getAttribute('controller');
  35. $controllerClass = $attributes['class'] ?? null;
  36. if ($controllerClass) {
  37. /** @var RequestHandlerInterface $controller */
  38. $controller = new $controllerClass($attributes['path'] ?? '', $attributes['params'] ?? []);
  39. try {
  40. $response = $controller->handle($request);
  41. if ($response->getStatusCode() === 418) {
  42. $response = $handler->handle($request);
  43. }
  44. $this->stopTimer();
  45. return $response;
  46. } catch (NotFoundException $e) {
  47. // Task not found: Let it pass through.
  48. }
  49. }
  50. if ($task) {
  51. $this->container->fireEvent('onTask.' . $task);
  52. } elseif ($action) {
  53. $this->container->fireEvent('onAction.' . $action);
  54. }
  55. }
  56. $this->stopTimer();
  57. return $handler->handle($request);
  58. }
  59. }