2
2
3
3
namespace PHPPM \Bridges ;
4
4
5
- use PHPPM \AppBootstrapInterface ;
6
5
use PHPPM \Bootstraps \BootstrapInterface ;
7
- use PHPPM \Bridges \ BridgeInterface ;
6
+ use PHPPM \Bootstraps \ SymfonyAppKernel ;
8
7
use React \Http \Request as ReactRequest ;
9
8
use React \Http \Response as ReactResponse ;
10
- use Stack \Builder ;
11
9
use Symfony \Component \HttpFoundation \Request as SymfonyRequest ;
12
10
use Symfony \Component \HttpFoundation \Response as SymfonyResponse ;
13
11
use Symfony \Component \HttpFoundation \StreamedResponse as SymfonyStreamedResponse ;
@@ -34,9 +32,10 @@ class HttpKernel implements BridgeInterface
34
32
*
35
33
* @param string $appBootstrap The name of the class used to bootstrap the application
36
34
* @param string|null $appenv The environment your application will use to bootstrap (if any)
35
+ * @param boolean $debug If debug is enabled
37
36
* @see http://stackphp.com
38
37
*/
39
- public function bootstrap ($ appBootstrap , $ appenv )
38
+ public function bootstrap ($ appBootstrap , $ appenv, $ debug )
40
39
{
41
40
// include applications autoload
42
41
$ autoloader = dirname (realpath ($ _SERVER ['SCRIPT_NAME ' ])) . '/vendor/autoload.php ' ;
@@ -46,19 +45,21 @@ public function bootstrap($appBootstrap, $appenv)
46
45
47
46
$ appBootstrap = $ this ->normalizeAppBootstrap ($ appBootstrap );
48
47
49
- $ bootstrap = new $ appBootstrap ($ appenv );
48
+ $ bootstrap = new $ appBootstrap ($ appenv, $ debug );
50
49
51
50
if ($ bootstrap instanceof BootstrapInterface) {
52
51
$ this ->application = $ bootstrap ->getApplication ();
53
-
54
- if ($ bootstrap instanceof StackableBootstrapInterface) {
55
- $ stack = new Builder ();
56
- $ stack = $ bootstrap ->getStack ($ stack );
57
- $ this ->application = $ stack ->resolve ($ this ->application );
58
- }
59
52
}
60
53
}
61
54
55
+ /**
56
+ * {@inheritdoc}
57
+ */
58
+ public function getStaticDirectory ()
59
+ {
60
+ return 'web/ ' ;
61
+ }
62
+
62
63
/**
63
64
* Handle a request using a HttpKernelInterface implementing application.
64
65
*
@@ -73,11 +74,10 @@ public function onRequest(ReactRequest $request, ReactResponse $response)
73
74
74
75
$ content = '' ;
75
76
$ headers = $ request ->getHeaders ();
76
- $ contentLength = isset ($ headers ['Content-Length ' ]) ? (int ) $ headers ['Content-Length ' ] : 0 ;
77
+ $ contentLength = isset ($ headers ['Content-Length ' ]) ? (int )$ headers ['Content-Length ' ] : 0 ;
77
78
78
- $ request ->on ('data ' , function ($ data )
79
- use ($ request , $ response , &$ content , $ contentLength )
80
- {
79
+ $ request ->on ('data ' , function ($ data )
80
+ use ($ request , $ response , &$ content , $ contentLength ) {
81
81
// read data (may be empty for GET request)
82
82
$ content .= $ data ;
83
83
@@ -86,18 +86,26 @@ public function onRequest(ReactRequest $request, ReactResponse $response)
86
86
$ syRequest = self ::mapRequest ($ request , $ content );
87
87
88
88
try {
89
+ if ($ this ->application instanceof SymfonyAppKernel) {
90
+ $ this ->application ->preHandle ();
91
+ }
92
+
89
93
$ syResponse = $ this ->application ->handle ($ syRequest );
90
94
} catch (\Exception $ exception ) {
91
95
$ response ->writeHead (500 ); // internal server error
92
96
$ response ->end ();
93
- return ;
97
+ throw $ exception ;
94
98
}
95
99
96
100
self ::mapResponse ($ response , $ syResponse );
97
101
98
102
if ($ this ->application instanceof TerminableInterface) {
99
103
$ this ->application ->terminate ($ syRequest , $ syResponse );
100
104
}
105
+
106
+ if ($ this ->application instanceof SymfonyAppKernel) {
107
+ $ this ->application ->postHandle ();
108
+ }
101
109
}
102
110
});
103
111
}
@@ -124,15 +132,15 @@ protected static function mapRequest(ReactRequest $reactRequest, $content)
124
132
125
133
$ cookies = array ();
126
134
if (isset ($ headers ['Cookie ' ])) {
127
- $ headersCookie = explode ('; ' , $ headers ['Cookie ' ]);
128
- foreach ($ headersCookie as $ cookie ) {
129
- list ($ name , $ value ) = explode ('= ' , trim ($ cookie ));
130
- $ cookies [$ name ] = $ value ;
131
- }
135
+ $ headersCookie = explode ('; ' , $ headers ['Cookie ' ]);
136
+ foreach ($ headersCookie as $ cookie ) {
137
+ list ($ name , $ value ) = explode ('= ' , trim ($ cookie ));
138
+ $ cookies [$ name ] = $ value ;
139
+ }
132
140
}
133
141
134
142
$ syRequest = new SymfonyRequest (
135
- // $query, $request, $attributes, $cookies, $files, $server, $content
143
+ // $query, $request, $attributes, $cookies, $files, $server, $content
136
144
$ query , $ post , array (), $ cookies , array (), array (), $ content
137
145
);
138
146
@@ -151,7 +159,7 @@ protected static function mapRequest(ReactRequest $reactRequest, $content)
151
159
* @param SymfonyResponse $syResponse
152
160
*/
153
161
protected static function mapResponse (ReactResponse $ reactResponse ,
154
- SymfonyResponse $ syResponse )
162
+ SymfonyResponse $ syResponse )
155
163
{
156
164
$ headers = $ syResponse ->headers ->all ();
157
165
$ reactResponse ->writeHead ($ syResponse ->getStatusCode (), $ headers );
@@ -162,8 +170,7 @@ protected static function mapResponse(ReactResponse $reactResponse,
162
170
$ syResponse ->sendContent ();
163
171
$ content = ob_get_contents ();
164
172
ob_end_clean ();
165
- }
166
- else {
173
+ } else {
167
174
$ content = $ syResponse ->getContent ();
168
175
}
169
176
0 commit comments