Passed
Push — master ( 5b5ed9...47b84a )
by Alex
02:17
created

CommonApplication.php (3 issues)

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
The parameter $route is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

68
    public function noRouteFoundErrorHandler(/** @scrutinizer ignore-unused */ string $route): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
     * Formatting call stack
85
     *
86
     * @param mixed $e
87
     *            Exception object
88
     */
89
    protected function formatCallStack($e): array
90
    {
91
        $stack = $e->getTrace();
92
93
        foreach ($stack as $i => $call) {
94
            $stack[$i] = (@$call['file'] == '' ? 'lambda : ' : @$call['file'] . ' (' . $call['line'] . ') : ') .
95
                (@$call['class'] == '' ? '' : $call['class'] . '->') . $call['function'];
96
        }
97
98
        return $stack;
99
    }
100
101
    /**
102
     * Method formats exception object
103
     *
104
     * @param \Exception $e
105
     *            Exception
106
     * @return object Formatted exception object
107
     */
108
    protected function baseFormatter(\Exception $e): object
109
    {
110
        $error = new \stdClass();
111
        $error->message = $e->getMessage();
112
        $error->code = $e->getCode();
113
        $error->call_stack = $this->formatCallStack($e);
114
        if (isset($_SERVER['HTTP_HOST']) && $_SERVER['REQUEST_URI']) {
115
            $error->host = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
116
        } else {
117
            $error->host = 'undefined';
118
        }
119
        return $error;
120
    }
121
122
    /**
123
     * Method processes exception.
124
     *
125
     * @param \Mezon\Rest\Exception $e
126
     *            RestException object.
127
     */
128
    public function handleRestException(\Mezon\Rest\Exception $e): void
129
    {
130
        $error = $this->baseFormatter($e);
131
132
        $error->httpBody = $e->getHttpBody();
133
134
        print('<pre>' . json_encode($error, JSON_PRETTY_PRINT));
135
    }
136
137
    /**
138
     * Method processes exception.
139
     *
140
     * @param \Exception $e
141
     *            Exception object.
142
     */
143
    public function handleException(\Exception $e): void
144
    {
145
        $error = $this->baseFormatter($e);
146
147
        print('<pre>' . json_encode($error, JSON_PRETTY_PRINT));
148
    }
149
150
    /**
151
     * Running application.
152
     */
153
    public function run(): void
154
    {
155
        try {
156
            $callRouteResult = $this->callRoute();
157
            if (is_array($callRouteResult) === false) {
158
                throw (new \Exception('Route was not called properly'));
159
            }
160
161
            $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 ignore-type  annotation

161
            $result = array_merge(/** @scrutinizer ignore-type */ $callRouteResult, $this->crossRender());
Loading history...
162
163
            if (is_array($result)) {
0 ignored issues
show
The condition is_array($result) is always true.
Loading history...
164
                foreach ($result as $key => $value) {
165
                    $content = $value instanceof \Mezon\Application\ViewInterface ? $value->render() : $value;
166
167
                    $this->template->setPageVar($key, $content);
168
                }
169
            }
170
171
            print($this->template->compile());
172
        } catch (\Mezon\Rest\Exception $e) {
173
            $this->handleRestException($e);
174
        } catch (\Exception $e) {
175
            $this->handleException($e);
176
        }
177
    }
178
179
    /**
180
     * Getting template
181
     *
182
     * @return \Mezon\HtmlTemplate\HtmlTemplate Application's template
183
     * @codeCoverageIgnore
184
     */
185
    public function getTemplate(): \Mezon\HtmlTemplate\HtmlTemplate
186
    {
187
        return $this->template;
188
    }
189
190
    /**
191
     * Setting template
192
     *
193
     * @param \Mezon\HtmlTemplate\HtmlTemplate $template
194
     *            Template
195
     * @codeCoverageIgnore
196
     */
197
    public function setTemplate(\Mezon\HtmlTemplate\HtmlTemplate $template): void
198
    {
199
        $this->template = $template;
200
    }
201
}
202