Skip to content

Commit c2a7659

Browse files
authored
Merge pull request #6 from inhere/master
router update, some modify for http server
2 parents 81ab4ce + cedde46 commit c2a7659

File tree

15 files changed

+719
-387
lines changed

15 files changed

+719
-387
lines changed

master

Whitespace-only changes.

src/Bean/Annotation/RequestMapping.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class RequestMapping
2929
*/
3030
private $method = [RequestMethod::GET, RequestMethod::POST];
3131

32-
3332
/**
3433
* {"id"="\d+"}
3534
* @var array
@@ -76,7 +75,7 @@ public function __construct(array $values)
7675
*
7776
* @return string
7877
*/
79-
public function getRoute()
78+
public function getRoute(): string
8079
{
8180
return $this->route;
8281
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018/3/19
6+
* Time: 上午11:32
7+
*/
8+
9+
namespace Swoft\Http\Server\Bootstrap\Listener;
10+
11+
use Swoft\App;
12+
use Swoft\Bean\Annotation\ServerListener;
13+
use Swoft\Bootstrap\Listeners\Interfaces\StartInterface;
14+
use Swoft\Bootstrap\SwooleEvent;
15+
use Swoole\Server;
16+
17+
/**
18+
* Class MasterStartListener
19+
* @package Swoft\Http\Server\Bootstrap\Listener
20+
* @ServerListener(event=SwooleEvent::ON_START)
21+
*/
22+
class MasterStartListener implements StartInterface
23+
{
24+
public function onStart(Server $server)
25+
{
26+
\output()->writeln(
27+
'Server has been started. ' .
28+
"(master PID: <cyan>{$server->master_pid}</cyan>, manager PID: <cyan>{$server->manager_pid}</cyan>)"
29+
);
30+
31+
// output a message before start
32+
if (!App::$server->isDaemonize()) {
33+
\output()->writeln('You can use <info>CTRL + C</info> to stop run.');
34+
}
35+
}
36+
}

src/Command/ServerCommand.php

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ class ServerCommand
1616
/**
1717
* Start http server
1818
*
19-
* @Usage server:{command} [arguments] [options]
19+
* @Usage {fullCommand} [-d|--daemon]
2020
* @Options
21-
* -d,--d start by daemonized process
22-
* @Example php swoft.php server:start -d -r
21+
* -d, --daemon Run server on the background
22+
* @Example
23+
* {fullCommand}
24+
* {fullCommand} -d
2325
* @throws \Swoft\Exception\RuntimeException
2426
* @throws \RuntimeException
2527
*/
@@ -32,7 +34,7 @@ public function start()
3234

3335
// 是否正在运行
3436
if ($httpServer->isRunning()) {
35-
output()->writeln("<error>The server have been running!(PID: {$serverStatus['masterPid']})</error>", true, true);
37+
\output()->writeln("<error>The server have been running!(PID: {$serverStatus['masterPid']})</error>", true, true);
3638
}
3739

3840
// 启动参数
@@ -54,29 +56,30 @@ public function start()
5456
$tcpHost = $tcpStatus['host'];
5557
$tcpPort = $tcpStatus['port'];
5658
$tcpType = $tcpStatus['type'];
57-
$tcpEnable = $tcpEnable ? 1 : 0;
59+
$tcpEnable = $tcpEnable ? '<note>Enabled</note>' : '<warning>Disabled</warning>';
5860

5961
// 信息面板
6062
$lines = [
61-
' Information Panel ',
62-
'******************************************************************',
63-
"* http | Host: <note>$httpHost</note>, port: <note>$httpPort</note>, Model: <note>$httpMode</note>, type: <note>$httpType</note>, Worker: <note>$workerNum</note>",
64-
"* tcp | Enable: <note>$tcpEnable</note>, host: <note>$tcpHost</note>, port: <note>$tcpPort</note>, type: <note>$tcpType</note>, Worker: <note>$workerNum</note>",
65-
'******************************************************************',
63+
' Server Information ',
64+
'********************************************************************',
65+
"* HTTP | host: <note>$httpHost</note>, port: <note>$httpPort</note>, type: <note>$httpType</note>, worker: <note>$workerNum</note>, mode: <note>$httpMode</note>",
66+
"* TCP | host: <note>$tcpHost</note>, port: <note>$tcpPort</note>, type: <note>$tcpType</note>, worker: <note>$workerNum</note> ($tcpEnable)",
67+
'********************************************************************',
6668
];
6769

6870
// 启动服务器
69-
output()->writeln(implode("\n", $lines));
71+
\output()->writeln(implode("\n", $lines));
7072
$httpServer->start();
7173
}
7274

7375
/**
74-
* Reload worker process
76+
* Reload worker processes
7577
*
76-
* @Usage server:{command} [arguments] [options]
78+
* @Usage {fullCommand} [-t]
7779
* @Options
78-
* -t only to reload task processes, default to reload worker and task
79-
* @Example php swoft.php server:reload
80+
* -t Only to reload task processes, default to reload worker and task
81+
* @Example {fullCommand}
82+
* @throws \InvalidArgumentException
8083
* @throws \RuntimeException
8184
*/
8285
public function reload()
@@ -97,10 +100,10 @@ public function reload()
97100
}
98101

99102
/**
100-
* Stop http server
103+
* Stop the http server
101104
*
102-
* @Usage server:{command} [arguments] [options]
103-
* @Example php swoft.php server:stop
105+
* @Usage {fullCommand}
106+
* @Example {fullCommand}
104107
* @throws \RuntimeException
105108
*/
106109
public function stop()
@@ -109,31 +112,36 @@ public function stop()
109112

110113
// 是否已启动
111114
if (!$httpServer->isRunning()) {
112-
output()->writeln('<error>The server is not running! cannot stop</error>', true, true);
115+
\output()->writeln('<error>The server is not running! cannot stop</error>', true, true);
113116
}
114117

115118
// pid文件
116119
$serverStatus = $httpServer->getServerSetting();
117120
$pidFile = $serverStatus['pfile'];
118121

119122
@unlink($pidFile);
120-
output()->writeln(sprintf('<info>Swoft %s is stopping ...</info>', input()->getScript()));
123+
\output()->writeln(sprintf('<info>Swoft %s is stopping ...</info>', input()->getScript()));
121124

122125
$result = $httpServer->stop();
123126

124127
// 停止失败
125128
if (!$result) {
126-
output()->writeln(sprintf('<error>Swoft %s stop fail</error>', input()->getScript()), true, true);
129+
\output()->writeln(sprintf('<error>Swoft %s stop fail</error>', input()->getScript()), true, true);
127130
}
128131

129132
output()->writeln(sprintf('<success>Swoft %s stop success!</success>', input()->getScript()));
130133
}
131134

132135
/**
133-
* Restart http server
136+
* Restart the http server
134137
*
135-
* @Usage server:{command} [arguments] [options]
136-
* @Example php swoft.php server:restart
138+
* @Usage {fullCommand} [-d|--daemon]
139+
* @Options
140+
* -d, --daemon Run server on the background
141+
* @Example
142+
* {fullCommand}
143+
* {fullCommand} -d
144+
* @throws \InvalidArgumentException
137145
* @throws \RuntimeException
138146
*/
139147
public function restart()
@@ -152,6 +160,7 @@ public function restart()
152160

153161
/**
154162
* @return HttpServer
163+
* @throws \InvalidArgumentException
155164
* @throws \RuntimeException
156165
*/
157166
private function getHttpServer(): HttpServer
@@ -175,7 +184,7 @@ private function getHttpServer(): HttpServer
175184
*/
176185
private function setStartArgs(HttpServer $httpServer)
177186
{
178-
$daemonize = input()->hasOpt('d');
187+
$daemonize = \input()->getSameOpt(['d', 'daemon'], false);
179188

180189
if ($daemonize) {
181190
$httpServer->setDaemonize();

src/Event/HttpServerEvent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ class HttpServerEvent
1616
/**
1717
* before request
1818
*/
19-
const BEFORE_REQUEST = "beforeRequest";
19+
const BEFORE_REQUEST = 'beforeRequest';
2020

2121
/**
2222
* after request
2323
*/
24-
const AFTER_REQUEST = "afterRequest";
25-
}
24+
const AFTER_REQUEST = 'afterRequest';
25+
}

src/Event/Listeners/ApplicationLoaderListener.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Swoft\Http\Server\Bean\Collector\ControllerCollector;
1010

1111
/**
12-
* the listener of applicatioin loader
12+
* the listener of application loader
1313
*
1414
* @Listener(AppEvent::APPLICATION_LOADER)
1515
* @uses ApplicationLoaderListener
@@ -22,6 +22,8 @@ class ApplicationLoaderListener implements EventHandlerInterface
2222
{
2323
/**
2424
* @param \Swoft\Event\EventInterface $event
25+
* @throws \LogicException
26+
* @throws \InvalidArgumentException
2527
*/
2628
public function handle(EventInterface $event)
2729
{
@@ -31,5 +33,4 @@ public function handle(EventInterface $event)
3133
$requestMapping = ControllerCollector::getCollector();
3234
$httpRouter->registerRoutes($requestMapping);
3335
}
34-
35-
}
36+
}

src/Http/HttpServer.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ public function start()
5656
*
5757
* @throws \Swoft\Exception\RuntimeException
5858
*/
59-
private function registerRpcEvent()
59+
protected function registerRpcEvent()
6060
{
6161
$swooleListeners = SwooleListenerCollector::getCollector();
62-
if (! isset($swooleListeners[SwooleEvent::TYPE_PORT][0]) || empty($swooleListeners[SwooleEvent::TYPE_PORT][0])) {
62+
63+
if (!isset($swooleListeners[SwooleEvent::TYPE_PORT][0]) || empty($swooleListeners[SwooleEvent::TYPE_PORT][0])) {
6364
throw new RuntimeException("Please use swoft/rpc-server, run 'composer require swoft/rpc-server'");
6465
}
6566

@@ -81,11 +82,11 @@ private function registerRpcEvent()
8182
public function onRequest(Request $request, Response $response)
8283
{
8384
// Initialize Request and Response and set to RequestContent
84-
$request = \Swoft\Http\Message\Server\Request::loadFromSwooleRequest($request);
85-
$response = new \Swoft\Http\Message\Server\Response($response);
85+
$psr7Request = \Swoft\Http\Message\Server\Request::loadFromSwooleRequest($request);
86+
$psr7Response = new \Swoft\Http\Message\Server\Response($response);
8687

8788
/** @var \Swoft\Http\Server\ServerDispatcher $dispatcher */
8889
$dispatcher = App::getBean('serverDispatcher');
89-
$dispatcher->dispatch($request, $response);
90+
$dispatcher->dispatch($psr7Request, $psr7Response);
9091
}
9192
}

0 commit comments

Comments
 (0)