Skip to content

Commit 2ec1e86

Browse files
author
ahmard
committed
StartedServer.php Renamed to RunningServer.php
StartedServerInterface.php removed Process logs can be forwarded to console ServerInterface::start() now returns an instance of RunningServer
1 parent 0e01d1a commit 2ec1e86

13 files changed

+55
-57
lines changed

bin/react.php

+3
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@
1414

1515
$socket = new SocketServer("{$serverInfo->getHost()}:{$serverInfo->getPort()}");
1616
$http = new HttpServer($serverInfo->getRequestCallback());
17+
18+
echo "Server started";
19+
1720
$http->listen($socket);

composer.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"php": "^8.0",
88
"react/child-process": "^0.6.3",
99
"react/http": "^1.5",
10-
"opis/closure": "^3.6"
10+
"opis/closure": "^3.6",
11+
"symfony/console": "^5.3"
1112
},
1213
"require-dev": {
1314
"phpstan/phpstan": "^0.12.98",
@@ -24,5 +25,9 @@
2425
"name": "ahmard",
2526
"email": "[email protected]"
2627
}
27-
]
28+
],
29+
"scripts": {
30+
"analyse": "phpstan analyse",
31+
"analyze": "@analyse"
32+
}
2833
}

phpstan.neon

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
level: 6
3+
checkMissingIterableValueType: false
4+
paths:
5+
- bin
6+
- src
7+
ignoreErrors:
8+
- '#Attribute class JetBrains.* does not exist\.#'

src/BuiltIn/Server.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use PHPServer\ServerInfo;
99
use PHPServer\ServerInterface;
1010
use PHPServer\ServerProcess;
11-
use PHPServer\StartedServer;
12-
use PHPServer\StartedServerInterface;
11+
use PHPServer\RunningServer;
1312
use PHPServer\Terminal;
1413
use function PHPServer\base_path;
1514

@@ -29,7 +28,7 @@ public function __construct(
2928

3029
#[Pure] public static function create(string $host, int $port): static
3130
{
32-
return new Server($host, $port);
31+
return new static($host, $port);
3332
}
3433

3534
public function getCommand(): ServerCommand
@@ -88,8 +87,8 @@ public function onRequest(callable $callback): static
8887
return $this;
8988
}
9089

91-
public function start(): StartedServerInterface
90+
public function start(): RunningServer
9291
{
93-
return new StartedServer($this, ServerProcess::create($this));
92+
return new RunningServer($this, ServerProcess::create($this));
9493
}
9594
}

src/React/Server.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use PHPServer\ServerInfo;
99
use PHPServer\ServerInterface;
1010
use PHPServer\ServerProcess;
11-
use PHPServer\StartedServer;
12-
use PHPServer\StartedServerInterface;
11+
use PHPServer\RunningServer;
1312
use PHPServer\Terminal;
1413
use function PHPServer\base_path;
1514

@@ -28,7 +27,7 @@ public function __construct(
2827

2928
#[Pure] public static function create(string $host, int $port): static
3029
{
31-
return new Server($host, $port);
30+
return new static($host, $port);
3231
}
3332

3433
public function setDocumentRoot(string $path): static
@@ -43,9 +42,9 @@ public function onRequest(callable $callback): static
4342
return $this;
4443
}
4544

46-
public function start(): StartedServerInterface
45+
public function start(): RunningServer
4746
{
48-
return new StartedServer($this, ServerProcess::create($this));
47+
return new RunningServer($this, ServerProcess::create($this));
4948
}
5049

5150
public function getCommand(): ServerCommand

src/StartedServer.php renamed to src/RunningServer.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace PHPServer;
44

55
use React\ChildProcess\Process;
6+
use React\Stream\WritableResourceStream;
67

7-
class StartedServer implements StartedServerInterface
8+
class RunningServer
89
{
910
public function __construct(
1011
protected ServerInterface $server,
@@ -13,6 +14,17 @@ public function __construct(
1314
{
1415
}
1516

17+
public function logOutputToConsole(): static
18+
{
19+
$stdout = new WritableResourceStream(STDOUT);
20+
$stderr = new WritableResourceStream(STDERR);
21+
22+
$this->getProcess()->stdout->pipe($stdout);
23+
$this->getProcess()->stderr->pipe($stderr);
24+
25+
return $this;
26+
}
27+
1628
/**
1729
* @return Process
1830
*/

src/ServerInfo.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class ServerInfo
1515
protected Closure|null $requestCallback = null;
1616

1717

18-
#[Pure] public static function create(): static
18+
#[Pure] public static function create(): ServerInfo
1919
{
20-
return new static();
20+
return new ServerInfo();
2121
}
2222

2323
/**
@@ -32,7 +32,7 @@ public function getHost(): string
3232
* @param string $host
3333
* @return ServerInfo
3434
*/
35-
public function setHost(string $host): static
35+
public function setHost(string $host): ServerInfo
3636
{
3737
$this->host = $host;
3838
return $this;
@@ -50,7 +50,7 @@ public function getPort(): int
5050
* @param int $port
5151
* @return ServerInfo
5252
*/
53-
public function setPort(int $port): static
53+
public function setPort(int $port): ServerInfo
5454
{
5555
$this->port = $port;
5656
return $this;
@@ -68,7 +68,7 @@ public function getDocumentRoot(): ?string
6868
* @param string|null $documentRoot
6969
* @return ServerInfo
7070
*/
71-
public function setDocumentRoot(?string $documentRoot): static
71+
public function setDocumentRoot(?string $documentRoot): ServerInfo
7272
{
7373
$this->documentRoot = $documentRoot;
7474
return $this;
@@ -86,7 +86,7 @@ public function getRouterScript(): ?string
8686
* @param string|null $routerScript
8787
* @return ServerInfo
8888
*/
89-
public function setRouterScript(?string $routerScript): static
89+
public function setRouterScript(?string $routerScript): ServerInfo
9090
{
9191
$this->routerScript = $routerScript;
9292
return $this;
@@ -104,7 +104,7 @@ public function getRequestCallback(): ?Closure
104104
* @param Closure|null $requestCallback
105105
* @return ServerInfo
106106
*/
107-
public function setRequestCallback(?Closure $requestCallback): static
107+
public function setRequestCallback(?Closure $requestCallback): ServerInfo
108108
{
109109
$this->requestCallback = $requestCallback;
110110
return $this;

src/ServerInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function setDocumentRoot(string $path): static;
1212

1313
public function onRequest(callable $callback): static;
1414

15-
public function start(): StartedServerInterface;
15+
public function start(): RunningServer;
1616

1717
public function getCommand(): ServerCommand;
1818

src/ServerProcess.php

+1-11
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,9 @@ public static function create(ServerInterface $server): Process
1111
{
1212
$command = self::constructCommand($server->getCommand());
1313

14-
var_dump(exec($command));
15-
die;
16-
$process = new Process('echo foo');
14+
$process = new Process($command);
1715
$process->start();
1816

19-
$process->stdout->on('data', function ($chunk) {
20-
// echo $chunk;
21-
});
22-
23-
$process->on('exit', function ($exitCode, $termSignal) {
24-
//echo 'Process exited with code ' . $exitCode . PHP_EOL;
25-
});
26-
2717
return $process;
2818
}
2919

src/StartedServerInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
interface StartedServerInterface
88
{
9+
public function logOutputToConsole(): static;
10+
911
public function getProcess(): Process;
1012

1113
public function getServer(): ServerInterface;

src/Terminal.php

-14
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@
1010

1111
class Terminal
1212
{
13-
public static function getJsonDecodedArg(
14-
string $shortName,
15-
string $logName,
16-
bool $associative = false,
17-
int $depth = 512,
18-
int $flags = 0
19-
)
20-
{
21-
$json = self::getArgument($shortName, $logName);
22-
if (!$json) return null;
23-
24-
return json_decode($json, $associative, $depth, $flags);
25-
}
26-
2713
public static function getArgument(string $shortName, string|null $longName = null): string|null
2814
{
2915
$optValue = getopt(

test-builtin.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44

55
require 'vendor/autoload.php';
66

7-
Server::create('127.0.0.1', '9901')
8-
->setDocumentRoot(__DIR__)
9-
->start();
10-
11-
Server::create('127.0.0.1', '9902')
12-
->setRouterScript('index.php')
13-
->start();
14-
157
Server::create('127.0.0.1', '9903')
168
->onRequest(fn() => var_dump('Request Received'))
17-
->start();
9+
->start()
10+
->logOutputToConsole();

test-react.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
return new Response(200, ['Content-Type' => 'text/html'], $html);
1414
};
1515

16-
Server::create('127.0.0.1', 9001)
16+
Server::create('127.0.0.1', 9903)
1717
->onRequest($handler)
18-
->start();
18+
->start()
19+
->logOutputToConsole();

0 commit comments

Comments
 (0)