alexdodonov /
mezon-application
| 1 | <?php |
||||
| 2 | namespace Mezon\Application; |
||||
| 3 | |||||
| 4 | /** |
||||
| 5 | * Class CommonApplication |
||||
| 6 | * |
||||
| 7 | * @package Mezon |
||||
| 8 | * @subpackage CommonApplication |
||||
| 9 | * @author Dodonov A.A. |
||||
| 10 | * @version v.1.0 (2019/08/07) |
||||
| 11 | * @copyright Copyright (c) 2019, aeon.org |
||||
| 12 | */ |
||||
| 13 | |||||
| 14 | /** |
||||
| 15 | * Common application with any available template |
||||
| 16 | * |
||||
| 17 | * To load routes from the config call $this->load_routes_from_config('./conf/routes.json'); |
||||
| 18 | * |
||||
| 19 | * The format of the *.json config must be like this: |
||||
| 20 | * |
||||
| 21 | * [ |
||||
| 22 | * { |
||||
| 23 | * "route" : "/route1" , |
||||
| 24 | * "callback" : "callback1" , |
||||
| 25 | * "method" : "POST" |
||||
| 26 | * } , |
||||
| 27 | * { |
||||
| 28 | * "route" : "/route2" , |
||||
| 29 | * "callback" : "callback2" , |
||||
| 30 | * "method" : ["GET" , "POST"] |
||||
| 31 | * } |
||||
| 32 | * ] |
||||
| 33 | */ |
||||
| 34 | class CommonApplication extends \Mezon\Application\Application |
||||
| 35 | { |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * Application's template |
||||
| 39 | * |
||||
| 40 | * @var \Mezon\HtmlTemplate\HtmlTemplate |
||||
| 41 | */ |
||||
| 42 | private $template = false; |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * Constructor |
||||
| 46 | * |
||||
| 47 | * @param \Mezon\HtmlTemplate\HtmlTemplate $template |
||||
| 48 | * Template |
||||
| 49 | */ |
||||
| 50 | public function __construct(\Mezon\HtmlTemplate\HtmlTemplate $template) |
||||
| 51 | { |
||||
| 52 | parent::__construct(); |
||||
| 53 | |||||
| 54 | $this->template = $template; |
||||
| 55 | |||||
| 56 | $this->getRouter()->setNoProcessorFoundErrorHandler([ |
||||
| 57 | $this, |
||||
| 58 | 'noRouteFoundErrorHandler' |
||||
| 59 | ]); |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * Method handles 404 errors |
||||
| 64 | * |
||||
| 65 | * @param string $route |
||||
| 66 | * @codeCoverageIgnore |
||||
| 67 | */ |
||||
| 68 | public function noRouteFoundErrorHandler(string $route): void |
||||
|
0 ignored issues
–
show
|
|||||
| 69 | { |
||||
| 70 | $this->redirectTo('/404'); |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | /** |
||||
| 74 | * Method renders common parts of all pages. |
||||
| 75 | * |
||||
| 76 | * @return array List of common parts. |
||||
| 77 | */ |
||||
| 78 | public function crossRender(): array |
||||
| 79 | { |
||||
| 80 | return []; |
||||
| 81 | } |
||||
| 82 | |||||
| 83 | /** |
||||
| 84 | * Method formats exception object |
||||
| 85 | * |
||||
| 86 | * @param \Exception $e |
||||
| 87 | * Exception |
||||
| 88 | * @return object Formatted exception object |
||||
| 89 | */ |
||||
| 90 | protected function baseFormatter(\Exception $e): object |
||||
| 91 | { |
||||
| 92 | $error = new \stdClass(); |
||||
| 93 | $error->message = $e->getMessage(); |
||||
| 94 | $error->code = $e->getCode(); |
||||
| 95 | $error->call_stack = $this->formatCallStack($e); |
||||
| 96 | if (isset($_SERVER['HTTP_HOST']) && $_SERVER['REQUEST_URI']) { |
||||
| 97 | $error->host = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||||
| 98 | } else { |
||||
| 99 | $error->host = 'undefined'; |
||||
| 100 | } |
||||
| 101 | return $error; |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * Method processes exception. |
||||
| 106 | * |
||||
| 107 | * @param \Mezon\Rest\Exception $e |
||||
| 108 | * RestException object. |
||||
| 109 | */ |
||||
| 110 | public function handleRestException(\Mezon\Rest\Exception $e): void |
||||
| 111 | { |
||||
| 112 | $error = $this->baseFormatter($e); |
||||
| 113 | |||||
| 114 | $error->httpBody = $e->getHttpBody(); |
||||
| 115 | |||||
| 116 | print('<pre>' . json_encode($error, JSON_PRETTY_PRINT)); |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | /** |
||||
| 120 | * Method processes exception. |
||||
| 121 | * |
||||
| 122 | * @param \Exception $e |
||||
| 123 | * Exception object. |
||||
| 124 | */ |
||||
| 125 | public function handleException(\Exception $e): void |
||||
| 126 | { |
||||
| 127 | $error = $this->baseFormatter($e); |
||||
| 128 | |||||
| 129 | print('<pre>' . json_encode($error, JSON_PRETTY_PRINT)); |
||||
| 130 | } |
||||
| 131 | |||||
| 132 | /** |
||||
| 133 | * Running application. |
||||
| 134 | */ |
||||
| 135 | public function run(): void |
||||
| 136 | { |
||||
| 137 | try { |
||||
| 138 | $callRouteResult = $this->callRoute(); |
||||
| 139 | if (is_array($callRouteResult) === false) { |
||||
| 140 | throw (new \Exception('Route was not called properly')); |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | $result = array_merge($callRouteResult, $this->crossRender()); |
||||
|
0 ignored issues
–
show
It seems like
$callRouteResult can also be of type boolean and null and string; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 144 | |||||
| 145 | if (is_array($result)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 146 | foreach ($result as $key => $value) { |
||||
| 147 | $content = $value instanceof \Mezon\Application\ViewInterface ? $value->render() : $value; |
||||
| 148 | |||||
| 149 | $this->template->setPageVar($key, $content); |
||||
| 150 | } |
||||
| 151 | } |
||||
| 152 | |||||
| 153 | print($this->template->compile()); |
||||
| 154 | } catch (\Mezon\Rest\Exception $e) { |
||||
| 155 | $this->handleRestException($e); |
||||
| 156 | } catch (\Exception $e) { |
||||
| 157 | $this->handleException($e); |
||||
| 158 | } |
||||
| 159 | } |
||||
| 160 | |||||
| 161 | /** |
||||
| 162 | * Getting template |
||||
| 163 | * |
||||
| 164 | * @return \Mezon\HtmlTemplate\HtmlTemplate Application's template |
||||
| 165 | * @codeCoverageIgnore |
||||
| 166 | */ |
||||
| 167 | public function getTemplate(): \Mezon\HtmlTemplate\HtmlTemplate |
||||
| 168 | { |
||||
| 169 | return $this->template; |
||||
| 170 | } |
||||
| 171 | |||||
| 172 | /** |
||||
| 173 | * Setting template |
||||
| 174 | * |
||||
| 175 | * @param \Mezon\HtmlTemplate\HtmlTemplate $template |
||||
| 176 | * Template |
||||
| 177 | * @codeCoverageIgnore |
||||
| 178 | */ |
||||
| 179 | public function setTemplate(\Mezon\HtmlTemplate\HtmlTemplate $template): void |
||||
| 180 | { |
||||
| 181 | $this->template = $template; |
||||
| 182 | } |
||||
| 183 | } |
||||
| 184 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.