Skip to content

Commit bdf5d5a

Browse files
Merge pull request #144 from pinguo-zhanglu/pb
通过配置自定义Input类和Output类
2 parents 105f11f + d3817f7 commit bdf5d5a

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/Helpers/Context.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace PG\MSF\Helpers;
1010

1111
use PG\Context\AbstractContext;
12-
use PG\Log\PGLog;
1312
use PG\MSF\Base\Input;
1413
use PG\MSF\Base\Output;
1514
use PG\MSF\Base\Pool;

src/HttpServer.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ abstract class HttpServer extends Server
4747
* @var array
4848
*/
4949
public $viewResolvePaths;
50+
51+
/**
52+
* Input类
53+
* 通过继承 PG\MSF\Base\Input 来自定义,默认是 PG\MSF\Base\Input
54+
* 可以在配置文件中修改,例如:
55+
*
56+
* ```php
57+
* 'http' => [
58+
* 'input' => PG\MSF\Base\Input::class,
59+
* ]
60+
* ```
61+
* @var Input
62+
*/
63+
public $input;
64+
65+
/**
66+
* Output类
67+
* 通过继承 PG\MSF\Base\Output 来自定义,默认是 PG\MSF\Base\Output
68+
* 可以在配置文件中修改,例如:
69+
*
70+
* ```php
71+
* 'http' => [
72+
* 'output' => PG\MSF\Base\Output::class,
73+
* ]
74+
* ```
75+
* @var Output
76+
*/
77+
public $output;
5078

5179
/**
5280
* HttpServer constructor.
@@ -82,6 +110,14 @@ public function setConfig()
82110
$this->httpEnable = $this->config->get('http_server.enable', true);
83111
$this->httpSocketName = $this->config['http_server']['socket'];
84112
$this->httpPort = $this->config['http_server']['port'];
113+
$this->input = $this->config->get('http.input', Input::class);
114+
if (is_array($this->input)) {
115+
$this->input = $this->input['class'] ?? Input::class;
116+
}
117+
$this->output = $this->config->get('http.output', Output::class);
118+
if (is_array($this->output)) {
119+
$this->output = $this->output['class'] ?? Output::class;
120+
}
85121
return $this;
86122
}
87123

@@ -231,12 +267,12 @@ public function onRequest($request, $response)
231267
/**
232268
* @var $input Input
233269
*/
234-
$input = $instance->context->getObjectPool()->get(Input::class);
270+
$input = $instance->context->getObjectPool()->get($this->input);
235271
$input->set($request);
236272
/**
237273
* @var $output Output
238274
*/
239-
$output = $instance->context->getObjectPool()->get(Output::class, [$instance]);
275+
$output = $instance->context->getObjectPool()->get($this->output, [$instance]);
240276
$output->set($request, $response);
241277

242278
$instance->context->setInput($input);

src/Rest/Controller.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
namespace PG\MSF\Rest;
1010

11-
use PG\MSF\Base\Output;
12-
1311
/**
1412
* Class Controller
1513
* @package PG\MSF\Rest
@@ -137,7 +135,7 @@ public function outputJson($data = null, $message = '', $status = 200)
137135
*/
138136
public function outputOptions(array $options)
139137
{
140-
/* @var $output Output */
138+
/* @var $output \PG\MSF\Base\Output */
141139
$output = $this->getContext()->getOutput();
142140
$status = 200;
143141
if ($this->verb !== 'OPTIONS') {

0 commit comments

Comments
 (0)