RequestHandler.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Opentalent\OtStats\Middleware;
  3. use Opentalent\OtCore\Website\OtPageRepository;
  4. use Opentalent\OtStats\Settings\StatsSettingsRepository;
  5. use Psr\Http\Message\ResponseInterface;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. use Psr\Http\Server\MiddlewareInterface;
  8. use Psr\Http\Server\RequestHandlerInterface;
  9. use TYPO3\CMS\Core\Http\Stream;
  10. use TYPO3\CMS\Core\Utility\GeneralUtility;
  11. use TYPO3\CMS\Extbase\Object\ObjectManager;
  12. use TYPO3\CMS\Fluid\View\StandaloneView;
  13. /**
  14. * Hooks into the frontend request to add the matomo script into
  15. * the request's header
  16. *
  17. * @internal
  18. */
  19. class RequestHandler implements MiddlewareInterface
  20. {
  21. const TEMPLATES_ROOT_PATHS = 'EXT:ot_stats/Resources/Private/Templates';
  22. const PARTIALS_ROOT_PATHS = 'EXT:ot_stats/Resources/Private/Partials';
  23. const LAYOUTS_ROOT_PATHS = 'EXT:ot_stats/Resources/Private/Layouts';
  24. const TEMPLATE_FILE = self::TEMPLATES_ROOT_PATHS . '/Header/Include.html';
  25. public function __construct() {}
  26. /**
  27. * Process the frontend request to insert the matomo script in the header
  28. * if the stats module is activated
  29. *
  30. * @param ServerRequestInterface $request
  31. * @param RequestHandlerInterface $handler
  32. * @return ResponseInterface
  33. */
  34. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  35. {
  36. // Retrieve the current matomo site's id if set
  37. $otPageRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtPageRepository::class);
  38. $rootPageUid = $otPageRepository->getCurrentSiteRootPageUid();
  39. $statsSettingsRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(StatsSettingsRepository::class);
  40. $matomoSiteId = $statsSettingsRepository->getMatomoSiteId($rootPageUid);
  41. if (!$matomoSiteId > 0) {
  42. // stats are not enabled, continue
  43. return $handler->handle($request);
  44. }
  45. // render view
  46. $view = GeneralUtility::makeInstance(StandaloneView::class);
  47. $view->setTemplateRootPaths([self::TEMPLATES_ROOT_PATHS]);
  48. $view->setPartialRootPaths([self::PARTIALS_ROOT_PATHS]);
  49. $view->setLayoutRootPaths([self::LAYOUTS_ROOT_PATHS]);
  50. $view->setTemplatePathAndFilename(
  51. GeneralUtility::getFileAbsFileName(self::TEMPLATE_FILE)
  52. );
  53. // Build the response
  54. $response = $handler->handle($request);
  55. // insert the rendered view in the body, just before the closing </html> tag
  56. $newSection = $view->render();
  57. $bodyContent = (string)$response->getBody();
  58. $bodyContent = str_replace('</head>', $newSection . '</head>', $bodyContent);
  59. $newBody = new Stream('php://temp', 'wb+');
  60. $newBody->write($bodyContent);
  61. $newBody->rewind();
  62. $response = $response->withBody($newBody);
  63. return $response;
  64. }
  65. }