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(); }
/** * 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; }
<?php use Phidias\Utilities\Debugger; use Phidias\Api\Environment; $request = Environment::getServerRequest(); if (!$request) { return; } $debugId = $request->getHeaderLine("X-Phidias-Debug"); if ($debugId) { Debugger::enable(); register_shutdown_function(function () use($debugId) { $tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . md5($debugId) . '.debug.json'; file_put_contents($tmpFile, Debugger::toJson()); }); }
function printMessage($message, $depth = 0, $parent = null) { $message->timer = $message->timestamp - Debugger::getInitialTimestamp(); $messageId = "debug_message_" . $depth . preg_replace('/[^0-9]/', '', $message->timer) . rand(1, 999999); $parentClass = $parent !== null ? "son_of_" . $parent : ''; ?> <tr id="<?php echo $messageId; ?> " class="debugger_record debugger_type_<?php echo $message->type; ?> debugger_depth_<?php echo $depth; ?> <?php echo $parentClass; ?> "> <td class="debugger_timestamp"><?php echo number_format($message->timer, 3); ?> </td> <td class="debugger_message" style="padding-left: <?php echo $depth * 20 + 10; ?> px"> <span class="message"><?php if ($message->messages) { ?> <span class="handle">▼</span><?php } echo trim($message->text); ?> </span> <span class="file"><?php echo $message->file; ?> Line: <?php echo $message->line; ?> </span> </td> <td class="debugger_duration"><?php if ($message->duration) { echo number_format($message->duration * 1000, 2); } ?> </td> <td class="debugger_memory"><?php echo number_format($message->memory / 1048576, 2); ?> </td> </tr> <?php foreach ($message->messages as $submessage) { printMessage($submessage, $depth + 1, $messageId); } }
/** * 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; }