-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathExecuteJavascriptMiddleware.php
More file actions
141 lines (117 loc) · 4.06 KB
/
ExecuteJavascriptMiddleware.php
File metadata and controls
141 lines (117 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
/**
* Copyright (c) 2024 Kai Sassnowski
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/roach-php/roach
*/
namespace RoachPHP\Downloader\Middleware;
use GuzzleHttp\Psr7\Response as Psr7Response;
use Psr\Log\LoggerInterface;
use RoachPHP\Http\Request;
use RoachPHP\Http\Response;
use RoachPHP\Support\Configurable;
use Spatie\Browsershot\Browsershot;
final class ExecuteJavascriptMiddleware implements RequestMiddlewareInterface
{
use Configurable;
/**
* @var callable(string): Browsershot
*/
private $getBrowsershot;
/**
* @param null|callable(string): Browsershot $getBrowsershot
*/
public function __construct(
private LoggerInterface $logger,
?callable $getBrowsershot = null,
) {
$this->getBrowsershot = $getBrowsershot ?? static fn (string $uri): Browsershot => Browsershot::url($uri)->waitUntilNetworkIdle();
}
public function handleRequest(Request $request): Request
{
$browsershot = $this->configureBrowsershot(
$request->getUri(),
);
try {
$body = $browsershot->bodyHtml();
} catch (\Throwable $e) {
$this->logger->info('[ExecuteJavascriptMiddleware] Error while executing javascript', [
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return $request->drop('Error while executing javascript');
}
return $request->withResponse(
$this->makeResponse($request, $body),
);
}
private function makeResponse(Request $request, string $body): Response
{
return new Response(
new Psr7Response(200, [], $body),
$request,
);
}
/**
* @psalm-suppress MixedArgument, MixedAssignment
*/
private function configureBrowsershot(string $uri): Browsershot
{
$browsershot = ($this->getBrowsershot)($uri);
if (!empty($this->option('chromiumArguments'))) {
/** @phpstan-ignore argument.type */
$browsershot->addChromiumArguments($this->option('chromiumArguments'));
}
if (null !== ($chromePath = $this->option('chromePath'))) {
/** @phpstan-ignore argument.type */
$browsershot->setChromePath($chromePath);
}
if (null !== ($binPath = $this->option('binPath'))) {
/** @phpstan-ignore argument.type */
$browsershot->setBinPath($binPath);
}
if (null !== ($nodeModulePath = $this->option('nodeModulePath'))) {
/** @phpstan-ignore argument.type */
$browsershot->setNodeModulePath($nodeModulePath);
}
if (null !== ($includePath = $this->option('includePath'))) {
/** @phpstan-ignore argument.type */
$browsershot->setIncludePath($includePath);
}
if (null !== ($nodeBinary = $this->option('nodeBinary'))) {
/** @phpstan-ignore argument.type */
$browsershot->setNodeBinary($nodeBinary);
}
if (null !== ($npmBinary = $this->option('npmBinary'))) {
/** @phpstan-ignore argument.type */
$browsershot->setNpmBinary($npmBinary);
}
if (null !== ($userAgent = $this->option('userAgent'))) {
/** @phpstan-ignore argument.type */
$browsershot->userAgent($userAgent);
}
if (!empty($delay = $this->option('delay'))) {
/** @phpstan-ignore argument.type */
$browsershot->setDelay($delay);
}
return $browsershot;
}
private function defaultOptions(): array
{
return [
'chromiumArguments' => [],
'chromePath' => null,
'binPath' => null,
'nodeModulePath' => null,
'includePath' => null,
'nodeBinary' => null,
'npmBinary' => null,
'userAgent' => null,
'delay' => 0,
];
}
}