Skip to content

Commit d57d94c

Browse files
committed
Beautification fixes
1 parent c113f46 commit d57d94c

File tree

7 files changed

+110
-183
lines changed

7 files changed

+110
-183
lines changed

README.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,27 @@ require_once(__DIR__.'/vendor/autoload.php');
5252
$app = Flight::app();
5353

5454
$app->route('/', function() use ($app) {
55-
$app->json([
56-
'hello' => 'world'
57-
]);
55+
$app->json([
56+
'hello' => 'world'
57+
]);
5858
});
5959

6060
if(!defined("NOT_SWOOLE")) {
61-
// Require the SwooleServerDriver class since we're running in Swoole mode.
62-
require_once(__DIR__.'/SwooleServerDriver.php');
63-
64-
// Custom little hack
65-
// Makes it so the app doesn't stop when it runs.
66-
$app->map('stop', function (?int $code = null) use ($app) {
67-
if ($code !== null) {
68-
$app->response()->status($code);
69-
}
70-
});
71-
Swoole\Runtime::enableCoroutine();
72-
$Swoole_Server = new SwooleServerDriver('127.0.0.1', 9501, $app);
73-
$Swoole_Server->start();
61+
// Require the SwooleServerDriver class since we're running in Swoole mode.
62+
require_once(__DIR__.'/SwooleServerDriver.php');
63+
64+
// Custom little hack
65+
// Makes it so the app doesn't stop when it runs.
66+
$app->map('stop', function (?int $code = null) use ($app) {
67+
if ($code !== null) {
68+
$app->response()->status($code);
69+
}
70+
});
71+
Swoole\Runtime::enableCoroutine();
72+
$Swoole_Server = new SwooleServerDriver('127.0.0.1', 9501, $app);
73+
$Swoole_Server->start();
7474
} else {
75-
$app->start();
75+
$app->start();
7676
}
7777
```
7878

@@ -90,26 +90,26 @@ use Swoole\HTTP\Response as SwooleResponse;
9090

9191
class SwooleServerDriver {
9292

93-
//use ConnectionPoolTrait;
93+
//use ConnectionPoolTrait;
9494

95-
/** @var SwooleServer */
95+
/** @var SwooleServer */
9696
protected $Swoole;
9797

98-
/** @var Engine */
99-
protected $app;
98+
/** @var Engine */
99+
protected $app;
100100

101101
public function __construct(string $host, int $port, Engine $app) {
102102
$this->Swoole = new SwooleServer($host, $port);
103-
$this->app = $app;
103+
$this->app = $app;
104104

105105
$this->setDefault();
106106
$this->bindWorkerEvents();
107107
$this->bindHttpEvent();
108108
}
109109

110-
protected function setDefault() {
111-
// A bunch of default settings for the Swoole server.
112-
// You can customize these settings based on your needs.
110+
protected function setDefault() {
111+
// A bunch of default settings for the Swoole server.
112+
// You can customize these settings based on your needs.
113113
$this->Swoole->set([
114114
'daemonize' => false,
115115
'dispatch_mode' => 1,
@@ -126,34 +126,34 @@ class SwooleServerDriver {
126126
]);
127127
}
128128

129-
protected function bindHttpEvent() {
130-
$app = $this->app;
131-
$AsyncBridge = new AsyncBridge($app);
132-
$this->Swoole->on("Start", function(SwooleServer $server) {
133-
echo "Swoole http server is started at http://127.0.0.1:9501\n";
134-
});
129+
protected function bindHttpEvent() {
130+
$app = $this->app;
131+
$AsyncBridge = new AsyncBridge($app);
132+
$this->Swoole->on("Start", function(SwooleServer $server) {
133+
echo "Swoole http server is started at http://127.0.0.1:9501\n";
134+
});
135135

136-
// This is where the magic happens, the request is processed by the AsyncBridge
136+
// This is where the magic happens, the request is processed by the AsyncBridge
137137
$this->Swoole->on('Request', function (SwooleRequest $request, SwooleResponse $response) use ($AsyncBridge) {
138-
$SwooleAsyncRequest = new SwooleAsyncRequest($request);
139-
$SwooleAsyncResponse = new SwooleAsyncResponse($response);
140-
$AsyncBridge->processRequest($SwooleAsyncRequest, $SwooleAsyncResponse);
141-
$response->end();
138+
$SwooleAsyncRequest = new SwooleAsyncRequest($request);
139+
$SwooleAsyncResponse = new SwooleAsyncResponse($response);
140+
$AsyncBridge->processRequest($SwooleAsyncRequest, $SwooleAsyncResponse);
141+
$response->end();
142142

143-
gc_collect_cycles(); // Collect garbage to free memory (optional)
143+
gc_collect_cycles(); // Collect garbage to free memory (optional)
144144
});
145145
}
146146

147147
protected function bindWorkerEvents() {
148-
// You can use this to set custom events for the workers, such as creating connection pools.
149-
$createPools = function() {
150-
// Create connection pools for each worker
151-
// This is useful for managing database connections or other resources that need to be shared across requests.
152-
};
153-
$closePools = function() {
154-
// Close connection pools for each worker
155-
// This is useful for cleaning up resources when the worker stops or encounters an error.
156-
};
148+
// You can use this to set custom events for the workers, such as creating connection pools.
149+
$createPools = function() {
150+
// Create connection pools for each worker
151+
// This is useful for managing database connections or other resources that need to be shared across requests.
152+
};
153+
$closePools = function() {
154+
// Close connection pools for each worker
155+
// This is useful for cleaning up resources when the worker stops or encounters an error.
156+
};
157157
$this->Swoole->on('WorkerStart', $createPools);
158158
$this->Swoole->on('WorkerStop', $closePools);
159159
$this->Swoole->on('WorkerError', $closePools);

src/AsyncBridge.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ protected function buildFlightRequestFromAsyncRequest(AsyncRequestInterface $Asy
6565
//$Request = $this->copyHeaders($AsyncRequest, $Request);
6666

6767
$this->app->unregister('request');
68-
$this->app->register('request', Request::class, [], function () use (&$Request, $AsyncRequest) { // @phpstan-ignore-line
68+
$this->app->register('request', Request::class, [], function () use (&$Request, $AsyncRequest) {
69+
// @phpstan-ignore-line
6970
if ($this->isMultiPartFormData($Request) || $this->isXWwwFormUrlEncoded($Request)) {
7071
$Request = $this->handlePostData($AsyncRequest, $Request);
7172
}

src/adapter/SwooleAsyncRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class SwooleAsyncRequest implements AsyncRequestInterface
1111
{
1212
/**
13-
* swoole request
13+
* Swoole request
1414
*
1515
* @var Swoole_Request
1616
*/

src/commands/AbstractBaseCommand.php

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 28 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use flight\adapter\SwooleAsyncRequest;
46
use flight\adapter\SwooleAsyncResponse;
57
use flight\AsyncBridge;
68
use flight\Engine;
7-
use Smf\ConnectionPool\ConnectionPool;
8-
use Smf\ConnectionPool\ConnectionPoolTrait;
9-
use Smf\ConnectionPool\Connectors\CoroutineMySQLConnector;
109
use Swoole\HTTP\Server as SwooleServer;
1110
use Swoole\HTTP\Request as SwooleRequest;
1211
use Swoole\HTTP\Response as SwooleResponse;
1312
use Swoole\Coroutine\MySQL;
1413

15-
class SwooleServerDriver {
16-
17-
//use ConnectionPoolTrait;
14+
class SwooleServerDriver
15+
{
16+
//use ConnectionPoolTrait;
1817

19-
/** @var SwooleServer */
18+
/** @var SwooleServer */
2019
protected $Swoole;
2120

22-
/** @var Engine */
23-
protected $app;
21+
/** @var Engine */
22+
protected $app;
2423

25-
/** @var Async_Bridge */
26-
protected $Async_Bridge;
27-
28-
public function __construct(string $host, int $port, Engine $app) {
24+
public function __construct(string $host, int $port, Engine $app)
25+
{
2926
$this->Swoole = new SwooleServer($host, $port);
30-
$this->app = $app;
27+
$this->app = $app;
3128

3229
$this->setDefault();
3330
$this->bindWorkerEvents();
3431
$this->bindHttpEvent();
3532
}
3633

37-
protected function setDefault() {
34+
protected function setDefault()
35+
{
3836
$this->Swoole->set([
3937
'daemonize' => false,
4038
'dispatch_mode' => 1,
@@ -51,81 +49,29 @@ protected function setDefault() {
5149
]);
5250
}
5351

54-
protected function bindHttpEvent() {
55-
$app = $this->app;
56-
$AsyncBridge = new AsyncBridge($app);
57-
$this->Swoole->on("Start", function(SwooleServer $server) {
58-
echo "Swoole http server is started at http://127.0.0.1:9501\n";
59-
});
52+
protected function bindHttpEvent()
53+
{
54+
$app = $this->app;
55+
$AsyncBridge = new AsyncBridge($app);
56+
$this->Swoole->on("Start", function (SwooleServer $server) {
57+
echo "Swoole http server is started at http://127.0.0.1:9501\n";
58+
});
6059

6160
$this->Swoole->on('Request', function (SwooleRequest $request, SwooleResponse $response) use ($AsyncBridge) {
62-
echo memory_get_peak_usage() . PHP_EOL;
63-
// $pool1 = $this->getConnectionPool('mysql');
64-
// // /**@var MySQL $mysql */
65-
// $pdo = $pool1->borrow();
66-
67-
// $server_vars = $request->server;
68-
// $server_vars = array_change_key_case($server_vars, CASE_UPPER);
69-
// $_SERVER = $server_vars;
70-
// $_GET = $request->get ?? [];
71-
// $_POST = $request->post ?? [];
72-
// $_COOKIE = $request->cookie ?? [];
73-
// $_FILES = $request->files ?? [];
74-
// $_REQUEST = array_merge($_GET, $_POST, $_COOKIE);
61+
$SwooleAsyncRequest = new SwooleAsyncRequest($request);
62+
$SwooleAsyncResponse = new SwooleAsyncResponse($response);
63+
$AsyncBridge->processRequest($SwooleAsyncRequest, $SwooleAsyncResponse);
64+
$response->end();
7565

76-
// $Engine->start();
77-
78-
// foreach($Engine->response()->headers() as $header => $value) {
79-
// $response->header($header, $value);
80-
// }
81-
82-
// $response->status($Engine->response()->status());
83-
// if($Engine->response()->getBody()) {
84-
// $response->write($Engine->response()->getBody());
85-
// }
86-
87-
// $Engine->unregister('response');
88-
// $Engine->unregister('request');
89-
// $Engine->register('response', 'flight\net\Response');
90-
// $Engine->register('request', 'flight\net\Request');
91-
92-
$SwooleAsyncRequest = new SwooleAsyncRequest($request);
93-
$SwooleAsyncResponse = new SwooleAsyncResponse($response);
94-
$AsyncBridge->processRequest($SwooleAsyncRequest, $SwooleAsyncResponse);
95-
// $pool1->return($pdo);
96-
$response->end();
97-
98-
gc_collect_cycles(); // Collect garbage to free memory
66+
gc_collect_cycles(); // Collect garbage to free memory
9967
});
10068
}
10169

102-
protected function bindWorkerEvents() {
70+
protected function bindWorkerEvents()
71+
{
10372
$createPools = function () {
104-
105-
// $database_config = $this->App->getServicesContainer()->Config->database;
106-
// // All MySQL connections: [4 workers * 2 = 8, 4 workers * 10 = 40]
107-
// $pool1 = new ConnectionPool(
108-
// [
109-
// 'minActive' => 20,
110-
// 'maxActive' => 100,
111-
// ],
112-
// new CoroutineMySQLConnector,
113-
// [
114-
// 'host' => $database_config['host'],
115-
// 'port' => $database_config['port'],
116-
// 'user' => $database_config['username'],
117-
// 'password' => $database_config['password'],
118-
// 'database' => $database_config['dbname'],
119-
// 'timeout' => 10,
120-
// 'charset' => 'utf8mb4',
121-
// 'strict_type' => true,
122-
// 'fetch_mode' => true,
123-
// ]);
124-
// $pool1->init();
125-
// $this->addConnectionPool('mysql', $pool1);
12673
};
12774
$closePools = function () {
128-
// $this->closeConnectionPools();
12975
};
13076
$this->Swoole->on('WorkerStart', $createPools);
13177
$this->Swoole->on('WorkerStop', $closePools);
@@ -136,4 +82,4 @@ public function start()
13682
{
13783
$this->Swoole->start();
13884
}
139-
}
85+
}

tests/swoole/index.php

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

3+
declare(strict_types=1);
4+
35
define('NOT_SWOOLE', true);
46

5-
include 'swoole_server.php';
7+
include 'swoole_server.php';

0 commit comments

Comments
 (0)