-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathServerManager.php
More file actions
112 lines (95 loc) · 2.8 KB
/
ServerManager.php
File metadata and controls
112 lines (95 loc) · 2.8 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
<?php
declare(strict_types=1);
namespace Pest\Browser;
use Pest\Browser\Contracts\HttpServer;
use Pest\Browser\Contracts\PlaywrightServer;
use Pest\Browser\Drivers\LaravelHttpServer;
use Pest\Browser\Drivers\NullableHttpServer;
use Pest\Browser\Playwright\Servers\AlreadyStartedPlaywrightServer;
use Pest\Browser\Playwright\Servers\PlaywrightNpmServer;
use Pest\Browser\Support\PackageJsonDirectory;
use Pest\Browser\Support\Port;
use Pest\Plugins\Parallel;
/**
* @internal
*
* @codeCoverageIgnore
*/
final class ServerManager
{
/**
* The default host for the server.
*/
public const string DEFAULT_HOST = '127.0.0.1';
/**
* The singleton instance of the server manager.
*/
private static ?ServerManager $instance = null;
/**
* The Playwright server process.
*/
private ?PlaywrightServer $playwright = null;
/**
* The HTTP server process.
*/
private ?HttpServer $http = null;
/**
* The HTTP server class.
*
* @param class-string<HttpServer> $class
*/
private static ?string $httpServerClass = null;
/**
* Gets the singleton instance of the server manager.
*/
public static function instance(): self
{
return self::$instance ??= new self();
}
/**
* Sets the browsers http server class.
*
* @param class-string<HttpServer> $class
*/
public static function setHttpServerClass(string $class): void
{
self::$httpServerClass = $class;
}
/**
* Returns the Playwright server process instance.
*/
public function playwright(): PlaywrightServer
{
if (Parallel::isWorker()) {
return AlreadyStartedPlaywrightServer::fromPersisted();
}
$port = Port::find();
$this->playwright ??= PlaywrightNpmServer::create(
PackageJsonDirectory::find(),
'.'.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR.'playwright run-server --host %s --port %d --mode launchServer',
self::DEFAULT_HOST,
$port,
'Listening on',
);
AlreadyStartedPlaywrightServer::persist(
self::DEFAULT_HOST,
$port,
);
return $this->playwright;
}
/**
* Returns the HTTP server process instance.
*/
public function http(): HttpServer
{
$httpServer = self::$httpServerClass !== null ? new self::$httpServerClass(self::DEFAULT_HOST, Port::find()) : null;
return $this->http ??= match (true) {
$httpServer instanceof HttpServer => $httpServer,
function_exists('app_path') => new LaravelHttpServer(
self::DEFAULT_HOST,
Port::find(),
),
default => new NullableHttpServer(),
};
}
}