1 | <?php |
||
12 | trait CallableTrait |
||
13 | { |
||
14 | use ArgumentsTrait; |
||
15 | |||
16 | /** |
||
17 | * Execute the callable. |
||
18 | * |
||
19 | * @param mixed $target |
||
20 | * @param RequestInterface $request |
||
21 | * @param ResponseInterface $response |
||
22 | * |
||
23 | * @return ResponseInterface |
||
24 | */ |
||
25 | private function executeCallable($target, RequestInterface $request, ResponseInterface $response) |
||
26 | { |
||
27 | try { |
||
28 | ob_start(); |
||
29 | |||
30 | $arguments = array_merge([$request, $response], $this->arguments); |
||
31 | $target = self::getCallable($target, $arguments); |
||
32 | $return = call_user_func_array($target, $arguments); |
||
33 | |||
34 | if ($return instanceof ResponseInterface) { |
||
35 | $response = $return; |
||
36 | $return = ''; |
||
37 | } |
||
38 | |||
39 | $return = ob_get_contents().$return; |
||
40 | $body = $response->getBody(); |
||
41 | |||
42 | if ($return !== '' && $body->isWritable()) { |
||
43 | $body->write($return); |
||
44 | } |
||
45 | |||
46 | return $response; |
||
47 | } catch (\Exception $exception) { |
||
48 | throw $exception; |
||
49 | } finally { |
||
50 | if (ob_get_level() > 0) { |
||
51 | ob_end_clean(); |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Resolves the target of the route and returns a callable. |
||
58 | * |
||
59 | * @param mixed $target |
||
60 | * @param array $construct_args |
||
61 | * |
||
62 | * @throws RuntimeException If the target is not callable |
||
63 | * |
||
64 | * @return callable |
||
65 | */ |
||
66 | private static function getCallable($target, array $construct_args) |
||
97 | } |
||
98 |