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