PagesProcessor.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\Common\Page\Interfaces\PageInterface;
  10. use RocketTheme\Toolbox\Event\Event;
  11. use Psr\Http\Message\ResponseInterface;
  12. use Psr\Http\Message\ServerRequestInterface;
  13. use Psr\Http\Server\RequestHandlerInterface;
  14. class PagesProcessor extends ProcessorBase
  15. {
  16. public $id = 'pages';
  17. public $title = 'Pages';
  18. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
  19. {
  20. $this->startTimer();
  21. // Dump Cache state
  22. $this->container['debugger']->addMessage($this->container['cache']->getCacheStatus());
  23. $this->container['pages']->init();
  24. $this->container->fireEvent('onPagesInitialized', new Event(['pages' => $this->container['pages']]));
  25. $this->container->fireEvent('onPageInitialized', new Event(['page' => $this->container['page']]));
  26. /** @var PageInterface $page */
  27. $page = $this->container['page'];
  28. if (!$page->routable()) {
  29. // If no page found, fire event
  30. $event = new Event(['page' => $page]);
  31. $event->page = null;
  32. $event = $this->container->fireEvent('onPageNotFound', $event);
  33. if (isset($event->page)) {
  34. unset ($this->container['page']);
  35. $this->container['page'] = $page = $event->page;
  36. } else {
  37. throw new \RuntimeException('Page Not Found', 404);
  38. }
  39. $this->addMessage("Routed to page {$page->rawRoute()} (type: {$page->template()}) [Not Found fallback]");
  40. } else {
  41. $this->addMessage("Routed to page {$page->rawRoute()} (type: {$page->template()})");
  42. $task = $this->container['task'];
  43. $action = $this->container['action'];
  44. if ($task) {
  45. $event = new Event(['task' => $task, 'page' => $page]);
  46. $this->container->fireEvent('onPageTask', $event);
  47. $this->container->fireEvent('onPageTask.' . $task, $event);
  48. } elseif ($action) {
  49. $event = new Event(['action' => $action, 'page' => $page]);
  50. $this->container->fireEvent('onPageAction', $event);
  51. $this->container->fireEvent('onPageAction.' . $action, $event);
  52. }
  53. }
  54. $this->stopTimer();
  55. return $handler->handle($request);
  56. }
  57. }