function handleRequest()
 {
     $request = $this->request;
     // Merge route parameters.
     $rp = Http::getRouteParameters($request);
     if ($rp) {
         $o = [];
         setAt($o, $this->mainSubModelPath, $rp);
         $this->merge($o);
     }
     switch ($request->getMethod()) {
         case 'GET':
             $old = $this->session->getOldInput();
             if ($old) {
                 $this->merge($this->parseFormData($old));
             }
             break;
         case 'POST':
             $data = $request->getParsedBody();
             if (isset($data)) {
                 $contentType = $request->getHeaderLine('Content-Type');
                 if ($contentType == 'application/x-www-form-urlencoded' || str_beginsWith($contentType, 'multipart/form-data')) {
                     $data = $this->parseFormData($data);
                 }
                 unset($data[PlatformModule::ACTION_FIELD]);
                 $this->merge($data);
             }
     }
     $this->runExtensions();
 }
Beispiel #2
0
/**
 * 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);
        };
    });
}