TasksProcessor.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @package Grav\Common\Processors
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 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. class TasksProcessor extends ProcessorBase
  14. {
  15. public $id = 'tasks';
  16. public $title = 'Tasks';
  17. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
  18. {
  19. $this->startTimer();
  20. $task = $this->container['task'];
  21. $action = $this->container['action'];
  22. if ($task || $action) {
  23. $attributes = $request->getAttribute('controller');
  24. $controllerClass = $attributes['class'] ?? null;
  25. if ($controllerClass) {
  26. /** @var RequestHandlerInterface $controller */
  27. $controller = new $controllerClass($attributes['path'] ?? '', $attributes['params'] ?? []);
  28. try {
  29. $response = $controller->handle($request);
  30. if ($response->getStatusCode() === 418) {
  31. $response = $handler->handle($request);
  32. }
  33. $this->stopTimer();
  34. return $response;
  35. } catch (NotFoundException $e) {
  36. // Task not found: Let it pass through.
  37. }
  38. }
  39. if ($task) {
  40. $this->container->fireEvent('onTask.' . $task);
  41. } elseif ($action) {
  42. $this->container->fireEvent('onAction.' . $action);
  43. }
  44. }
  45. $this->stopTimer();
  46. return $handler->handle($request);
  47. }
  48. }