SimplePageHandler.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @package Grav\Common\Errors
  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\Errors;
  9. use Whoops\Handler\Handler;
  10. use Whoops\Util\Misc;
  11. use Whoops\Util\TemplateHelper;
  12. class SimplePageHandler extends Handler
  13. {
  14. private $searchPaths = array();
  15. private $resourceCache = array();
  16. public function __construct()
  17. {
  18. // Add the default, local resource search path:
  19. $this->searchPaths[] = __DIR__ . '/Resources';
  20. }
  21. /**
  22. * @return int|null
  23. */
  24. public function handle()
  25. {
  26. $inspector = $this->getInspector();
  27. $helper = new TemplateHelper();
  28. $templateFile = $this->getResource('layout.html.php');
  29. $cssFile = $this->getResource('error.css');
  30. $code = $inspector->getException()->getCode();
  31. if ( ($code >= 400) && ($code < 600) )
  32. {
  33. $this->getRun()->sendHttpCode($code);
  34. }
  35. $message = $inspector->getException()->getMessage();
  36. if ($inspector->getException() instanceof \ErrorException) {
  37. $code = Misc::translateErrorCode($code);
  38. }
  39. $vars = array(
  40. 'stylesheet' => file_get_contents($cssFile),
  41. 'code' => $code,
  42. 'message' => filter_var(rawurldecode($message), FILTER_SANITIZE_STRING),
  43. );
  44. $helper->setVariables($vars);
  45. $helper->render($templateFile);
  46. return Handler::QUIT;
  47. }
  48. /**
  49. * @param string $resource
  50. *
  51. * @return string
  52. * @throws \RuntimeException
  53. */
  54. protected function getResource($resource)
  55. {
  56. // If the resource was found before, we can speed things up
  57. // by caching its absolute, resolved path:
  58. if (isset($this->resourceCache[$resource])) {
  59. return $this->resourceCache[$resource];
  60. }
  61. // Search through available search paths, until we find the
  62. // resource we're after:
  63. foreach ($this->searchPaths as $path) {
  64. $fullPath = "{$path}/{$resource}";
  65. if (is_file($fullPath)) {
  66. // Cache the result:
  67. $this->resourceCache[$resource] = $fullPath;
  68. return $fullPath;
  69. }
  70. }
  71. // If we got this far, nothing was found.
  72. throw new \RuntimeException(
  73. "Could not find resource '{$resource}' in any resource paths (searched: " . implode(', ', $this->searchPaths). ')'
  74. );
  75. }
  76. public function addResourcePath($path)
  77. {
  78. if (!is_dir($path)) {
  79. throw new \InvalidArgumentException(
  80. "'{$path}' is not a valid directory"
  81. );
  82. }
  83. array_unshift($this->searchPaths, $path);
  84. }
  85. public function getResourcePaths()
  86. {
  87. return $this->searchPaths;
  88. }
  89. }