1: <?php
2:
3: 4: 5: 6: 7:
8:
9:
10:
11: 12: 13: 14: 15: 16: 17: 18:
19: class Nette_MicroPresenter extends Object implements IPresenter
20: {
21:
22: private $context;
23:
24:
25: private $request;
26:
27:
28: public function __construct(DIContainer $context)
29: {
30: $this->context = $context;
31: }
32:
33:
34: 35: 36: 37:
38: public function getContext()
39: {
40: return $this->context;
41: }
42:
43:
44: 45: 46:
47: public function run(PresenterRequest $request)
48: {
49: $this->request = $request;
50:
51: $httpRequest = $this->context->getByType('IHttpRequest');
52: if (!$httpRequest->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) {
53: $refUrl = clone $httpRequest->getUrl();
54: $url = $this->context->getService('router')->constructUrl($request, $refUrl->setPath($refUrl->getScriptPath()));
55: if ($url !== NULL && !$httpRequest->getUrl()->isEqual($url)) {
56: return new RedirectResponse($url, IHttpResponse::S301_MOVED_PERMANENTLY);
57: }
58: }
59:
60: $params = $request->getParameters();
61: if (!isset($params['callback'])) {
62: throw new BadRequestException('Parameter callback is missing.');
63: }
64: $params['presenter'] = $this;
65: $callback = new Callback($params['callback']);
66: $response = $callback->invokeArgs(PresenterComponentReflection::combineArgs($callback->toReflection(), $params));
67:
68: if (is_string($response)) {
69: $response = array($response, array());
70: }
71: if (is_array($response)) {
72: if ($response[0] instanceof SplFileInfo) {
73: $response = $this->createTemplate('FileTemplate')
74: ->setParameters($response[1])->setFile($response[0]);
75: } else {
76: $response = $this->createTemplate('Template')
77: ->setParameters($response[1])->setSource($response[0]);
78: }
79: }
80: if ($response instanceof ITemplate) {
81: return new TextResponse($response);
82: } else {
83: return $response;
84: }
85: }
86:
87:
88: 89: 90: 91: 92: 93:
94: public function createTemplate($class = NULL, $latteFactory = NULL)
95: {
96: $template = $class ? new $class : new FileTemplate;
97:
98: $template->setParameters($this->request->getParameters());
99: $template->presenter = $this;
100: $template->context = $context = $this->context;
101: $url = $context->getByType('IHttpRequest')->getUrl();
102: $template->baseUrl = rtrim($url->getBaseUrl(), '/');
103: $template->basePath = rtrim($url->getBasePath(), '/');
104:
105: $template->registerHelperLoader('TemplateHelpers::loader');
106: $template->setCacheStorage($context->getService('nette.templateCacheStorage'));
107: $template->onPrepareFilters[] = create_function('ITemplate $template', 'extract($GLOBALS[0]['.array_push($GLOBALS[0], array('latteFactory'=>$latteFactory)).'-1], EXTR_REFS);
108: $template->registerFilter($latteFactory ? $latteFactory() : new LatteFilter);
109: ');
110: return $template;
111: }
112:
113:
114: 115: 116: 117: 118: 119:
120: public function redirectUrl($url, $code = IHttpResponse::S302_FOUND)
121: {
122: return new RedirectResponse($url, $code);
123: }
124:
125:
126: 127: 128: 129: 130: 131: 132:
133: public function error($message = NULL, $code = IHttpResponse::S404_NOT_FOUND)
134: {
135: throw new BadRequestException($message, $code);
136: }
137:
138:
139: 140: 141:
142: public function getRequest()
143: {
144: return $this->request;
145: }
146:
147: }
148: