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