|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Mouf\Annotations\URLAnnotation; |
|
6
|
|
|
use Mouf\Mvc\Splash\Utils\SplashException; |
|
7
|
|
|
use Mouf\Reflection\MoufReflectionMethod; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Mouf\Annotations\paramAnnotation; |
|
10
|
|
|
use Mouf\Reflection\MoufReflectionParameter; |
|
11
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
12
|
|
|
|
|
13
|
|
|
class SplashUtils |
|
14
|
|
|
{ |
|
15
|
|
|
const MODE_WEAK = 'weak'; |
|
16
|
|
|
const MODE_STRICT = 'strict'; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
public static function buildControllerResponse($callback, $mode = self::MODE_STRICT, $debug = false) |
|
21
|
|
|
{ |
|
22
|
|
|
ob_start(); |
|
23
|
|
|
try { |
|
24
|
|
|
$result = $callback(); |
|
25
|
|
|
} catch (\Exception $e) { |
|
26
|
|
|
ob_end_clean(); |
|
27
|
|
|
// Rethrow and keep stack trace. |
|
28
|
|
|
throw $e; |
|
29
|
|
|
} |
|
30
|
|
|
$html = ob_get_clean(); |
|
31
|
|
|
|
|
32
|
|
|
if (!empty($html) || $mode === self::MODE_WEAK) { |
|
33
|
|
|
if ($mode === self::MODE_WEAK) { |
|
34
|
|
|
$code = http_response_code(); |
|
35
|
|
|
$headers = self::getResponseHeaders(); |
|
36
|
|
|
|
|
37
|
|
|
// Suppress actual headers (re-add by PSR-7 Response) |
|
38
|
|
|
// If you don't remove old headers, it's duplicated in HTTP Headers |
|
39
|
|
|
foreach ($headers as $key => $head) { |
|
40
|
|
|
header_remove($key); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ($result !== null) { |
|
44
|
|
|
// We might be in weak mode, it is not normal to have both an output and a response! |
|
45
|
|
|
$html = '<h1>Output started in controller. It is not normal to have an output in the controller, and a response returned by the controller. Output detected:</h1>'.$html; |
|
46
|
|
|
$code = 500; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return new HtmlResponse($html, $code, $headers); |
|
50
|
|
|
} else { |
|
51
|
|
|
if ($debug) { |
|
52
|
|
|
$html = '<h1>Output started in controller. A controller should return an object implementing the ResponseInterface rather than outputting directly content. Output detected:</h1>'.$html; |
|
53
|
|
|
return new HtmlResponse($html, 500); |
|
54
|
|
|
} else { |
|
55
|
|
|
throw new SplashException('Output started in Controller : '.$html); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (!$result instanceof ResponseInterface) { |
|
61
|
|
|
if ($result === null) { |
|
62
|
|
|
throw new SplashException('Your controller should return an instance of Psr\\Http\\Message\\ResponseInterface. Your controller did not return any value.'); |
|
63
|
|
|
} else { |
|
64
|
|
|
$class = (gettype($result) == 'object') ? get_class($result) : gettype($result); |
|
65
|
|
|
throw new SplashException('Your controller should return an instance of Psr\\Http\\Message\\ResponseInterface. Type of value returned: '.$class); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $result; |
|
70
|
|
|
|
|
71
|
|
|
// TODO: If Symfony Response convert to psr-7 |
|
72
|
|
|
// if ($result instanceof Response) { |
|
73
|
|
|
// if ($html !== "") { |
|
74
|
|
|
// throw new SplashException("You cannot output text AND return Response object in the same action. Output already started :'$html"); |
|
75
|
|
|
// } |
|
76
|
|
|
// |
|
77
|
|
|
// if (headers_sent()) { |
|
78
|
|
|
// $headers = headers_list(); |
|
79
|
|
|
// throw new SplashException("Headers already sent. Detected headers are : ".var_export($headers, true)); |
|
80
|
|
|
// } |
|
81
|
|
|
// |
|
82
|
|
|
// return $result; |
|
83
|
|
|
// } |
|
84
|
|
|
// |
|
85
|
|
|
// $code = http_response_code(); |
|
86
|
|
|
// $headers = SplashUtils::greatResponseHeaders(); |
|
87
|
|
|
// |
|
88
|
|
|
// // Suppress actual headers (re-add by Symfony Response) |
|
89
|
|
|
// // If you don't remove old headers, it's duplicated in HTTP Headers |
|
90
|
|
|
// foreach ($headers as $key => $head) { |
|
91
|
|
|
// header_remove($key); |
|
92
|
|
|
// } |
|
93
|
|
|
// |
|
94
|
|
|
// return new Response($html, $code, $headers); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Same as apache_response_headers (for any server) |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
private static function getResponseHeaders() { |
|
102
|
|
|
$arh = array(); |
|
103
|
|
|
|
|
104
|
|
|
// headers_list don't return associative array |
|
105
|
|
|
$headers = headers_list(); |
|
106
|
|
|
foreach ($headers as $header) { |
|
107
|
|
|
$header = explode(":", $header); |
|
108
|
|
|
$arh[array_shift($header)] = trim(implode(":", $header)); |
|
109
|
|
|
} |
|
110
|
|
|
return $arh; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|