/** * Run the given query * * Strings preceded with ":" are replaced with corresponging sanitized values given in the $parameters argument * * e.g. * query("INSERT INTO table VALUES (:firstName, :lastName)", array( * "firstName" => "D'angelo", * "lastName" => "Piot'r" * )) * * will generate the query "INSERT INTO table VALUES ('D\'angelo', 'Piot\'r')"; * */ public function query($query, $parameters = null) { if (is_array($parameters)) { $query = $this->bindParameters($query, $parameters); } $queryLength = strlen($query); Debug::startBlock($queryLength > 10000 ? "[Query too long to debug ({$queryLength} characters)]" : $query, 'SQL'); $result = $this->mysqli->query($query); Debug::endBlock(); if ($result === false) { throw self::obtainException($this->mysqli->errno, $this->mysqli->error, $query); } return $result; }
private function renderOutput($allowCustomCallbacks = true) { // If the response already has data, ignore if (!!$this->response->getBody()) { return; } Debugger::startBlock("rendering response data"); $acceptedMediaTypes = $this->getAcceptedMediaTypes($this->request); $acceptedMediaTypes[] = "application/json"; $interpreter = null; $mediaType = null; if ($allowCustomCallbacks) { foreach ($this->queue as $action) { foreach ($acceptedMediaTypes as $mediaType) { $interpreter = $action->getInterpreter($mediaType); if ($interpreter) { break 2; } } } } $body = new Http\Stream("php://temp", "w"); $this->response->body($body); // interpreter can be: // A PHP file, which gets included if (is_string($interpreter) && file_exists($interpreter)) { $this->response->header("Content-Type", "{$mediaType}; charset=utf-8"); $body->write($this->renderFile($interpreter)); // A valid callback } elseif ($interpreter) { $callback = Callback::factory($interpreter); $string = $callback->run($this->getCallbackArguments($action)); $this->response->header("Content-Type", "{$mediaType}; charset=utf-8"); $body->write($string); // No interpreter: write output as JSON (if any) } elseif ($this->output !== null) { $this->response->header("Content-Type", "application/json; charset=utf-8"); $body->write(safe_json_encode($this->output)); } Debugger::endBlock(); }
/** * Execute a server request. * * @param ServerRequest * @return Response */ public static function execute(ServerRequest $request) { self::initialize(); $method = $request->getMethod(); $path = $request->getUri()->getPath(); Debug::startBlock("{$method} {$path}", "resource"); try { $resource = self::getResource($path); $dispatcher = $resource->getDispatcher($method); $response = $dispatcher->dispatch($request); } catch (Server\Exception\ResourceNotFound $e) { $response = new Response(404); } catch (Server\Exception\MethodNotImplemented $e) { if ($method == "options") { $response = new Response(200); $accessControl = $resource->getAccessControl()->allowMethods($e->getImplementedMethods()); $response = $accessControl->filter($response, $request); } else { $response = (new Response(405))->withHeader("Allowed", $e->getImplementedMethods()); } } Debug::endBlock(); return $response; }