Pjax.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Dcat\Admin\Middleware;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\MessageBag;
  8. use Symfony\Component\DomCrawler\Crawler;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class Pjax
  11. {
  12. /**
  13. * Handle an incoming request.
  14. *
  15. * @param Request $request
  16. * @param Closure $next
  17. *
  18. * @return Response
  19. */
  20. public function handle($request, Closure $next)
  21. {
  22. $response = $next($request);
  23. if (!$request->pjax() || $response->isRedirection() || Admin::guard()->guest()) {
  24. return $response;
  25. }
  26. if (!$response->isSuccessful()) {
  27. return $this->handleErrorResponse($response);
  28. }
  29. try {
  30. $this->setUriHeader($response, $request);
  31. } catch (\Exception $exception) {
  32. }
  33. return $response;
  34. }
  35. /**
  36. * Send a response through this middleware.
  37. *
  38. * @param Response $response
  39. */
  40. public static function respond(Response $response)
  41. {
  42. $next = function () use ($response) {
  43. return $response;
  44. };
  45. (new static())->handle(Request::capture(), $next)->send();
  46. exit;
  47. }
  48. /**
  49. * Handle Response with exceptions.
  50. *
  51. * @param Response $response
  52. *
  53. * @return \Illuminate\Http\RedirectResponse
  54. */
  55. protected function handleErrorResponse(Response $response)
  56. {
  57. if (config('app.debug')) {
  58. throw $response->exception;
  59. }
  60. $exception = $response->exception;
  61. $error = new MessageBag([
  62. 'type' => get_class($exception),
  63. 'message' => $exception->getMessage(),
  64. 'file' => $exception->getFile(),
  65. 'line' => $exception->getLine(),
  66. ]);
  67. return back()->withInput()->withErrors($error, 'exception');
  68. }
  69. /**
  70. * Prepare the PJAX-specific response content.
  71. *
  72. * @param Response $response
  73. * @param string $container
  74. *
  75. * @return $this
  76. */
  77. protected function filterResponse(Response $response, $container)
  78. {
  79. $crawler = new Crawler($response->getContent());
  80. $response->setContent(
  81. $this->makeTitle($crawler).
  82. $this->fetchContents($crawler, $container)
  83. );
  84. return $this;
  85. }
  86. /**
  87. * Prepare an HTML title tag.
  88. *
  89. * @param Crawler $crawler
  90. *
  91. * @return string
  92. */
  93. protected function makeTitle($crawler)
  94. {
  95. $pageTitle = $crawler->filter('head > title')->html();
  96. return "<title>{$pageTitle}</title>";
  97. }
  98. /**
  99. * Fetch the PJAX-specific HTML from the response.
  100. *
  101. * @param Crawler $crawler
  102. * @param string $container
  103. *
  104. * @return string
  105. */
  106. protected function fetchContents($crawler, $container)
  107. {
  108. $content = $crawler->filter($container);
  109. if (!$content->count()) {
  110. abort(422);
  111. }
  112. return $this->decodeUtf8HtmlEntities($content->html());
  113. }
  114. /**
  115. * Decode utf-8 characters to html entities.
  116. *
  117. * @param string $html
  118. *
  119. * @return string
  120. */
  121. protected function decodeUtf8HtmlEntities($html)
  122. {
  123. return preg_replace_callback('/(&#[0-9]+;)/', function ($html) {
  124. return mb_convert_encoding($html[1], 'UTF-8', 'HTML-ENTITIES');
  125. }, $html);
  126. }
  127. /**
  128. * Set the PJAX-URL header to the current uri.
  129. *
  130. * @param Response $response
  131. * @param Request $request
  132. */
  133. protected function setUriHeader(Response $response, Request $request)
  134. {
  135. $response->header(
  136. 'X-PJAX-URL',
  137. $request->getRequestUri()
  138. );
  139. }
  140. }