1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jellyfish\Http; |
4
|
|
|
|
5
|
|
|
use Http\Factory\Diactoros\ResponseFactory; |
6
|
|
|
use League\Route\Router; |
7
|
|
|
use League\Route\Strategy\JsonStrategy; |
8
|
|
|
use League\Route\Strategy\StrategyInterface; |
9
|
|
|
use Pimple\Container; |
10
|
|
|
use Pimple\ServiceProviderInterface; |
11
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
13
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
14
|
|
|
use Zend\HttpHandlerRunner\Emitter\EmitterInterface; |
15
|
|
|
use Zend\HttpHandlerRunner\Emitter\SapiStreamEmitter; |
16
|
|
|
|
17
|
|
|
class HttpServiceProvider implements ServiceProviderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param \Pimple\Container $pimple |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function register(Container $pimple): void |
25
|
|
|
{ |
26
|
|
|
$this->registerRequest($pimple) |
27
|
|
|
->registerRouter($pimple) |
28
|
|
|
->registerEmitter($pimple); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
33
|
|
|
* |
34
|
|
|
* @param \Pimple\Container $container |
35
|
|
|
* |
36
|
|
|
* @return \Jellyfish\Http\HttpServiceProvider |
37
|
|
|
*/ |
38
|
|
|
protected function registerRequest(Container $container): HttpServiceProvider |
39
|
|
|
{ |
40
|
|
|
$container->offsetSet('request', function () { |
41
|
|
|
return ServerRequestFactory::fromGlobals(); |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param \Pimple\Container $container |
49
|
|
|
* |
50
|
|
|
* @return \Jellyfish\Http\HttpServiceProvider |
51
|
|
|
*/ |
52
|
|
|
protected function registerRouter(Container $container): HttpServiceProvider |
53
|
|
|
{ |
54
|
|
|
$self = $this; |
55
|
|
|
|
56
|
|
|
$container->offsetSet('router', function () use ($self) { |
57
|
|
|
$router = new Router(); |
58
|
|
|
|
59
|
|
|
$router->setStrategy($self->createStrategy()); |
60
|
|
|
|
61
|
|
|
return $router; |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param \Pimple\Container $container |
69
|
|
|
* |
70
|
|
|
* @return \Jellyfish\Http\HttpServiceProvider |
71
|
|
|
*/ |
72
|
|
|
protected function registerEmitter(Container $container): HttpServiceProvider |
73
|
|
|
{ |
74
|
|
|
$container->offsetSet('emitter', function () { |
75
|
|
|
return new SapiStreamEmitter(); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \League\Route\Strategy\StrategyInterface |
83
|
|
|
*/ |
84
|
|
|
protected function createStrategy(): StrategyInterface |
85
|
|
|
{ |
86
|
|
|
return new JsonStrategy($this->createResponseFactory()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return \Psr\Http\Message\ResponseFactoryInterface |
91
|
|
|
*/ |
92
|
|
|
protected function createResponseFactory(): ResponseFactoryInterface |
93
|
|
|
{ |
94
|
|
|
return new ResponseFactory(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|