1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Application;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: class Application extends Nette\Object
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 = array();
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: $this->onError($this, $e);
88: if ($this->catchExceptions && $this->errorPresenter) {
89: try {
90: $this->processException($e);
91: $this->onShutdown($this, $e);
92: return;
93:
94: } catch (\Exception $e) {
95: $this->onError($this, $e);
96: }
97: }
98: $this->onShutdown($this, $e);
99: throw $e;
100: }
101: }
102:
103:
104: 105: 106:
107: public function createInitialRequest()
108: {
109: $request = $this->router->match($this->httpRequest);
110:
111: if (!$request instanceof Request) {
112: throw new BadRequestException('No route for HTTP request.');
113: }
114: $name = $request->getPresenterName();
115: if (strcasecmp($name, $this->errorPresenter) === 0
116: || (Nette\Utils\Strings::startsWith($name, 'Nette:') && $name !== 'Nette:Micro')
117: ) {
118: throw new BadRequestException('Invalid request. Presenter is not achievable.');
119: }
120:
121: try {
122: $this->presenterFactory->getPresenterClass($name);
123: $request->setPresenterName($name);
124: } catch (InvalidPresenterException $e) {
125: throw new BadRequestException($e->getMessage(), 0, $e);
126: }
127:
128: return $request;
129: }
130:
131:
132: 133: 134:
135: public function processRequest(Request $request)
136: {
137: if (count($this->requests) > self::$maxLoop) {
138: throw new ApplicationException('Too many loops detected in application life cycle.');
139: }
140:
141: $this->requests[] = $request;
142: $this->onRequest($this, $request);
143:
144: $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
145: $this->onPresenter($this, $this->presenter);
146: $response = $this->presenter->run($request);
147:
148: if ($response instanceof Responses\ForwardResponse) {
149: $this->processRequest($response->getRequest());
150:
151: } elseif ($response) {
152: $this->onResponse($this, $response);
153: $response->send($this->httpRequest, $this->httpResponse);
154: }
155: }
156:
157:
158: 159: 160:
161: public function processException(\Exception $e)
162: {
163: if (!$e instanceof BadRequestException && $this->httpResponse instanceof Nette\Http\Response) {
164: $this->httpResponse->warnOnBuffer = FALSE;
165: }
166: if (!$this->httpResponse->isSent()) {
167: $this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getCode() ?: 404) : 500);
168: }
169:
170: $args = array('exception' => $e, 'request' => end($this->requests) ?: NULL);
171: if ($this->presenter instanceof UI\Presenter) {
172: try {
173: $this->presenter->forward(":$this->errorPresenter:", $args);
174: } catch (AbortException $foo) {
175: $this->processRequest($this->presenter->getLastCreatedRequest());
176: }
177: } else {
178: $this->processRequest(new Request($this->errorPresenter, Request::FORWARD, $args));
179: }
180: }
181:
182:
183: 184: 185: 186:
187: public function getRequests()
188: {
189: return $this->requests;
190: }
191:
192:
193: 194: 195: 196:
197: public function getPresenter()
198: {
199: return $this->presenter;
200: }
201:
202:
203:
204:
205:
206: 207: 208: 209:
210: public function getRouter()
211: {
212: return $this->router;
213: }
214:
215:
216: 217: 218: 219:
220: public function getPresenterFactory()
221: {
222: return $this->presenterFactory;
223: }
224:
225:
226:
227:
228:
229:
230: function storeRequest($expiration = '+ 10 minutes')
231: {
232: trigger_error(__METHOD__ . '() is deprecated; use $presenter->storeRequest() instead.', E_USER_DEPRECATED);
233: return $this->presenter->storeRequest($expiration);
234: }
235:
236:
237: function restoreRequest($key)
238: {
239: trigger_error(__METHOD__ . '() is deprecated; use $presenter->restoreRequest() instead.', E_USER_DEPRECATED);
240: return $this->presenter->restoreRequest($key);
241: }
242:
243: }
244: