1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WShafer\SwooleExpressive\Command; |
5
|
|
|
|
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Swoole\Http\Request; |
8
|
|
|
use Swoole\Http\Response; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use WShafer\SwooleExpressive\Bridge\MiddlewareSetupRunnerInterface; |
14
|
|
|
use WShafer\SwooleExpressive\Bridge\Psr7RequestBuilder; |
15
|
|
|
use WShafer\SwooleExpressive\Bridge\SwooleResponseEmitter; |
16
|
|
|
use WShafer\SwooleExpressive\Exception\MissingPipeLineException; |
17
|
|
|
use WShafer\SwooleExpressive\Exception\SwooleExpressiveException; |
18
|
|
|
use Zend\Expressive\Application; |
19
|
|
|
use Zend\Expressive\MiddlewareFactory; |
20
|
|
|
|
21
|
|
|
class SwooleRunnerCommand extends Command |
22
|
|
|
{ |
23
|
|
|
/** @var ContainerInterface */ |
24
|
|
|
protected $container; |
25
|
|
|
|
26
|
|
|
/** @var Application */ |
27
|
|
|
protected $app; |
28
|
|
|
|
29
|
|
|
/** @var MiddlewareSetupRunnerInterface */ |
30
|
|
|
protected $setupRunner; |
31
|
|
|
|
32
|
|
|
/** @var Psr7RequestBuilder */ |
33
|
|
|
protected $requestBuilder; |
34
|
|
|
|
35
|
|
|
/** @var SwooleResponseEmitter */ |
36
|
|
|
protected $responseEmitter; |
37
|
|
|
|
38
|
1 |
|
public function __construct( |
39
|
|
|
Application $application, |
40
|
|
|
MiddlewareSetupRunnerInterface $setupRunner, |
41
|
|
|
Psr7RequestBuilder $requestBuilder, |
42
|
|
|
SwooleResponseEmitter $responseEmitter, |
43
|
|
|
?string $name = null |
44
|
|
|
) { |
45
|
1 |
|
$this->app = $application; |
46
|
1 |
|
$this->setupRunner = $setupRunner; |
47
|
1 |
|
$this->requestBuilder = $requestBuilder; |
48
|
1 |
|
$this->responseEmitter = $responseEmitter; |
49
|
1 |
|
parent::__construct($name); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
1 |
|
protected function configure() |
53
|
|
|
{ |
54
|
1 |
|
$this->setName('swoole:expressive:runner') |
55
|
1 |
|
->setDescription('Run Expressive inside Swoole web server') |
56
|
1 |
|
->setHelp('This command will start a Swoole web server for Zend Expressive'); |
57
|
|
|
|
58
|
1 |
|
$this->addOption( |
59
|
1 |
|
'host', |
60
|
1 |
|
'i', |
61
|
1 |
|
InputOption::VALUE_OPTIONAL, |
62
|
1 |
|
'Swoole host to listen to.', |
63
|
1 |
|
'0.0.0.0' |
64
|
|
|
); |
65
|
|
|
|
66
|
1 |
|
$this->addOption( |
67
|
1 |
|
'port', |
68
|
1 |
|
'p', |
69
|
1 |
|
InputOption::VALUE_OPTIONAL, |
70
|
1 |
|
'Swoole port number to listen on.', |
71
|
1 |
|
8080 |
72
|
|
|
); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
$this->setupRunner->execute(); |
79
|
|
|
} catch (SwooleExpressiveException $e) { |
80
|
|
|
$output->writeln($e->getMessage()); |
81
|
|
|
throw $e; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$host = (string) $input->getOption('host'); |
85
|
|
|
$port = (int) $input->getOption('port'); |
86
|
|
|
|
87
|
|
|
$http = new \swoole_http_server($host, $port); |
88
|
|
|
$app = $this->app; |
89
|
|
|
$requestBuilder = $this->requestBuilder; |
90
|
|
|
$responseEmitter = $this->responseEmitter; |
91
|
|
|
|
92
|
|
|
$http->on( |
93
|
|
|
'request', |
94
|
|
|
function (Request $request, Response $response) use ($app, $requestBuilder, $responseEmitter) { |
95
|
|
|
$psrResponse = $app->handle($requestBuilder->build($request)); |
96
|
|
|
$responseEmitter->toSwoole($psrResponse, $response); |
97
|
|
|
} |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$http->start(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|