1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Application;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class Application
17: {
18: use Nette\SmartObject;
19:
20:
21: public static $maxLoop = 20;
22:
23:
24: public $catchExceptions;
25:
26:
27: public $errorPresenter;
28:
29:
30: public $onStartup;
31:
32:
33: public $onShutdown;
34:
35:
36: public $onRequest;
37:
38:
39: public $onPresenter;
40:
41:
42: public $onResponse;
43:
44:
45: public $onError;
46:
47:
48: private $requests = [];
49:
50:
51: private $presenter;
52:
53:
54: private $httpRequest;
55:
56:
57: private $httpResponse;
58:
59:
60: private $presenterFactory;
61:
62:
63: private $router;
64:
65:
66: public function __construct(IPresenterFactory $presenterFactory, IRouter $router, Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
67: {
68: $this->httpRequest = $httpRequest;
69: $this->httpResponse = $httpResponse;
70: $this->presenterFactory = $presenterFactory;
71: $this->router = $router;
72: }
73:
74:
75: 76: 77: 78:
79: public function run()
80: {
81: try {
82: $this->onStartup($this);
83: $this->processRequest($this->createInitialRequest());
84: $this->onShutdown($this);
85:
86: } catch (\Exception $e) {
87: } catch (\Throwable $e) {
88: }
89: if (isset($e)) {
90: $this->onError($this, $e);
91: if ($this->catchExceptions && $this->errorPresenter) {
92: try {
93: $this->processException($e);
94: $this->onShutdown($this, $e);
95: return;
96:
97: } catch (\Exception $e) {
98: $this->onError($this, $e);
99: } catch (\Throwable $e) {
100: $this->onError($this, $e);
101: }
102: }
103: $this->onShutdown($this, $e);
104: throw $e;
105: }
106: }
107:
108:
109: 110: 111:
112: public function createInitialRequest()
113: {
114: $request = $this->router->match($this->httpRequest);
115: if (!$request instanceof Request) {
116: throw new BadRequestException('No route for HTTP request.');
117: }
118: return $request;
119: }
120:
121:
122: 123: 124:
125: public function processRequest(Request $request)
126: {
127: process:
128: if (count($this->requests) > self::$maxLoop) {
129: throw new ApplicationException('Too many loops detected in application life cycle.');
130: }
131:
132: $this->requests[] = $request;
133: $this->onRequest($this, $request);
134:
135: if (!$request->isMethod($request::FORWARD) && !strcasecmp($request->getPresenterName(), $this->errorPresenter)) {
136: throw new BadRequestException('Invalid request. Presenter is not achievable.');
137: }
138:
139: try {
140: $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
141: } catch (InvalidPresenterException $e) {
142: throw count($this->requests) > 1 ? $e : new BadRequestException($e->getMessage(), 0, $e);
143: }
144: $this->onPresenter($this, $this->presenter);
145: $response = $this->presenter->run(clone $request);
146:
147: if ($response instanceof Responses\ForwardResponse) {
148: $request = $response->getRequest();
149: goto process;
150:
151: } elseif ($response) {
152: $this->onResponse($this, $response);
153: $response->send($this->httpRequest, $this->httpResponse);
154: }
155: }
156:
157:
158: 159: 160: 161:
162: public function processException($e)
163: {
164: if (!$e instanceof BadRequestException && $this->httpResponse instanceof Nette\Http\Response) {
165: $this->httpResponse->warnOnBuffer = false;
166: }
167: if (!$this->httpResponse->isSent()) {
168: $this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getHttpCode() ?: 404) : 500);
169: }
170:
171: $args = ['exception' => $e, 'request' => end($this->requests) ?: null];
172: if ($this->presenter instanceof UI\Presenter) {
173: try {
174: $this->presenter->forward(":$this->errorPresenter:", $args);
175: } catch (AbortException $foo) {
176: $this->processRequest($this->presenter->getLastCreatedRequest());
177: }
178: } else {
179: $this->processRequest(new Request($this->errorPresenter, Request::FORWARD, $args));
180: }
181: }
182:
183:
184: 185: 186: 187:
188: public function getRequests()
189: {
190: return $this->requests;
191: }
192:
193:
194: 195: 196: 197:
198: public function getPresenter()
199: {
200: return $this->presenter;
201: }
202:
203:
204:
205:
206:
207: 208: 209: 210:
211: public function getRouter()
212: {
213: return $this->router;
214: }
215:
216:
217: 218: 219: 220:
221: public function getPresenterFactory()
222: {
223: return $this->presenterFactory;
224: }
225: }
226: