1 | <?php |
||||||
2 | |||||||
3 | namespace Yeelight\Http\Middleware; |
||||||
4 | |||||||
5 | use Closure; |
||||||
6 | use Illuminate\Http\Request; |
||||||
7 | use Illuminate\Support\Facades\Auth; |
||||||
8 | use Illuminate\Support\MessageBag; |
||||||
9 | use Symfony\Component\DomCrawler\Crawler; |
||||||
10 | use Symfony\Component\HttpFoundation\Response; |
||||||
11 | |||||||
12 | class Pjax |
||||||
13 | { |
||||||
14 | /** |
||||||
15 | * Handle an incoming request. |
||||||
16 | * |
||||||
17 | * @param Request $request |
||||||
18 | * @param Closure $next |
||||||
19 | * |
||||||
20 | * @return Response |
||||||
21 | */ |
||||||
22 | public function handle($request, Closure $next) |
||||||
23 | { |
||||||
24 | $response = $next($request); |
||||||
25 | |||||||
26 | if (!$request->pjax() || $response->isRedirection() || Auth::guard(config('yeelight.backend.route.prefix'))->guest()) { |
||||||
27 | return $response; |
||||||
28 | } |
||||||
29 | |||||||
30 | if (!$response->isSuccessful()) { |
||||||
31 | return $this->handleErrorResponse($response); |
||||||
32 | } |
||||||
33 | |||||||
34 | try { |
||||||
35 | $this->filterResponse($response, $request->header('X-PJAX-CONTAINER')) |
||||||
36 | ->setUriHeader($response, $request); |
||||||
37 | } catch (\Exception $exception) { |
||||||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
![]() |
|||||||
38 | } |
||||||
39 | |||||||
40 | return $response; |
||||||
41 | } |
||||||
42 | |||||||
43 | /** |
||||||
44 | * Send a response through this middleware. |
||||||
45 | * |
||||||
46 | * @param Response $response |
||||||
47 | */ |
||||||
48 | public static function respond(Response $response) |
||||||
49 | { |
||||||
50 | $next = function () use ($response) { |
||||||
51 | return $response; |
||||||
52 | }; |
||||||
53 | |||||||
54 | (new static())->handle(Request::capture(), $next)->send(); |
||||||
55 | |||||||
56 | exit; |
||||||
0 ignored issues
–
show
|
|||||||
57 | } |
||||||
58 | |||||||
59 | /** |
||||||
60 | * Handle Response with exceptions. |
||||||
61 | * |
||||||
62 | * @param Response $response |
||||||
63 | * |
||||||
64 | * @return \Illuminate\Http\RedirectResponse |
||||||
65 | */ |
||||||
66 | protected function handleErrorResponse(Response $response) |
||||||
67 | { |
||||||
68 | $exception = $response->exception; |
||||||
69 | |||||||
70 | $error = new MessageBag([ |
||||||
71 | 'type' => get_class($exception), |
||||||
72 | 'message' => $exception->getMessage(), |
||||||
73 | 'file' => $exception->getFile(), |
||||||
74 | 'line' => $exception->getLine(), |
||||||
75 | ]); |
||||||
76 | |||||||
77 | return back()->withInput()->withErrors($error, 'exception'); |
||||||
78 | } |
||||||
79 | |||||||
80 | /** |
||||||
81 | * Prepare the PJAX-specific response content. |
||||||
82 | * |
||||||
83 | * @param Response $response |
||||||
84 | * @param string $container |
||||||
85 | * |
||||||
86 | * @return $this |
||||||
87 | */ |
||||||
88 | protected function filterResponse(Response $response, $container) |
||||||
89 | { |
||||||
90 | $crawler = new Crawler($response->getContent()); |
||||||
91 | |||||||
92 | $response->setContent( |
||||||
93 | $this->makeTitle($crawler). |
||||||
94 | $this->fetchContents($crawler, $container) |
||||||
95 | ); |
||||||
96 | |||||||
97 | return $this; |
||||||
98 | } |
||||||
99 | |||||||
100 | /** |
||||||
101 | * Prepare an HTML title tag. |
||||||
102 | * |
||||||
103 | * @param Crawler $crawler |
||||||
104 | * |
||||||
105 | * @return string |
||||||
106 | */ |
||||||
107 | protected function makeTitle($crawler) |
||||||
108 | { |
||||||
109 | $pageTitle = $crawler->filter('head > title')->html(); |
||||||
110 | |||||||
111 | return "<title>{$pageTitle}</title>"; |
||||||
112 | } |
||||||
113 | |||||||
114 | /** |
||||||
115 | * Fetch the PJAX-specific HTML from the response. |
||||||
116 | * |
||||||
117 | * @param Crawler $crawler |
||||||
118 | * @param string $container |
||||||
119 | * |
||||||
120 | * @return string |
||||||
121 | */ |
||||||
122 | protected function fetchContents($crawler, $container) |
||||||
123 | { |
||||||
124 | $content = $crawler->filter($container); |
||||||
125 | |||||||
126 | if (!$content->count()) { |
||||||
127 | abort(422); |
||||||
128 | } |
||||||
129 | |||||||
130 | return $this->decodeUtf8HtmlEntities($content->html()); |
||||||
131 | } |
||||||
132 | |||||||
133 | /** |
||||||
134 | * Decode utf-8 characters to html entities. |
||||||
135 | * |
||||||
136 | * @param string $html |
||||||
137 | * |
||||||
138 | * @return string |
||||||
139 | */ |
||||||
140 | protected function decodeUtf8HtmlEntities($html) |
||||||
141 | { |
||||||
142 | return preg_replace_callback('/(&#[0-9]+;)/', function ($html) { |
||||||
143 | return mb_convert_encoding($html[1], 'UTF-8', 'HTML-ENTITIES'); |
||||||
144 | }, $html); |
||||||
145 | } |
||||||
146 | |||||||
147 | /** |
||||||
148 | * Set the PJAX-URL header to the current uri. |
||||||
149 | * |
||||||
150 | * @param Response $response |
||||||
151 | * @param Request $request |
||||||
152 | */ |
||||||
153 | protected function setUriHeader(Response $response, Request $request) |
||||||
154 | { |
||||||
155 | $response->header( |
||||||
0 ignored issues
–
show
The method
header() does not exist on Symfony\Component\HttpFoundation\Response . It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
156 | 'X-PJAX-URL', |
||||||
157 | $request->getRequestUri() |
||||||
158 | ); |
||||||
159 | } |
||||||
160 | } |
||||||
161 |