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