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