/** * Footer */ private function footer() { // Completed VividShell::Print(""); VividShell::Repeat("~", 5, $this->sleep(0)); // Errors $errors = $this->app->errorHandler()->fetchAll(); if (count($errors) <= 0) { if ($this->success) { VividShell::Print("{green}{b}Completed!{/}", $this->sleep(100)); VividShell::Print("{gray}%d Triggered Errors", $this->sleep(0), [count($errors)]); } } else { VividShell::Print("{red}Warning", $this->sleep(100)); VividShell::Print("{yellow}%d Triggered Errors", $this->sleep(0), [count($errors)]); VividShell::Print(""); foreach ($errors as $error) { VividShell::Print($error["formatted"] ?? $error["message"] ?? "", $this->sleep(100)); } } // Footprint VividShell::Print(""); VividShell::Print("Execution Time: {gray}%s seconds{/}", $this->sleep(0), [number_format(microtime(true) - $this->timeStamp, 4)]); VividShell::Print("Memory Usage: {gray}%sMB{/} of {gray}%sMB{/}", $this->sleep(0), [round(memory_get_usage(false) / 1024 / 1024, 2), round(memory_get_usage(true) / 1024 / 1024, 2)]); }
/** * ErrorHandler constructor. * @param Kernel $kernel */ public function __construct(Kernel $kernel) { $this->kernel = $kernel; $this->format = '[%type|strtoupper%] %message% in %file|basename% on line # %line%'; $this->method = Kernel::ERRORS_DEFAULT; $this->ignoreNotice = false; $this->flush(); set_error_handler([$this, "handleError"]); set_exception_handler(function (\Throwable $ex) use($kernel) { if ($kernel->isBootstrapped(__METHOD__, true)) { (new Kernel\ErrorHandler\Screen($kernel))->send($ex); } else { exit(strval($ex)); } }); }
/** * @param \Throwable $t * @throws \Comely\Framework\KernelException * @throws \Comely\KnitException */ public function send(\Throwable $t) { $knit = (new Knit())->setTemplatePath(__DIR__)->setCompilerPath($this->kernel->getDisk("cache")); // Extract information from \Throwable $error = ["message" => null, "method" => null, "code" => $t->getCode(), "file" => $t->getFile(), "line" => $t->getLine(), "trace" => []]; // Check if exception has "getTranslated" method $error["message"] = method_exists($t, "getTranslated") ? $t->getTranslated() : $t->getMessage(); // Check if exception is child of ComelyException if (method_exists($t, "getMethod") && is_subclass_of($t, "ComelyException")) { $error["method"] = $t->getMethod(); $error["source"] = "Component"; } else { $error["method"] = get_class($t); $error["source"] = "Caught"; } // Populate Trace foreach ($t->getTrace() as $trace) { if (Arrays::hasKeys($trace, ["function", "file", "line"])) { $trace["method"] = $trace["function"]; if (isset($trace["class"])) { $trace["method"] = $trace["class"] . $trace["type"] . $trace["function"]; } $error["trace"][] = $trace; } } // Config $config = $this->kernel->config()->getNode("app"); $display = ["backtrace" => $config["errorHandler"]["screen"]["debugBacktrace"] ?? false, "triggered" => $config["errorHandler"]["screen"]["triggeredErrors"] ?? false, "paths" => $config["errorHandler"]["screen"]["completePaths"] ?? false]; // Assign values $knit->assign("display", $display); $knit->assign("error", $error); $knit->assign("triggered", $this->kernel->errorHandler()->fetchAll()); $knit->assign("version", ["comely" => \Comely::VERSION, "kernel" => Kernel::VERSION, "framework" => Kernel::VERSION, "knit" => Knit::VERSION]); // Prepare template $screen = $knit->prepare("screen.knit"); $screen = str_replace("%%knit-timer%%", number_format($screen->getTimer(), 6, ".", ""), $screen->getOutput()); exit($screen); }
/** * @param Request $request * @param Response $response * @throws \Comely\IO\Http\Exception\RequestException */ public function init(Request $request, Response $response) { // Save all request related information $this->request = $request; $this->controller = $request->getController(); $this->input = $request->getInput(); $this->method = $request->getMethod(); $this->response = $response; $this->uri = $request->getUri(); $this->page->setProp("root", $request->getUriRoot()); // Get input params $params = $this->input->getData(); try { // Check if we have necessary param to build method name if (!empty($params[self::REST_METHOD_PARAM])) { $callMethod = $this->method . "_" . $params[self::REST_METHOD_PARAM]; $callMethod = \Comely::camelCase($callMethod); } else { // Necessary param not found if ($this->method !== "GET") { // Throw exception if request method is not GET throw new HttpException(get_called_class(), sprintf('Http requests must have required parameter "%s"', self::REST_METHOD_PARAM)); } // If method is GET, default method is getView $callMethod = "getView"; } // Check if method exists if (!method_exists($this, $callMethod)) { throw new HttpException(get_called_class(), sprintf('Request method "%1$s" not found', $callMethod)); } // Call "callBack" method prior to calling request method call_user_func([$this, "callBack"]); // Call method call_user_func([$this, $callMethod]); } catch (KernelException $e) { $this->response->set("message", $e->getMessage()); } // Check number of props in Response object, if ($this->response->count() !== 1) { // populate "errors" property if there are multiple properties already $this->response->set("errors", array_map(function (array $error) { return $error["formatted"]; }, $this->app->errorHandler()->fetchAll())); } }
/** * Security constructor. * @param Kernel $kernel */ public function __construct(Kernel $kernel) { $this->sessionBag = $kernel->getSession()->getBags()->getBag("Comely")->getBag("Framework")->getBag("Security"); }