RenderProcessor.php 1.8 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\Common\Page\Interfaces\PageInterface;
  10. use Grav\Framework\Psr7\Response;
  11. use Psr\Http\Message\ResponseInterface;
  12. use Psr\Http\Message\ServerRequestInterface;
  13. use Psr\Http\Server\RequestHandlerInterface;
  14. use RocketTheme\Toolbox\Event\Event;
  15. /**
  16. * Class RenderProcessor
  17. * @package Grav\Common\Processors
  18. */
  19. class RenderProcessor extends ProcessorBase
  20. {
  21. /** @var string */
  22. public $id = 'render';
  23. /** @var string */
  24. public $title = 'Render';
  25. /**
  26. * @param ServerRequestInterface $request
  27. * @param RequestHandlerInterface $handler
  28. * @return ResponseInterface
  29. */
  30. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  31. {
  32. $this->startTimer();
  33. $container = $this->container;
  34. $output = $container['output'];
  35. if ($output instanceof ResponseInterface) {
  36. return $output;
  37. }
  38. /** @var PageInterface $page */
  39. $page = $this->container['page'];
  40. // Use internal Grav output.
  41. $container->output = $output;
  42. ob_start();
  43. $event = new Event(['page' => $page, 'output' => &$container->output]);
  44. $container->fireEvent('onOutputGenerated', $event);
  45. echo $container->output;
  46. $html = ob_get_clean();
  47. // remove any output
  48. $container->output = '';
  49. $event = new Event(['page' => $page, 'output' => $html]);
  50. $this->container->fireEvent('onOutputRendered', $event);
  51. $this->stopTimer();
  52. return new Response($page->httpResponseCode(), $page->httpHeaders(), $html);
  53. }
  54. }