Skip to content

Commit e7b3f0a

Browse files
author
ahmard
committed
Swoole server ready
Console helper ready for all Small touches across the system
1 parent 32c171e commit e7b3f0a

18 files changed

+75
-156
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/.idea/
22
/vendor/
33
/composer.lock
4-
/bin/.pid
4+
/bin/.swoole-pid
55
.env

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
# PHP Server
2-
A small library to help run PHP servers easily and quickly.
2+
3+
A small library to help run PHP servers easily and quickly.
34

45
## Installation
6+
57
```
68
composer require ahmard/php-server
79
```
810

911
## Usage
12+
1013
### PHP Built-In Server
14+
1115
An implementation of [Built-In Server](https://www.php.net/manual/en/features.commandline.webserver.php)
1216

1317
- With document root
18+
1419
```php
1520
use PHPServer\BuiltIn\Server;
1621

@@ -21,6 +26,7 @@ Server::create('127.0.0.1', '9900')
2126
```
2227

2328
- Route request to single entry file
29+
2430
```php
2531
use PHPServer\BuiltIn\Server;
2632

@@ -30,6 +36,7 @@ Server::create('127.0.0.1', '9900')
3036
```
3137

3238
- Provide callable to be invoked when request is received
39+
3340
```php
3441
use PHPServer\BuiltIn\Server;
3542

@@ -38,8 +45,8 @@ Server::create('127.0.0.1', '9900')
3845
->start();
3946
```
4047

41-
4248
### ReactPHP
49+
4350
An implementation of [ReactPHP](https://reactphp.org)
4451

4552
```php
@@ -63,6 +70,7 @@ Server::create('127.0.0.1', 9001)
6370
```
6471

6572
### Swoole
73+
6674
An implementation of [Swoole](https://swoole.co.uk)
6775

6876
```php
@@ -79,7 +87,6 @@ $handler = function (Request $request) {
7987
};
8088

8189
Server::create('127.0.0.1', 9904)
82-
->watchFilesystemChanges([__DIR__])
8390
->onRequest($handler)
8491
->setServerConfig([
8592
'enable_static_handler' => true,
@@ -89,4 +96,27 @@ Server::create('127.0.0.1', 9904)
8996
])
9097
->start()
9198
->logOutputToConsole();
99+
```
100+
101+
Swoole with filesystem watcher
102+
103+
```php
104+
use PHPServer\Swoole\Http\Request;
105+
use PHPServer\Swoole\Server;
106+
107+
require 'vendor/autoload.php';
108+
109+
Server::create('127.0.0.1', 9904)
110+
->watchFilesystemChanges([__DIR__])
111+
->onRequest(function (Request $request){
112+
$request->response()->html('Hello');
113+
})
114+
->setServerConfig([
115+
'enable_static_handler' => true,
116+
'http_parse_post' => true,
117+
'worker_num' => 8,
118+
'package_max_length' => 10 * 1024 * 1024
119+
])
120+
->start()
121+
->logOutputToConsole();
92122
```

bin/built-in.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
/**@var ServerInfo $serverInfo * */
1111
$serverInfo = $arguments['info'];
1212

13-
1413
if (null !== $serverInfo->getRequestCallback()) {
1514
$serverInfo->getRequestCallback()();
1615
}

bin/react.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
$serverInfo = $arguments['info'];
1414

1515
$socket = new SocketServer("{$serverInfo->getHost()}:{$serverInfo->getPort()}");
16-
$http = new HttpServer($serverInfo->getRequestCallback());
16+
$httpServer = new HttpServer($serverInfo->getRequestCallback());
1717

18-
echo "Server started";
18+
echo "ReactPHP server started at http://{$serverInfo->getHost()}:{$serverInfo->getPort()}\n";
1919

20-
$http->listen($socket);
20+
$httpServer->listen($socket);

bin/swoole.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

33
use Dotenv\Dotenv;
4+
use PHPServer\Console;
45
use PHPServer\Env;
56
use PHPServer\ServerInfo;
6-
use PHPServer\Swoole\Console;
77
use PHPServer\Swoole\Http\Request;
88
use PHPServer\Swoole\Http\Response;
99
use PHPServer\Terminal;
@@ -62,7 +62,7 @@ function (SWRequest $swRequest, SWResponse $swResponse) use ($serverInfo): void
6262
$server->on('start', function (Server $server) use ($serverInfo, $swooleData) {
6363
Console::info("Swoole server started at http://{$serverInfo->getHost()}:{$serverInfo->getPort()}");
6464

65-
file_put_contents(__DIR__ . '/.pid', $server->getMasterPid());
65+
file_put_contents(__DIR__ . '/.swoole-pid', $server->getMasterPid());
6666

6767
//Run file watcher
6868
if (!empty($swooleData['watch_filesystem_changes'])) {

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
"require-dev": {
1818
"phpstan/phpstan": "^0.12.98",
1919
"symfony/var-dumper": "^5.3",
20-
"swoole/ide-helper": "^4.7",
21-
"ahmard/swotch": "^0.0.1"
20+
"swoole/ide-helper": "^4.7"
2221
},
2322
"autoload": {
2423
"psr-4": {

src/Swoole/Console.php renamed to src/Console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33

4-
namespace PHPServer\Swoole;
4+
namespace PHPServer;
55

66

77
use Symfony\Component\Console\Output\ConsoleOutput;

src/Swoole/Http/Middleware.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Swoole/Http/Middlewares/MiddlewareInterface.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Swoole/Http/Request.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPServer\Env;
99
use RingCentral\Psr7\Uri;
1010
use Swoole\Http\Request as SWRequest;
11-
use Throwable;
1211

1312

1413
/**
@@ -20,7 +19,6 @@ class Request
2019
{
2120
protected ?array $parsedJsonBody = null;
2221
protected bool $expectsJson = false;
23-
protected RequestMiddleware $middleware;
2422
protected Uri $uri;
2523

2624

@@ -75,16 +73,6 @@ public function __call(string $name, array $arguments): mixed
7573
return $this->request->$name(...$arguments);
7674
}
7775

78-
/**
79-
* @throws Throwable
80-
* @throws JsonException
81-
*/
82-
public function initMiddleware(array $middlewares): void
83-
{
84-
$this->middleware = new RequestMiddleware($this, $middlewares);
85-
$this->middleware->current()->handle($this);
86-
}
87-
8876
/**
8977
* Get http url params
9078
*
@@ -191,11 +179,11 @@ public function response(): Response
191179
return $this->response;
192180
}
193181

194-
public function middleware(): RequestMiddleware
195-
{
196-
return $this->middleware;
197-
}
198-
182+
/**
183+
* Returns an instance of swoole request class
184+
*
185+
* @return SWRequest
186+
*/
199187
public function getSwooleRequest(): SWRequest
200188
{
201189
return $this->request;

src/Swoole/Http/RequestMiddleware.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Swoole/Http/Response.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,14 @@ public function html(string $htmlCode, int $status = 200, array $headers = []):
173173
);
174174
}
175175
}
176+
177+
/**
178+
* Returns an instance of swoole response class
179+
*
180+
* @return SWResponse
181+
*/
182+
public function getSwooleResponse(): SWResponse
183+
{
184+
return $this->response;
185+
}
176186
}

src/Swoole/Kernel.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Swoole/Server.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use PHPServer\AbstractServer;
66
use PHPServer\ServerCommand;
77
use PHPServer\Terminal;
8+
use RuntimeException;
9+
use Swotch\Watcher;
810
use function PHPServer\base_path;
911

1012
class Server extends AbstractServer
@@ -21,8 +23,18 @@ public static function cleanSwooleServerConfig(array $config): array
2123
return $config;
2224
}
2325

26+
/**
27+
* This will require an installation of "ahmard/swotch" package
28+
*
29+
* @param array $paths
30+
* @return $this
31+
*/
2432
public function watchFilesystemChanges(array $paths): static
2533
{
34+
if (!class_exists(Watcher::class)) {
35+
throw new RuntimeException('Filesystem watcher requires an installation of "ahmard/swotch"');
36+
}
37+
2638
$this->serverConfig['watch_filesystem_changes'] = $paths;
2739
return $this;
2840
}

0 commit comments

Comments
 (0)