Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
52 / 52 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| WebApp | |
100.00% |
52 / 52 |
|
100.00% |
10 / 10 |
18 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| init | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| process | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| redirect | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| sendContent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| sendError | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| useRouteAnnotations | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| loadErrorPageContent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| isWeb | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handleException | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Dynart\Micro; |
| 4 | use Dynart\Micro\Annotation\RouteAnnotation; |
| 5 | use Dynart\Micro\Middleware\AnnotationProcessor; |
| 6 | |
| 7 | /** |
| 8 | * Handles HTTP request/response |
| 9 | * @package Dynart\Micro |
| 10 | */ |
| 11 | class WebApp extends App { |
| 12 | |
| 13 | const CONFIG_ERROR_PAGES_FOLDER = 'app.error_pages_folder'; |
| 14 | const HEADER_CONTENT_TYPE = 'Content-Type'; |
| 15 | const HEADER_LOCATION = 'Location'; |
| 16 | const CONTENT_TYPE_HTML = 'text/html; charset=UTF-8'; |
| 17 | const CONTENT_TYPE_JSON = 'application/json'; |
| 18 | const ERROR_CONTENT_PLACEHOLDER = '<!-- content -->'; |
| 19 | |
| 20 | /** @var Router */ |
| 21 | protected $router; |
| 22 | |
| 23 | /** @var Response */ |
| 24 | protected $response; |
| 25 | |
| 26 | public function __construct(array $configPaths) { |
| 27 | parent::__construct($configPaths); |
| 28 | Micro::add(Request::class); |
| 29 | Micro::add(Response::class); |
| 30 | Micro::add(Router::class); |
| 31 | Micro::add(Session::class); |
| 32 | Micro::add(View::class); |
| 33 | } |
| 34 | |
| 35 | public function init() { |
| 36 | $this->router = Micro::get(Router::class); |
| 37 | $this->response = Micro::get(Response::class); |
| 38 | } |
| 39 | |
| 40 | public function process() { |
| 41 | list($callable, $params) = $this->router->matchCurrentRoute(); |
| 42 | if ($callable) { |
| 43 | $callable = Micro::getCallable($callable); |
| 44 | $content = call_user_func_array($callable, $params); |
| 45 | $this->sendContent($content); |
| 46 | } else { |
| 47 | $this->sendError(404); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function redirect($location, $params = []) { |
| 52 | $url = substr($location, 0, 4) == 'http' ? $location : $this->router->url($location, $params); |
| 53 | $this->response->clearHeaders(); |
| 54 | $this->response->setHeader(self::HEADER_LOCATION, $url); |
| 55 | $this->response->send(); |
| 56 | $this->finish(); |
| 57 | } |
| 58 | |
| 59 | public function sendContent($content) { |
| 60 | if (is_string($content)) { |
| 61 | $this->response->setHeader(self::HEADER_CONTENT_TYPE, self::CONTENT_TYPE_HTML); |
| 62 | $this->response->send($content); |
| 63 | } else if (is_array($content)) { |
| 64 | $this->response->setHeader(self::HEADER_CONTENT_TYPE, self::CONTENT_TYPE_JSON); |
| 65 | $this->response->send(json_encode($content)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Sends an error response |
| 71 | * @param int $code The error code |
| 72 | * @param string $content The error content |
| 73 | */ |
| 74 | public function sendError(int $code, $content = '') { |
| 75 | if ($this->isWeb()) { // because of testing in cli |
| 76 | http_response_code($code); |
| 77 | } |
| 78 | $pageContent = str_replace(self::ERROR_CONTENT_PLACEHOLDER, $content, $this->loadErrorPageContent($code)); |
| 79 | $this->finish($pageContent); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Call this if you want to use @route annotations |
| 84 | */ |
| 85 | public function useRouteAnnotations() { |
| 86 | $this->addMiddleware(AnnotationProcessor::class); |
| 87 | Micro::add(RouteAnnotation::class); |
| 88 | $annotations = Micro::get(AnnotationProcessor::class); |
| 89 | $annotations->add(RouteAnnotation::class); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * If it exists, loads the content of an error HTML page otherwise |
| 94 | * returns the HTML comment for the error placeholder |
| 95 | * |
| 96 | * @param int $code The HTTP status code for the error |
| 97 | * @return string The content of the HTML file or the HTML comment for the error placeholder |
| 98 | */ |
| 99 | protected function loadErrorPageContent(int $code): string { |
| 100 | $dir = $this->config->get(self::CONFIG_ERROR_PAGES_FOLDER); |
| 101 | if ($dir) { |
| 102 | $path = $this->config->getFullPath($dir.'/'.$code.'.html'); |
| 103 | if (file_exists($path)) { |
| 104 | return file_get_contents($path); |
| 105 | } |
| 106 | } |
| 107 | return self::ERROR_CONTENT_PLACEHOLDER; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns true if the call is from the web |
| 112 | * @return bool |
| 113 | */ |
| 114 | protected function isWeb() { |
| 115 | return http_response_code() !== false; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Handles the exception |
| 120 | * |
| 121 | * Calls the parent exception handler, then calls the sendError with HTTP error 500. |
| 122 | * Sets the content for the error placeholder if the environment is not production. |
| 123 | * |
| 124 | * @param \Exception $e The exception for handling |
| 125 | */ |
| 126 | protected function handleException(\Exception $e): void { |
| 127 | parent::handleException($e); |
| 128 | $env = $this->config->get(App::CONFIG_ENVIRONMENT, App::PRODUCTION_ENVIRONMENT); |
| 129 | if ($env != App::PRODUCTION_ENVIRONMENT) { |
| 130 | $type = get_class($e); |
| 131 | $file = $e->getFile(); |
| 132 | $line = $e->getLine(); |
| 133 | $message = $e->getMessage(); |
| 134 | $trace = $e->getTraceAsString(); |
| 135 | $content = "<h2>$type</h2>\n<p>In <b>$file</b> on <b>line $line</b> with message: $message</p>\n"; |
| 136 | $content .= "<h3>Stacktrace:</h3>\n<p>".str_replace("\n", "<br>\n", $trace)."</p>"; |
| 137 | } else { |
| 138 | $content = ''; |
| 139 | } |
| 140 | $this->sendError(500, $content); |
| 141 | } |
| 142 | |
| 143 | } |