1: <?php
2:
3: 4: 5: 6:
7:
8: namespace NetteModule;
9:
10: use Nette,
11: Nette\Application,
12: Nette\Application\Responses,
13: 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->getService('router')->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 = new Nette\Callback($params['callback']);
70: $response = $callback->invokeArgs(Application\UI\PresenterComponentReflection::combineArgs($callback->toReflection(), $params));
71:
72: if (is_string($response)) {
73: $response = array($response, array());
74: }
75: if (is_array($response)) {
76: if ($response[0] instanceof \SplFileInfo) {
77: $response = $this->createTemplate('Nette\Templating\FileTemplate')
78: ->setParameters($response[1])->setFile($response[0]);
79: } else {
80: $response = $this->createTemplate('Nette\Templating\Template')
81: ->setParameters($response[1])->setSource($response[0]);
82: }
83: }
84: if ($response instanceof Nette\Templating\ITemplate) {
85: return new Responses\TextResponse($response);
86: } else {
87: return $response;
88: }
89: }
90:
91:
92: 93: 94: 95: 96: 97:
98: public function createTemplate($class = NULL, $latteFactory = NULL)
99: {
100: $template = $class ? new $class : new Nette\Templating\FileTemplate;
101:
102: $template->setParameters($this->request->getParameters());
103: $template->presenter = $this;
104: $template->context = $context = $this->context;
105: $url = $context->getByType('Nette\Http\IRequest')->getUrl();
106: $template->baseUrl = rtrim($url->getBaseUrl(), '/');
107: $template->basePath = rtrim($url->getBasePath(), '/');
108:
109: $template->registerHelperLoader('Nette\Templating\Helpers::loader');
110: $template->setCacheStorage($context->getService('nette.templateCacheStorage'));
111: $template->onPrepareFilters[] = function(Nette\Templating\ITemplate $template) use ($latteFactory) {
112: $template->registerFilter($latteFactory ? $latteFactory() : new Nette\Latte\Engine);
113: };
114: return $template;
115: }
116:
117:
118: 119: 120: 121: 122: 123:
124: public function redirectUrl($url, $code = Http\IResponse::S302_FOUND)
125: {
126: return new Responses\RedirectResponse($url, $code);
127: }
128:
129:
130: 131: 132: 133: 134: 135: 136:
137: public function error($message = NULL, $code = Http\IResponse::S404_NOT_FOUND)
138: {
139: throw new Application\BadRequestException($message, $code);
140: }
141:
142:
143: 144: 145:
146: public function getRequest()
147: {
148: return $this->request;
149: }
150:
151: }
152: