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