1: <?php
2:
3: 4: 5: 6:
7:
8: namespace NetteModule;
9:
10: use Nette;
11: use Nette\Application;
12: use Nette\Application\Responses;
13: use Nette\Http;
14: use Latte;
15:
16:
17: 18: 19: 20: 21:
22: class MicroPresenter extends Nette\Object implements Application\IPresenter
23: {
24:
25: private $context;
26:
27:
28: private $httpRequest;
29:
30:
31: private $router;
32:
33:
34: private $request;
35:
36:
37: public function __construct(Nette\DI\Container $context = NULL, Http\IRequest $httpRequest = NULL, Application\IRouter $router = NULL)
38: {
39: $this->context = $context;
40: $this->httpRequest = $httpRequest;
41: $this->router = $router;
42: }
43:
44:
45: 46: 47: 48:
49: public function getContext()
50: {
51: return $this->context;
52: }
53:
54:
55: 56: 57:
58: public function run(Application\Request $request)
59: {
60: $this->request = $request;
61:
62: if ($this->httpRequest && $this->router && !$this->httpRequest->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) {
63: $refUrl = clone $this->httpRequest->getUrl();
64: $url = $this->router->constructUrl($request, $refUrl->setPath($refUrl->getScriptPath()));
65: if ($url !== NULL && !$this->httpRequest->getUrl()->isEqual($url)) {
66: return new Responses\RedirectResponse($url, Http\IResponse::S301_MOVED_PERMANENTLY);
67: }
68: }
69:
70: $params = $request->getParameters();
71: if (!isset($params['callback'])) {
72: throw new Application\BadRequestException('Parameter callback is missing.');
73: }
74: $params['presenter'] = $this;
75: $callback = $params['callback'];
76: $reflection = Nette\Utils\Callback::toReflection(Nette\Utils\Callback::check($callback));
77: $params = Application\UI\PresenterComponentReflection::combineArgs($reflection, $params);
78:
79: if ($this->context) {
80: foreach ($reflection->getParameters() as $param) {
81: if ($param->getClassName()) {
82: unset($params[$param->getPosition()]);
83: }
84: }
85:
86: $params = Nette\DI\Helpers::autowireArguments($reflection, $params, $this->context);
87: $params['presenter'] = $this;
88: }
89:
90: $response = call_user_func_array($callback, $params);
91:
92: if (is_string($response)) {
93: $response = array($response, array());
94: }
95: if (is_array($response)) {
96: list($templateSource, $templateParams) = $response;
97: $response = $this->createTemplate()->setParameters($templateParams);
98: if (!$templateSource instanceof \SplFileInfo) {
99: $response->getLatte()->setLoader(new Latte\Loaders\StringLoader);
100: }
101: $response->setFile($templateSource);
102: }
103: if ($response instanceof Application\UI\ITemplate) {
104: return new Responses\TextResponse($response);
105: } else {
106: return $response;
107: }
108: }
109:
110:
111: 112: 113: 114: 115: 116:
117: public function createTemplate($class = NULL, $latteFactory = NULL)
118: {
119: $latte = $latteFactory ? $latteFactory() : $this->getContext()->getByType('Nette\Bridges\ApplicationLatte\ILatteFactory')->create();
120: $template = $class ? new $class : new Nette\Bridges\ApplicationLatte\Template($latte);
121:
122: $template->setParameters($this->request->getParameters());
123: $template->presenter = $this;
124: $template->context = $this->context;
125: if ($this->httpRequest) {
126: $url = $this->httpRequest->getUrl();
127: $template->baseUrl = rtrim($url->getBaseUrl(), '/');
128: $template->basePath = rtrim($url->getBasePath(), '/');
129: }
130: return $template;
131: }
132:
133:
134: 135: 136: 137: 138: 139:
140: public function redirectUrl($url, $code = Http\IResponse::S302_FOUND)
141: {
142: return new Responses\RedirectResponse($url, $code);
143: }
144:
145:
146: 147: 148: 149: 150: 151: 152:
153: public function error($message = NULL, $code = Http\IResponse::S404_NOT_FOUND)
154: {
155: throw new Application\BadRequestException($message, $code);
156: }
157:
158:
159: 160: 161:
162: public function getRequest()
163: {
164: return $this->request;
165: }
166:
167: }
168: