1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Application\UI;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: abstract class PresenterComponent extends Nette\ComponentModel\Container implements ISignalReceiver, IStatePersistent, \ArrayAccess
26: {
27:
28: protected $params = array();
29:
30:
31: 32: 33: 34: 35:
36: public function getPresenter($need = TRUE)
37: {
38: return $this->lookup('Nette\Application\UI\Presenter', $need);
39: }
40:
41:
42: 43: 44: 45: 46:
47: public function getUniqueId()
48: {
49: return $this->lookupPath('Nette\Application\UI\Presenter', TRUE);
50: }
51:
52:
53: 54: 55: 56: 57: 58:
59: protected function attached($presenter)
60: {
61: if ($presenter instanceof Presenter) {
62: $this->loadState($presenter->popGlobalParameters($this->getUniqueId()));
63: }
64: }
65:
66:
67: 68: 69:
70: protected function validateParent(Nette\ComponentModel\IContainer $parent)
71: {
72: parent::validateParent($parent);
73: $this->monitor('Nette\Application\UI\Presenter');
74: }
75:
76:
77: 78: 79: 80: 81: 82:
83: protected function tryCall($method, array $params)
84: {
85: $rc = $this->getReflection();
86: if ($rc->hasMethod($method)) {
87: $rm = $rc->getMethod($method);
88: if ($rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic()) {
89: $this->checkRequirements($rm);
90: $rm->invokeArgs($this, $rc->combineArgs($rm, $params));
91: return TRUE;
92: }
93: }
94: return FALSE;
95: }
96:
97:
98: 99: 100: 101:
102: public function checkRequirements($element)
103: {
104: }
105:
106:
107: 108: 109: 110:
111: public static function getReflection()
112: {
113: return new PresenterComponentReflection(get_called_class());
114: }
115:
116:
117:
118:
119:
120: 121: 122: 123: 124:
125: public function loadState(array $params)
126: {
127: $reflection = $this->getReflection();
128: foreach ($reflection->getPersistentParams() as $name => $meta) {
129: if (isset($params[$name])) {
130: $type = gettype($meta['def']);
131: if (!$reflection->convertType($params[$name], $type)) {
132: throw new Nette\Application\BadRequestException("Invalid value for persistent parameter '$name' in '{$this->getName()}', expected " . ($type === 'NULL' ? 'scalar' : $type) . ".");
133: }
134: $this->$name = & $params[$name];
135: } else {
136: $params[$name] = & $this->$name;
137: }
138: }
139: $this->params = $params;
140: }
141:
142:
143: 144: 145: 146: 147: 148:
149: public function saveState(array & $params, $reflection = NULL)
150: {
151: $reflection = $reflection === NULL ? $this->getReflection() : $reflection;
152: foreach ($reflection->getPersistentParams() as $name => $meta) {
153:
154: if (isset($params[$name])) {
155:
156:
157: } elseif (array_key_exists($name, $params)) {
158: continue;
159:
160: } elseif (!isset($meta['since']) || $this instanceof $meta['since']) {
161: $params[$name] = $this->$name;
162:
163: } else {
164: continue;
165: }
166:
167: $type = gettype($meta['def']);
168: if (!PresenterComponentReflection::convertType($params[$name], $type)) {
169: throw new InvalidLinkException(sprintf("Invalid value for persistent parameter '%s' in '%s', expected %s.", $name, $this->getName(), $type === 'NULL' ? 'scalar' : $type));
170: }
171:
172: if ($params[$name] === $meta['def'] || ($meta['def'] === NULL && is_scalar($params[$name]) && (string) $params[$name] === '')) {
173: $params[$name] = NULL;
174: }
175: }
176: }
177:
178:
179: 180: 181: 182: 183: 184:
185: public function getParameter($name = NULL, $default = NULL)
186: {
187: if (func_num_args() === 0) {
188: trigger_error('Calling ' . __METHOD__ . ' with no arguments to get all parameters is deprecated, use getParameters() instead.', E_USER_DEPRECATED);
189: return $this->params;
190:
191: } elseif (isset($this->params[$name])) {
192: return $this->params[$name];
193:
194: } else {
195: return $default;
196: }
197: }
198:
199:
200: 201: 202: 203:
204: public function getParameters()
205: {
206: return $this->params;
207: }
208:
209:
210: 211: 212: 213: 214:
215: public function getParameterId($name)
216: {
217: $uid = $this->getUniqueId();
218: return $uid === '' ? $name : $uid . self::NAME_SEPARATOR . $name;
219: }
220:
221:
222:
223: function getParam($name = NULL, $default = NULL)
224: {
225:
226: return func_num_args() ? $this->getParameter($name, $default) : $this->getParameter();
227: }
228:
229:
230: 231: 232: 233: 234:
235: public static function getPersistentParams()
236: {
237: $rc = new Nette\Reflection\ClassType(get_called_class());
238: $params = array();
239: foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC) as $rp) {
240: if (!$rp->isStatic() && $rp->hasAnnotation('persistent')) {
241: $params[] = $rp->getName();
242: }
243: }
244: return $params;
245: }
246:
247:
248:
249:
250:
251: 252: 253: 254: 255: 256:
257: public function signalReceived($signal)
258: {
259: if (!$this->tryCall($this->formatSignalMethod($signal), $this->params)) {
260: $class = get_class($this);
261: throw new BadSignalException("There is no handler for signal '$signal' in class $class.");
262: }
263: }
264:
265:
266: 267: 268: 269: 270:
271: public static function formatSignalMethod($signal)
272: {
273: return $signal == NULL ? NULL : 'handle' . $signal;
274: }
275:
276:
277:
278:
279:
280: 281: 282: 283: 284: 285: 286:
287: public function link($destination, $args = array())
288: {
289: try {
290: return $this->getPresenter()->createRequest($this, $destination, is_array($args) ? $args : array_slice(func_get_args(), 1), 'link');
291:
292: } catch (InvalidLinkException $e) {
293: return $this->getPresenter()->handleInvalidLink($e);
294: }
295: }
296:
297:
298: 299: 300: 301: 302: 303:
304: public function lazyLink($destination, $args = array())
305: {
306: return new Link($this, $destination, is_array($args) ? $args : array_slice(func_get_args(), 1));
307: }
308:
309:
310: 311: 312: 313: 314: 315: 316:
317: public function isLinkCurrent($destination = NULL, $args = array())
318: {
319: if ($destination !== NULL) {
320: $this->getPresenter()->createRequest($this, $destination, is_array($args) ? $args : array_slice(func_get_args(), 1), 'test');
321: }
322: return $this->getPresenter()->getLastCreatedRequestFlag('current');
323: }
324:
325:
326: 327: 328: 329: 330: 331: 332: 333:
334: public function redirect($code, $destination = NULL, $args = array())
335: {
336: if (!is_numeric($code)) {
337: $args = is_array($destination) ? $destination : array_slice(func_get_args(), 1);
338: $destination = $code;
339: $code = NULL;
340:
341: } elseif (!is_array($args)) {
342: $args = array_slice(func_get_args(), 2);
343: }
344:
345: $presenter = $this->getPresenter();
346: $presenter->redirectUrl($presenter->createRequest($this, $destination, $args, 'redirect'), $code);
347: }
348:
349:
350:
351:
352:
353: 354: 355: 356: 357: 358:
359: public function offsetSet($name, $component)
360: {
361: $this->addComponent($component, $name);
362: }
363:
364:
365: 366: 367: 368: 369: 370:
371: public function offsetGet($name)
372: {
373: return $this->getComponent($name, TRUE);
374: }
375:
376:
377: 378: 379: 380: 381:
382: public function offsetExists($name)
383: {
384: return $this->getComponent($name, FALSE) !== NULL;
385: }
386:
387:
388: 389: 390: 391: 392:
393: public function offsetUnset($name)
394: {
395: $component = $this->getComponent($name, FALSE);
396: if ($component !== NULL) {
397: $this->removeComponent($component);
398: }
399: }
400:
401: }
402: