function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $path = either($request->getAttribute('virtualUri', '<i>not set</i>'), '<i>empty</i>'); $realPath = $request->getUri()->getPath(); return Http::response($response, "<br><br><table align=center cellspacing=20 style='text-align:left'>\n<tr><th>Virtual URL:<td><kbd>{$path}</kbd>\n<tr><th>URL path:<td><kbd>{$realPath}</kbd>\n</table>", 'text/html', 404); }
/** * Returns a route to a controller method or function. * * <p>The callable will receive as arguments the route parameters, followed by the request and the response objects. * <p>It can return: * - a response object * - a string (sent as text/html) * - `null` to send an empty response * - arrays, objects or scalars will be sent as JSON. * * @param string|array|callable $ref Either a Closure, a 'Class::method' string or a ['Class', 'method'] array or an * [$instance, 'method'] array. * @return FactoryRoutable * @throws Fault If an invalid data type is returned from the controller. */ function controller($ref) { return new FactoryRoutable(function (InjectorInterface $injector) use($ref) { $ctrl = $injector->buildExecutable($ref); return function (ServerRequestInterface $request, ResponseInterface $response) use($ctrl) { $args = array_merge(array_values(Http::getRouteParameters($request)), [$request, $response]); $result = $ctrl(...$args); switch (true) { case $result instanceof ResponseInterface: return $result; case is_string($result): break; case is_null($result): $result = ''; break; case is_array($result): case is_object($result): case is_scalar($result): return Http::jsonResponse($response, $result); default: throw new Fault(Faults::INVALID_RESPONSE_TYPE); } return Http::response($response, $result); }; }); }