1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Pjax\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Http\Response; |
8
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response as BaseResponse; |
10
|
|
|
|
11
|
|
|
class FilterIfPjax |
12
|
|
|
{ |
13
|
|
|
/** @var \Symfony\Component\DomCrawler\Crawler */ |
14
|
|
|
protected $crawler; |
15
|
|
|
|
16
|
|
|
public function handle(Request $request, Closure $next): BaseResponse |
17
|
|
|
{ |
18
|
|
|
$response = $next($request); |
19
|
|
|
|
20
|
|
|
if (! $request->pjax() || $response->isRedirection()) { |
21
|
|
|
return $response; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$this->filterResponse($response, $request->header('X-PJAX-Container')) |
25
|
|
|
->setUriHeader($response, $request) |
26
|
|
|
->setVersionHeader($response, $request); |
27
|
|
|
|
28
|
|
|
return $response; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function filterResponse(BaseResponse $response, $container): self |
32
|
|
|
{ |
33
|
|
|
$crawler = $this->getCrawler($response); |
34
|
|
|
|
35
|
|
|
$response->setContent( |
36
|
|
|
$this->makeTitle($crawler). |
37
|
|
|
$this->fetchContainer($crawler, $container) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function makeTitle(Crawler $crawler): ?string |
44
|
|
|
{ |
45
|
|
|
$pageTitle = $crawler->filter('head > title'); |
46
|
|
|
|
47
|
|
|
if (! $pageTitle->count()) { |
48
|
|
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return "<title>{$pageTitle->html()}</title>"; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function fetchContainer(Crawler $crawler, $container): string |
55
|
|
|
{ |
56
|
|
|
$content = $crawler->filter($container); |
57
|
|
|
|
58
|
|
|
if (! $content->count()) { |
59
|
|
|
abort(422); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $content->html(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function setUriHeader(Response $response, Request $request): self |
66
|
|
|
{ |
67
|
|
|
$response->header('X-PJAX-URL', $request->getRequestUri()); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function setVersionHeader(Response $response, Request $request): self |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$crawler = $this->getCrawler($this->createResponseWithLowerCaseContent($response)); |
75
|
|
|
$node = $crawler->filter('head > meta[http-equiv="x-pjax-version"]'); |
76
|
|
|
|
77
|
|
|
if ($node->count()) { |
78
|
|
|
$response->header('x-pjax-version', $node->attr('content')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getCrawler(BaseResponse $response): Crawler |
85
|
|
|
{ |
86
|
|
|
if ($this->crawler) { |
87
|
|
|
return $this->crawler; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->crawler = new Crawler($response->getContent()); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function createResponseWithLowerCaseContent(Response $response): Response |
94
|
|
|
{ |
95
|
|
|
$lowercaseContent = strtolower($response->getContent()); |
96
|
|
|
|
97
|
|
|
return Response::create($lowercaseContent); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.