Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • Control
  • Form
  • Multiplier
  • Presenter
  • PresenterComponent

Interfaces

  • IRenderable
  • ISignalReceiver
  • IStatePersistent

Exceptions

  • BadSignalException
  • InvalidLinkException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
   1: <?php
   2: 
   3: /**
   4:  * This file is part of the Nette Framework (http://nette.org)
   5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
   6:  */
   7: 
   8: namespace Nette\Application\UI;
   9: 
  10: use Nette,
  11:     Nette\Application,
  12:     Nette\Application\Responses,
  13:     Nette\Http,
  14:     Nette\Reflection;
  15: 
  16: 
  17: /**
  18:  * Presenter component represents a webpage instance. It converts Request to IResponse.
  19:  *
  20:  * @author     David Grudl
  21:  *
  22:  * @property-read Nette\Application\Request $request
  23:  * @property-read array|NULL $signal
  24:  * @property-read string $action
  25:  * @property      string $view
  26:  * @property      string $layout
  27:  * @property-read \stdClass $payload
  28:  * @property-read bool $ajax
  29:  * @property-read Nette\Application\Request $lastCreatedRequest
  30:  * @property-read Nette\Http\SessionSection $flashSession
  31:  * @property-read \SystemContainer|Nette\DI\Container $context
  32:  * @property-read Nette\Application\Application $application
  33:  * @property-read Nette\Http\Session $session
  34:  * @property-read Nette\Security\User $user
  35:  */
  36: abstract class Presenter extends Control implements Application\IPresenter
  37: {
  38:     /** bad link handling {@link Presenter::$invalidLinkMode} */
  39:     const INVALID_LINK_SILENT = 1,
  40:         INVALID_LINK_WARNING = 2,
  41:         INVALID_LINK_EXCEPTION = 3;
  42: 
  43:     /** @internal special parameter key */
  44:     const SIGNAL_KEY = 'do',
  45:         ACTION_KEY = 'action',
  46:         FLASH_KEY = '_fid',
  47:         DEFAULT_ACTION = 'default';
  48: 
  49:     /** @var int */
  50:     public $invalidLinkMode;
  51: 
  52:     /** @var array of function(Presenter $sender, IResponse $response = NULL); Occurs when the presenter is shutting down */
  53:     public $onShutdown;
  54: 
  55:     /** @var Nette\Application\Request */
  56:     private $request;
  57: 
  58:     /** @var Nette\Application\IResponse */
  59:     private $response;
  60: 
  61:     /** @var bool  automatically call canonicalize() */
  62:     public $autoCanonicalize = TRUE;
  63: 
  64:     /** @var bool  use absolute Urls or paths? */
  65:     public $absoluteUrls = FALSE;
  66: 
  67:     /** @var array */
  68:     private $globalParams;
  69: 
  70:     /** @var array */
  71:     private $globalState;
  72: 
  73:     /** @var array */
  74:     private $globalStateSinces;
  75: 
  76:     /** @var string */
  77:     private $action;
  78: 
  79:     /** @var string */
  80:     private $view;
  81: 
  82:     /** @var string */
  83:     private $layout;
  84: 
  85:     /** @var \stdClass */
  86:     private $payload;
  87: 
  88:     /** @var string */
  89:     private $signalReceiver;
  90: 
  91:     /** @var string */
  92:     private $signal;
  93: 
  94:     /** @var bool */
  95:     private $ajaxMode;
  96: 
  97:     /** @var bool */
  98:     private $startupCheck;
  99: 
 100:     /** @var Nette\Application\Request */
 101:     private $lastCreatedRequest;
 102: 
 103:     /** @var array */
 104:     private $lastCreatedRequestFlag;
 105: 
 106:     /** @var \SystemContainer|Nette\DI\Container */
 107:     private $context;
 108: 
 109: 
 110:     public function __construct(Nette\DI\Container $context = NULL)
 111:     {
 112:         $this->context = $context;
 113:         if ($context && $this->invalidLinkMode === NULL) {
 114:             $this->invalidLinkMode = $context->parameters['productionMode'] ? self::INVALID_LINK_SILENT : self::INVALID_LINK_WARNING;
 115:         }
 116:     }
 117: 
 118: 
 119:     /**
 120:      * @return Nette\Application\Request
 121:      */
 122:     public function getRequest()
 123:     {
 124:         return $this->request;
 125:     }
 126: 
 127: 
 128:     /**
 129:      * Returns self.
 130:      * @return Presenter
 131:      */
 132:     public function getPresenter($need = TRUE)
 133:     {
 134:         return $this;
 135:     }
 136: 
 137: 
 138:     /**
 139:      * Returns a name that uniquely identifies component.
 140:      * @return string
 141:      */
 142:     public function getUniqueId()
 143:     {
 144:         return '';
 145:     }
 146: 
 147: 
 148:     /********************* interface IPresenter ****************d*g**/
 149: 
 150: 
 151:     /**
 152:      * @return Nette\Application\IResponse
 153:      */
 154:     public function run(Application\Request $request)
 155:     {
 156:         try {
 157:             // STARTUP
 158:             $this->request = $request;
 159:             $this->payload = new \stdClass;
 160:             $this->setParent($this->getParent(), $request->getPresenterName());
 161: 
 162:             if (!$this->getHttpResponse()->isSent()) {
 163:                 $this->getHttpResponse()->addHeader('Vary', 'X-Requested-With');
 164:             }
 165: 
 166:             $this->initGlobalParameters();
 167:             $this->checkRequirements($this->getReflection());
 168:             $this->startup();
 169:             if (!$this->startupCheck) {
 170:                 $class = $this->getReflection()->getMethod('startup')->getDeclaringClass()->getName();
 171:                 throw new Nette\InvalidStateException("Method $class::startup() or its descendant doesn't call parent::startup().");
 172:             }
 173:             // calls $this->action<Action>()
 174:             $this->tryCall($this->formatActionMethod($this->getAction()), $this->params);
 175: 
 176:             // autoload components
 177:             foreach ($this->globalParams as $id => $foo) {
 178:                 $this->getComponent($id, FALSE);
 179:             }
 180: 
 181:             if ($this->autoCanonicalize) {
 182:                 $this->canonicalize();
 183:             }
 184:             if ($this->getHttpRequest()->isMethod('head')) {
 185:                 $this->terminate();
 186:             }
 187: 
 188:             // SIGNAL HANDLING
 189:             // calls $this->handle<Signal>()
 190:             $this->processSignal();
 191: 
 192:             // RENDERING VIEW
 193:             $this->beforeRender();
 194:             // calls $this->render<View>()
 195:             $this->tryCall($this->formatRenderMethod($this->getView()), $this->params);
 196:             $this->afterRender();
 197: 
 198:             // save component tree persistent state
 199:             $this->saveGlobalState();
 200:             if ($this->isAjax()) {
 201:                 $this->payload->state = $this->getGlobalState();
 202:             }
 203: 
 204:             // finish template rendering
 205:             $this->sendTemplate();
 206: 
 207:         } catch (Application\AbortException $e) {
 208:             // continue with shutting down
 209:             if ($this->isAjax()) try {
 210:                 $hasPayload = (array) $this->payload; unset($hasPayload['state']);
 211:                 if ($this->response instanceof Responses\TextResponse && $this->isControlInvalid()) { // snippets - TODO
 212:                     $this->snippetMode = TRUE;
 213:                     $this->response->send($this->getHttpRequest(), $this->getHttpResponse());
 214:                     $this->sendPayload();
 215: 
 216:                 } elseif (!$this->response && $hasPayload) { // back compatibility for use terminate() instead of sendPayload()
 217:                     $this->sendPayload();
 218:                 }
 219:             } catch (Application\AbortException $e) { }
 220: 
 221:             if ($this->hasFlashSession()) {
 222:                 $this->getFlashSession()->setExpiration($this->response instanceof Responses\RedirectResponse ? '+ 30 seconds' : '+ 3 seconds');
 223:             }
 224: 
 225:             // SHUTDOWN
 226:             $this->onShutdown($this, $this->response);
 227:             $this->shutdown($this->response);
 228: 
 229:             return $this->response;
 230:         }
 231:     }
 232: 
 233: 
 234:     /**
 235:      * @return void
 236:      */
 237:     protected function startup()
 238:     {
 239:         $this->startupCheck = TRUE;
 240:     }
 241: 
 242: 
 243:     /**
 244:      * Common render method.
 245:      * @return void
 246:      */
 247:     protected function beforeRender()
 248:     {
 249:     }
 250: 
 251: 
 252:     /**
 253:      * Common render method.
 254:      * @return void
 255:      */
 256:     protected function afterRender()
 257:     {
 258:     }
 259: 
 260: 
 261:     /**
 262:      * @param  Nette\Application\IResponse
 263:      * @return void
 264:      */
 265:     protected function shutdown($response)
 266:     {
 267:     }
 268: 
 269: 
 270:     /**
 271:      * Checks authorization.
 272:      * @return void
 273:      */
 274:     public function checkRequirements($element)
 275:     {
 276:         $user = (array) $element->getAnnotation('User');
 277:         if (in_array('loggedIn', $user) && !$this->getUser()->isLoggedIn()) {
 278:             throw new Application\ForbiddenRequestException;
 279:         }
 280:     }
 281: 
 282: 
 283:     /********************* signal handling ****************d*g**/
 284: 
 285: 
 286:     /**
 287:      * @return void
 288:      * @throws BadSignalException
 289:      */
 290:     public function processSignal()
 291:     {
 292:         if ($this->signal === NULL) {
 293:             return;
 294:         }
 295: 
 296:         try {
 297:             $component = $this->signalReceiver === '' ? $this : $this->getComponent($this->signalReceiver, FALSE);
 298:         } catch (Nette\InvalidArgumentException $e) {}
 299: 
 300:         if (isset($e) || $component === NULL) {
 301:             throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not found.", NULL, isset($e) ? $e : NULL);
 302: 
 303:         } elseif (!$component instanceof ISignalReceiver) {
 304:             throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not ISignalReceiver implementor.");
 305:         }
 306: 
 307:         $component->signalReceived($this->signal);
 308:         $this->signal = NULL;
 309:     }
 310: 
 311: 
 312:     /**
 313:      * Returns pair signal receiver and name.
 314:      * @return array|NULL
 315:      */
 316:     public function getSignal()
 317:     {
 318:         return $this->signal === NULL ? NULL : array($this->signalReceiver, $this->signal);
 319:     }
 320: 
 321: 
 322:     /**
 323:      * Checks if the signal receiver is the given one.
 324:      * @param  mixed  component or its id
 325:      * @param  string signal name (optional)
 326:      * @return bool
 327:      */
 328:     public function isSignalReceiver($component, $signal = NULL)
 329:     {
 330:         if ($component instanceof Nette\ComponentModel\Component) {
 331:             $component = $component === $this ? '' : $component->lookupPath(__CLASS__, TRUE);
 332:         }
 333: 
 334:         if ($this->signal === NULL) {
 335:             return FALSE;
 336: 
 337:         } elseif ($signal === TRUE) {
 338:             return $component === ''
 339:                 || strncmp($this->signalReceiver . '-', $component . '-', strlen($component) + 1) === 0;
 340: 
 341:         } elseif ($signal === NULL) {
 342:             return $this->signalReceiver === $component;
 343: 
 344:         } else {
 345:             return $this->signalReceiver === $component && strcasecmp($signal, $this->signal) === 0;
 346:         }
 347:     }
 348: 
 349: 
 350:     /********************* rendering ****************d*g**/
 351: 
 352: 
 353:     /**
 354:      * Returns current action name.
 355:      * @return string
 356:      */
 357:     public function getAction($fullyQualified = FALSE)
 358:     {
 359:         return $fullyQualified ? ':' . $this->getName() . ':' . $this->action : $this->action;
 360:     }
 361: 
 362: 
 363:     /**
 364:      * Changes current action. Only alphanumeric characters are allowed.
 365:      * @param  string
 366:      * @return void
 367:      */
 368:     public function changeAction($action)
 369:     {
 370:         if (is_string($action) && Nette\Utils\Strings::match($action, '#^[a-zA-Z0-9][a-zA-Z0-9_\x7f-\xff]*\z#')) {
 371:             $this->action = $action;
 372:             $this->view = $action;
 373: 
 374:         } else {
 375:             $this->error('Action name is not alphanumeric string.');
 376:         }
 377:     }
 378: 
 379: 
 380:     /**
 381:      * Returns current view.
 382:      * @return string
 383:      */
 384:     public function getView()
 385:     {
 386:         return $this->view;
 387:     }
 388: 
 389: 
 390:     /**
 391:      * Changes current view. Any name is allowed.
 392:      * @param  string
 393:      * @return self
 394:      */
 395:     public function setView($view)
 396:     {
 397:         $this->view = (string) $view;
 398:         return $this;
 399:     }
 400: 
 401: 
 402:     /**
 403:      * Returns current layout name.
 404:      * @return string|FALSE
 405:      */
 406:     public function getLayout()
 407:     {
 408:         return $this->layout;
 409:     }
 410: 
 411: 
 412:     /**
 413:      * Changes or disables layout.
 414:      * @param  string|FALSE
 415:      * @return self
 416:      */
 417:     public function setLayout($layout)
 418:     {
 419:         $this->layout = $layout === FALSE ? FALSE : (string) $layout;
 420:         return $this;
 421:     }
 422: 
 423: 
 424:     /**
 425:      * @return void
 426:      * @throws Nette\Application\BadRequestException if no template found
 427:      * @throws Nette\Application\AbortException
 428:      */
 429:     public function sendTemplate()
 430:     {
 431:         $template = $this->getTemplate();
 432:         if (!$template) {
 433:             return;
 434:         }
 435: 
 436:         if ($template instanceof Nette\Templating\IFileTemplate && !$template->getFile()) { // content template
 437:             $files = $this->formatTemplateFiles();
 438:             foreach ($files as $file) {
 439:                 if (is_file($file)) {
 440:                     $template->setFile($file);
 441:                     break;
 442:                 }
 443:             }
 444: 
 445:             if (!$template->getFile()) {
 446:                 $file = preg_replace('#^.*([/\\\\].{1,70})\z#U', "\xE2\x80\xA6\$1", reset($files));
 447:                 $file = strtr($file, '/', DIRECTORY_SEPARATOR);
 448:                 $this->error("Page not found. Missing template '$file'.");
 449:             }
 450:         }
 451: 
 452:         $this->sendResponse(new Responses\TextResponse($template));
 453:     }
 454: 
 455: 
 456:     /**
 457:      * Finds layout template file name.
 458:      * @return string
 459:      */
 460:     public function findLayoutTemplateFile()
 461:     {
 462:         if ($this->layout === FALSE) {
 463:             return;
 464:         }
 465:         $files = $this->formatLayoutTemplateFiles();
 466:         foreach ($files as $file) {
 467:             if (is_file($file)) {
 468:                 return $file;
 469:             }
 470:         }
 471: 
 472:         if ($this->layout) {
 473:             $file = preg_replace('#^.*([/\\\\].{1,70})\z#U', "\xE2\x80\xA6\$1", reset($files));
 474:             $file = strtr($file, '/', DIRECTORY_SEPARATOR);
 475:             throw new Nette\FileNotFoundException("Layout not found. Missing template '$file'.");
 476:         }
 477:     }
 478: 
 479: 
 480:     /**
 481:      * Formats layout template file names.
 482:      * @return array
 483:      */
 484:     public function formatLayoutTemplateFiles()
 485:     {
 486:         $name = $this->getName();
 487:         $presenter = substr($name, strrpos(':' . $name, ':'));
 488:         $layout = $this->layout ? $this->layout : 'layout';
 489:         $dir = dirname($this->getReflection()->getFileName());
 490:         $dir = is_dir("$dir/templates") ? $dir : dirname($dir);
 491:         $list = array(
 492:             "$dir/templates/$presenter/@$layout.latte",
 493:             "$dir/templates/$presenter.@$layout.latte",
 494:             "$dir/templates/$presenter/@$layout.phtml",
 495:             "$dir/templates/$presenter.@$layout.phtml",
 496:         );
 497:         do {
 498:             $list[] = "$dir/templates/@$layout.latte";
 499:             $list[] = "$dir/templates/@$layout.phtml";
 500:             $dir = dirname($dir);
 501:         } while ($dir && ($name = substr($name, 0, strrpos($name, ':'))));
 502:         return $list;
 503:     }
 504: 
 505: 
 506:     /**
 507:      * Formats view template file names.
 508:      * @return array
 509:      */
 510:     public function formatTemplateFiles()
 511:     {
 512:         $name = $this->getName();
 513:         $presenter = substr($name, strrpos(':' . $name, ':'));
 514:         $dir = dirname($this->getReflection()->getFileName());
 515:         $dir = is_dir("$dir/templates") ? $dir : dirname($dir);
 516:         return array(
 517:             "$dir/templates/$presenter/$this->view.latte",
 518:             "$dir/templates/$presenter.$this->view.latte",
 519:             "$dir/templates/$presenter/$this->view.phtml",
 520:             "$dir/templates/$presenter.$this->view.phtml",
 521:         );
 522:     }
 523: 
 524: 
 525:     /**
 526:      * Formats action method name.
 527:      * @param  string
 528:      * @return string
 529:      */
 530:     protected static function formatActionMethod($action)
 531:     {
 532:         return 'action' . $action;
 533:     }
 534: 
 535: 
 536:     /**
 537:      * Formats render view method name.
 538:      * @param  string
 539:      * @return string
 540:      */
 541:     protected static function formatRenderMethod($view)
 542:     {
 543:         return 'render' . $view;
 544:     }
 545: 
 546: 
 547:     /********************* partial AJAX rendering ****************d*g**/
 548: 
 549: 
 550:     /**
 551:      * @return \stdClass
 552:      */
 553:     public function getPayload()
 554:     {
 555:         return $this->payload;
 556:     }
 557: 
 558: 
 559:     /**
 560:      * Is AJAX request?
 561:      * @return bool
 562:      */
 563:     public function isAjax()
 564:     {
 565:         if ($this->ajaxMode === NULL) {
 566:             $this->ajaxMode = $this->getHttpRequest()->isAjax();
 567:         }
 568:         return $this->ajaxMode;
 569:     }
 570: 
 571: 
 572:     /**
 573:      * Sends AJAX payload to the output.
 574:      * @return void
 575:      * @throws Nette\Application\AbortException
 576:      */
 577:     public function sendPayload()
 578:     {
 579:         $this->sendResponse(new Responses\JsonResponse($this->payload));
 580:     }
 581: 
 582: 
 583:     /********************* navigation & flow ****************d*g**/
 584: 
 585: 
 586:     /**
 587:      * Sends response and terminates presenter.
 588:      * @return void
 589:      * @throws Nette\Application\AbortException
 590:      */
 591:     public function sendResponse(Application\IResponse $response)
 592:     {
 593:         $this->response = $response;
 594:         $this->terminate();
 595:     }
 596: 
 597: 
 598:     /**
 599:      * Correctly terminates presenter.
 600:      * @return void
 601:      * @throws Nette\Application\AbortException
 602:      */
 603:     public function terminate()
 604:     {
 605:         if (func_num_args() !== 0) {
 606:             trigger_error(__METHOD__ . ' is not intended to send a Application\Response; use sendResponse() instead.', E_USER_WARNING);
 607:             $this->sendResponse(func_get_arg(0));
 608:         }
 609:         throw new Application\AbortException();
 610:     }
 611: 
 612: 
 613:     /**
 614:      * Forward to another presenter or action.
 615:      * @param  string|Request
 616:      * @param  array|mixed
 617:      * @return void
 618:      * @throws Nette\Application\AbortException
 619:      */
 620:     public function forward($destination, $args = array())
 621:     {
 622:         if ($destination instanceof Application\Request) {
 623:             $this->sendResponse(new Responses\ForwardResponse($destination));
 624:         }
 625: 
 626:         $this->createRequest($this, $destination, is_array($args) ? $args : array_slice(func_get_args(), 1), 'forward');
 627:         $this->sendResponse(new Responses\ForwardResponse($this->lastCreatedRequest));
 628:     }
 629: 
 630: 
 631:     /**
 632:      * Redirect to another URL and ends presenter execution.
 633:      * @param  string
 634:      * @param  int HTTP error code
 635:      * @return void
 636:      * @throws Nette\Application\AbortException
 637:      */
 638:     public function redirectUrl($url, $code = NULL)
 639:     {
 640:         if ($this->isAjax()) {
 641:             $this->payload->redirect = (string) $url;
 642:             $this->sendPayload();
 643: 
 644:         } elseif (!$code) {
 645:             $code = $this->getHttpRequest()->isMethod('post')
 646:                 ? Http\IResponse::S303_POST_GET
 647:                 : Http\IResponse::S302_FOUND;
 648:         }
 649:         $this->sendResponse(new Responses\RedirectResponse($url, $code));
 650:     }
 651: 
 652:     /** @deprecated */
 653:     function redirectUri($url, $code = NULL)
 654:     {
 655:         trigger_error(__METHOD__ . '() is deprecated; use ' . __CLASS__ . '::redirectUrl() instead.', E_USER_WARNING);
 656:         $this->redirectUrl($url, $code);
 657:     }
 658: 
 659: 
 660:     /**
 661:      * Throws HTTP error.
 662:      * @param  string
 663:      * @param  int HTTP error code
 664:      * @return void
 665:      * @throws Nette\Application\BadRequestException
 666:      */
 667:     public function error($message = NULL, $code = Http\IResponse::S404_NOT_FOUND)
 668:     {
 669:         throw new Application\BadRequestException($message, $code);
 670:     }
 671: 
 672: 
 673:     /**
 674:      * Link to myself.
 675:      * @return string
 676:      */
 677:     public function backlink()
 678:     {
 679:         return $this->getAction(TRUE);
 680:     }
 681: 
 682: 
 683:     /**
 684:      * Returns the last created Request.
 685:      * @return Nette\Application\Request
 686:      */
 687:     public function getLastCreatedRequest()
 688:     {
 689:         return $this->lastCreatedRequest;
 690:     }
 691: 
 692: 
 693:     /**
 694:      * Returns the last created Request flag.
 695:      * @param  string
 696:      * @return bool
 697:      */
 698:     public function getLastCreatedRequestFlag($flag)
 699:     {
 700:         return !empty($this->lastCreatedRequestFlag[$flag]);
 701:     }
 702: 
 703: 
 704:     /**
 705:      * Conditional redirect to canonicalized URI.
 706:      * @return void
 707:      * @throws Nette\Application\AbortException
 708:      */
 709:     public function canonicalize()
 710:     {
 711:         if (!$this->isAjax() && ($this->request->isMethod('get') || $this->request->isMethod('head'))) {
 712:             try {
 713:                 $url = $this->createRequest($this, $this->action, $this->getGlobalState() + $this->request->getParameters(), 'redirectX');
 714:             } catch (InvalidLinkException $e) {}
 715:             if (isset($url) && !$this->getHttpRequest()->getUrl()->isEqual($url)) {
 716:                 $this->sendResponse(new Responses\RedirectResponse($url, Http\IResponse::S301_MOVED_PERMANENTLY));
 717:             }
 718:         }
 719:     }
 720: 
 721: 
 722:     /**
 723:      * Attempts to cache the sent entity by its last modification date.
 724:      * @param  string|int|DateTime  last modified time
 725:      * @param  string strong entity tag validator
 726:      * @param  mixed  optional expiration time
 727:      * @return void
 728:      * @throws Nette\Application\AbortException
 729:      * @deprecated
 730:      */
 731:     public function lastModified($lastModified, $etag = NULL, $expire = NULL)
 732:     {
 733:         if ($expire !== NULL) {
 734:             $this->getHttpResponse()->setExpiration($expire);
 735:         }
 736: 
 737:         if (!$this->getHttpContext()->isModified($lastModified, $etag)) {
 738:             $this->terminate();
 739:         }
 740:     }
 741: 
 742: 
 743:     /**
 744:      * Request/URL factory.
 745:      * @param  PresenterComponent  base
 746:      * @param  string   destination in format "[[module:]presenter:]action" or "signal!" or "this"
 747:      * @param  array    array of arguments
 748:      * @param  string   forward|redirect|link
 749:      * @return string   URL
 750:      * @throws InvalidLinkException
 751:      * @internal
 752:      */
 753:     protected function createRequest($component, $destination, array $args, $mode)
 754:     {
 755:         // note: createRequest supposes that saveState(), run() & tryCall() behaviour is final
 756: 
 757:         // cached services for better performance
 758:         static $presenterFactory, $router, $refUrl;
 759:         if ($presenterFactory === NULL) {
 760:             $presenterFactory = $this->getApplication()->getPresenterFactory();
 761:             $router = $this->getApplication()->getRouter();
 762:             $refUrl = new Http\Url($this->getHttpRequest()->getUrl());
 763:             $refUrl->setPath($this->getHttpRequest()->getUrl()->getScriptPath());
 764:         }
 765: 
 766:         $this->lastCreatedRequest = $this->lastCreatedRequestFlag = NULL;
 767: 
 768:         // PARSE DESTINATION
 769:         // 1) fragment
 770:         $a = strpos($destination, '#');
 771:         if ($a === FALSE) {
 772:             $fragment = '';
 773:         } else {
 774:             $fragment = substr($destination, $a);
 775:             $destination = substr($destination, 0, $a);
 776:         }
 777: 
 778:         // 2) ?query syntax
 779:         $a = strpos($destination, '?');
 780:         if ($a !== FALSE) {
 781:             parse_str(substr($destination, $a + 1), $args); // requires disabled magic quotes
 782:             $destination = substr($destination, 0, $a);
 783:         }
 784: 
 785:         // 3) URL scheme
 786:         $a = strpos($destination, '//');
 787:         if ($a === FALSE) {
 788:             $scheme = FALSE;
 789:         } else {
 790:             $scheme = substr($destination, 0, $a);
 791:             $destination = substr($destination, $a + 2);
 792:         }
 793: 
 794:         // 4) signal or empty
 795:         if (!$component instanceof Presenter || substr($destination, -1) === '!') {
 796:             $signal = rtrim($destination, '!');
 797:             $a = strrpos($signal, ':');
 798:             if ($a !== FALSE) {
 799:                 $component = $component->getComponent(strtr(substr($signal, 0, $a), ':', '-'));
 800:                 $signal = (string) substr($signal, $a + 1);
 801:             }
 802:             if ($signal == NULL) {  // intentionally ==
 803:                 throw new InvalidLinkException("Signal must be non-empty string.");
 804:             }
 805:             $destination = 'this';
 806:         }
 807: 
 808:         if ($destination == NULL) {  // intentionally ==
 809:             throw new InvalidLinkException("Destination must be non-empty string.");
 810:         }
 811: 
 812:         // 5) presenter: action
 813:         $current = FALSE;
 814:         $a = strrpos($destination, ':');
 815:         if ($a === FALSE) {
 816:             $action = $destination === 'this' ? $this->action : $destination;
 817:             $presenter = $this->getName();
 818:             $presenterClass = get_class($this);
 819: 
 820:         } else {
 821:             $action = (string) substr($destination, $a + 1);
 822:             if ($destination[0] === ':') { // absolute
 823:                 if ($a < 2) {
 824:                     throw new InvalidLinkException("Missing presenter name in '$destination'.");
 825:                 }
 826:                 $presenter = substr($destination, 1, $a - 1);
 827: 
 828:             } else { // relative
 829:                 $presenter = $this->getName();
 830:                 $b = strrpos($presenter, ':');
 831:                 if ($b === FALSE) { // no module
 832:                     $presenter = substr($destination, 0, $a);
 833:                 } else { // with module
 834:                     $presenter = substr($presenter, 0, $b + 1) . substr($destination, 0, $a);
 835:                 }
 836:             }
 837:             try {
 838:                 $presenterClass = $presenterFactory->getPresenterClass($presenter);
 839:             } catch (Application\InvalidPresenterException $e) {
 840:                 throw new InvalidLinkException($e->getMessage(), NULL, $e);
 841:             }
 842:         }
 843: 
 844:         // PROCESS SIGNAL ARGUMENTS
 845:         if (isset($signal)) { // $component must be IStatePersistent
 846:             $reflection = new PresenterComponentReflection(get_class($component));
 847:             if ($signal === 'this') { // means "no signal"
 848:                 $signal = '';
 849:                 if (array_key_exists(0, $args)) {
 850:                     throw new InvalidLinkException("Unable to pass parameters to 'this!' signal.");
 851:                 }
 852: 
 853:             } elseif (strpos($signal, self::NAME_SEPARATOR) === FALSE) { // TODO: AppForm exception
 854:                 // counterpart of signalReceived() & tryCall()
 855:                 $method = $component->formatSignalMethod($signal);
 856:                 if (!$reflection->hasCallableMethod($method)) {
 857:                     throw new InvalidLinkException("Unknown signal '$signal', missing handler {$reflection->name}::$method()");
 858:                 }
 859:                 if ($args) { // convert indexed parameters to named
 860:                     self::argsToParams(get_class($component), $method, $args);
 861:                 }
 862:             }
 863: 
 864:             // counterpart of IStatePersistent
 865:             if ($args && array_intersect_key($args, $reflection->getPersistentParams())) {
 866:                 $component->saveState($args);
 867:             }
 868: 
 869:             if ($args && $component !== $this) {
 870:                 $prefix = $component->getUniqueId() . self::NAME_SEPARATOR;
 871:                 foreach ($args as $key => $val) {
 872:                     unset($args[$key]);
 873:                     $args[$prefix . $key] = $val;
 874:                 }
 875:             }
 876:         }
 877: 
 878:         // PROCESS ARGUMENTS
 879:         if (is_subclass_of($presenterClass, __CLASS__)) {
 880:             if ($action === '') {
 881:                 $action = self::DEFAULT_ACTION;
 882:             }
 883: 
 884:             $current = ($action === '*' || strcasecmp($action, $this->action) === 0) && $presenterClass === get_class($this); // TODO
 885: 
 886:             $reflection = new PresenterComponentReflection($presenterClass);
 887:             if ($args || $destination === 'this') {
 888:                 // counterpart of run() & tryCall()
 889:                 $method = $presenterClass::formatActionMethod($action);
 890:                 if (!$reflection->hasCallableMethod($method)) {
 891:                     $method = $presenterClass::formatRenderMethod($action);
 892:                     if (!$reflection->hasCallableMethod($method)) {
 893:                         $method = NULL;
 894:                     }
 895:                 }
 896: 
 897:                 // convert indexed parameters to named
 898:                 if ($method === NULL) {
 899:                     if (array_key_exists(0, $args)) {
 900:                         throw new InvalidLinkException("Unable to pass parameters to action '$presenter:$action', missing corresponding method.");
 901:                     }
 902: 
 903:                 } elseif ($destination === 'this') {
 904:                     self::argsToParams($presenterClass, $method, $args, $this->params);
 905: 
 906:                 } else {
 907:                     self::argsToParams($presenterClass, $method, $args);
 908:                 }
 909:             }
 910: 
 911:             // counterpart of IStatePersistent
 912:             if ($args && array_intersect_key($args, $reflection->getPersistentParams())) {
 913:                 $this->saveState($args, $reflection);
 914:             }
 915: 
 916:             if ($mode === 'redirect') {
 917:                 $this->saveGlobalState();
 918:             }
 919: 
 920:             $globalState = $this->getGlobalState($destination === 'this' ? NULL : $presenterClass);
 921:             if ($current && $args) {
 922:                 $tmp = $globalState + $this->params;
 923:                 foreach ($args as $key => $val) {
 924:                     if (http_build_query(array($val)) !== (isset($tmp[$key]) ? http_build_query(array($tmp[$key])) : '')) {
 925:                         $current = FALSE;
 926:                         break;
 927:                     }
 928:                 }
 929:             }
 930:             $args += $globalState;
 931:         }
 932: 
 933:         // ADD ACTION & SIGNAL & FLASH
 934:         if ($action) {
 935:             $args[self::ACTION_KEY] = $action;
 936:         }
 937:         if (!empty($signal)) {
 938:             $args[self::SIGNAL_KEY] = $component->getParameterId($signal);
 939:             $current = $current && $args[self::SIGNAL_KEY] === $this->getParameter(self::SIGNAL_KEY);
 940:         }
 941:         if (($mode === 'redirect' || $mode === 'forward') && $this->hasFlashSession()) {
 942:             $args[self::FLASH_KEY] = $this->getParameter(self::FLASH_KEY);
 943:         }
 944: 
 945:         $this->lastCreatedRequest = new Application\Request(
 946:             $presenter,
 947:             Application\Request::FORWARD,
 948:             $args,
 949:             array(),
 950:             array()
 951:         );
 952:         $this->lastCreatedRequestFlag = array('current' => $current);
 953: 
 954:         if ($mode === 'forward' || $mode === 'test') {
 955:             return;
 956:         }
 957: 
 958:         // CONSTRUCT URL
 959:         $url = $router->constructUrl($this->lastCreatedRequest, $refUrl);
 960:         if ($url === NULL) {
 961:             unset($args[self::ACTION_KEY]);
 962:             $params = urldecode(http_build_query($args, NULL, ', '));
 963:             throw new InvalidLinkException("No route for $presenter:$action($params)");
 964:         }
 965: 
 966:         // make URL relative if possible
 967:         if ($mode === 'link' && $scheme === FALSE && !$this->absoluteUrls) {
 968:             $hostUrl = $refUrl->getHostUrl() . '/';
 969:             if (strncmp($url, $hostUrl, strlen($hostUrl)) === 0) {
 970:                 $url = substr($url, strlen($hostUrl) - 1);
 971:             }
 972:         }
 973: 
 974:         return $url . $fragment;
 975:     }
 976: 
 977: 
 978:     /**
 979:      * Converts list of arguments to named parameters.
 980:      * @param  string  class name
 981:      * @param  string  method name
 982:      * @param  array   arguments
 983:      * @param  array   supplemental arguments
 984:      * @return void
 985:      * @throws InvalidLinkException
 986:      */
 987:     private static function argsToParams($class, $method, & $args, $supplemental = array())
 988:     {
 989:         $i = 0;
 990:         $rm = new \ReflectionMethod($class, $method);
 991:         foreach ($rm->getParameters() as $param) {
 992:             $name = $param->getName();
 993:             if (array_key_exists($i, $args)) {
 994:                 $args[$name] = $args[$i];
 995:                 unset($args[$i]);
 996:                 $i++;
 997: 
 998:             } elseif (array_key_exists($name, $args)) {
 999:                 // continue with process
1000: 
1001:             } elseif (array_key_exists($name, $supplemental)) {
1002:                 $args[$name] = $supplemental[$name];
1003: 
1004:             } else {
1005:                 continue;
1006:             }
1007: 
1008:             if ($args[$name] === NULL) {
1009:                 continue;
1010:             }
1011: 
1012:             $def = $param->isDefaultValueAvailable() && $param->isOptional() ? $param->getDefaultValue() : NULL; // see PHP bug #62988
1013:             $type = $param->isArray() ? 'array' : gettype($def);
1014:             if (!PresenterComponentReflection::convertType($args[$name], $type)) {
1015:                 throw new InvalidLinkException("Invalid value for parameter '$name' in method $class::$method(), expected " . ($type === 'NULL' ? 'scalar' : $type) . ".");
1016:             }
1017: 
1018:             if ($args[$name] === $def || ($def === NULL && is_scalar($args[$name]) && (string) $args[$name] === '')) {
1019:                 $args[$name] = NULL; // value transmit is unnecessary
1020:             }
1021:         }
1022: 
1023:         if (array_key_exists($i, $args)) {
1024:             $method = $rm->getName();
1025:             throw new InvalidLinkException("Passed more parameters than method $class::$method() expects.");
1026:         }
1027:     }
1028: 
1029: 
1030:     /**
1031:      * Invalid link handler. Descendant can override this method to change default behaviour.
1032:      * @return string
1033:      * @throws InvalidLinkException
1034:      */
1035:     protected function handleInvalidLink(InvalidLinkException $e)
1036:     {
1037:         if ($this->invalidLinkMode === self::INVALID_LINK_SILENT) {
1038:             return '#';
1039: 
1040:         } elseif ($this->invalidLinkMode === self::INVALID_LINK_WARNING) {
1041:             return 'error: ' . $e->getMessage();
1042: 
1043:         } else { // self::INVALID_LINK_EXCEPTION
1044:             throw $e;
1045:         }
1046:     }
1047: 
1048: 
1049:     /********************* request serialization ****************d*g**/
1050: 
1051: 
1052:     /**
1053:      * Stores current request to session.
1054:      * @param  mixed  optional expiration time
1055:      * @return string key
1056:      */
1057:     public function storeRequest($expiration = '+ 10 minutes')
1058:     {
1059:         $session = $this->getSession('Nette.Application/requests');
1060:         do {
1061:             $key = Nette\Utils\Strings::random(5);
1062:         } while (isset($session[$key]));
1063: 
1064:         $session[$key] = array($this->getUser()->getId(), $this->request);
1065:         $session->setExpiration($expiration, $key);
1066:         return $key;
1067:     }
1068: 
1069: 
1070:     /**
1071:      * Restores request from session.
1072:      * @param  string key
1073:      * @return void
1074:      */
1075:     public function restoreRequest($key)
1076:     {
1077:         $session = $this->getSession('Nette.Application/requests');
1078:         if (!isset($session[$key]) || ($session[$key][0] !== NULL && $session[$key][0] !== $this->getUser()->getId())) {
1079:             return;
1080:         }
1081:         $request = clone $session[$key][1];
1082:         unset($session[$key]);
1083:         $request->setFlag(Application\Request::RESTORED, TRUE);
1084:         $params = $request->getParameters();
1085:         $params[self::FLASH_KEY] = $this->getParameter(self::FLASH_KEY);
1086:         $request->setParameters($params);
1087:         $this->sendResponse(new Responses\ForwardResponse($request));
1088:     }
1089: 
1090: 
1091:     /********************* interface IStatePersistent ****************d*g**/
1092: 
1093: 
1094:     /**
1095:      * Returns array of persistent components.
1096:      * This default implementation detects components by class-level annotation @persistent(cmp1, cmp2).
1097:      * @return array
1098:      */
1099:     public static function getPersistentComponents()
1100:     {
1101:         return (array) Reflection\ClassType::from(get_called_class())
1102:             ->getAnnotation('persistent');
1103:     }
1104: 
1105: 
1106:     /**
1107:      * Saves state information for all subcomponents to $this->globalState.
1108:      * @return array
1109:      */
1110:     private function getGlobalState($forClass = NULL)
1111:     {
1112:         $sinces = & $this->globalStateSinces;
1113: 
1114:         if ($this->globalState === NULL) {
1115:             $state = array();
1116:             foreach ($this->globalParams as $id => $params) {
1117:                 $prefix = $id . self::NAME_SEPARATOR;
1118:                 foreach ($params as $key => $val) {
1119:                     $state[$prefix . $key] = $val;
1120:                 }
1121:             }
1122:             $this->saveState($state, $forClass ? new PresenterComponentReflection($forClass) : NULL);
1123: 
1124:             if ($sinces === NULL) {
1125:                 $sinces = array();
1126:                 foreach ($this->getReflection()->getPersistentParams() as $name => $meta) {
1127:                     $sinces[$name] = $meta['since'];
1128:                 }
1129:             }
1130: 
1131:             $components = $this->getReflection()->getPersistentComponents();
1132:             $iterator = $this->getComponents(TRUE, 'Nette\Application\UI\IStatePersistent');
1133: 
1134:             foreach ($iterator as $name => $component) {
1135:                 if ($iterator->getDepth() === 0) {
1136:                     // counts with Nette\Application\RecursiveIteratorIterator::SELF_FIRST
1137:                     $since = isset($components[$name]['since']) ? $components[$name]['since'] : FALSE; // FALSE = nonpersistent
1138:                 }
1139:                 $prefix = $component->getUniqueId() . self::NAME_SEPARATOR;
1140:                 $params = array();
1141:                 $component->saveState($params);
1142:                 foreach ($params as $key => $val) {
1143:                     $state[$prefix . $key] = $val;
1144:                     $sinces[$prefix . $key] = $since;
1145:                 }
1146:             }
1147: 
1148:         } else {
1149:             $state = $this->globalState;
1150:         }
1151: 
1152:         if ($forClass !== NULL) {
1153:             $since = NULL;
1154:             foreach ($state as $key => $foo) {
1155:                 if (!isset($sinces[$key])) {
1156:                     $x = strpos($key, self::NAME_SEPARATOR);
1157:                     $x = $x === FALSE ? $key : substr($key, 0, $x);
1158:                     $sinces[$key] = isset($sinces[$x]) ? $sinces[$x] : FALSE;
1159:                 }
1160:                 if ($since !== $sinces[$key]) {
1161:                     $since = $sinces[$key];
1162:                     $ok = $since && (is_subclass_of($forClass, $since) || $forClass === $since);
1163:                 }
1164:                 if (!$ok) {
1165:                     unset($state[$key]);
1166:                 }
1167:             }
1168:         }
1169: 
1170:         return $state;
1171:     }
1172: 
1173: 
1174:     /**
1175:      * Permanently saves state information for all subcomponents to $this->globalState.
1176:      * @return void
1177:      */
1178:     protected function saveGlobalState()
1179:     {
1180:         $this->globalParams = array();
1181:         $this->globalState = $this->getGlobalState();
1182:     }
1183: 
1184: 
1185:     /**
1186:      * Initializes $this->globalParams, $this->signal & $this->signalReceiver, $this->action, $this->view. Called by run().
1187:      * @return void
1188:      * @throws Nette\Application\BadRequestException if action name is not valid
1189:      */
1190:     private function initGlobalParameters()
1191:     {
1192:         // init $this->globalParams
1193:         $this->globalParams = array();
1194:         $selfParams = array();
1195: 
1196:         $params = $this->request->getParameters();
1197:         if ($this->isAjax()) {
1198:             $params += $this->request->getPost();
1199:         }
1200: 
1201:         foreach ($params as $key => $value) {
1202:             if (!preg_match('#^((?:[a-z0-9_]+-)*)((?!\d+\z)[a-z0-9_]+)\z#i', $key, $matches)) {
1203:                 continue;
1204:             } elseif (!$matches[1]) {
1205:                 $selfParams[$key] = $value;
1206:             } else {
1207:                 $this->globalParams[substr($matches[1], 0, -1)][$matches[2]] = $value;
1208:             }
1209:         }
1210: 
1211:         // init & validate $this->action & $this->view
1212:         $this->changeAction(isset($selfParams[self::ACTION_KEY]) ? $selfParams[self::ACTION_KEY] : self::DEFAULT_ACTION);
1213: 
1214:         // init $this->signalReceiver and key 'signal' in appropriate params array
1215:         $this->signalReceiver = $this->getUniqueId();
1216:         if (isset($selfParams[self::SIGNAL_KEY])) {
1217:             $param = $selfParams[self::SIGNAL_KEY];
1218:             if (!is_string($param)) {
1219:                 $this->error('Signal name is not string.');
1220:             }
1221:             $pos = strrpos($param, '-');
1222:             if ($pos) {
1223:                 $this->signalReceiver = substr($param, 0, $pos);
1224:                 $this->signal = substr($param, $pos + 1);
1225:             } else {
1226:                 $this->signalReceiver = $this->getUniqueId();
1227:                 $this->signal = $param;
1228:             }
1229:             if ($this->signal == NULL) { // intentionally ==
1230:                 $this->signal = NULL;
1231:             }
1232:         }
1233: 
1234:         $this->loadState($selfParams);
1235:     }
1236: 
1237: 
1238:     /**
1239:      * Pops parameters for specified component.
1240:      * @param  string  component id
1241:      * @return array
1242:      */
1243:     public function popGlobalParameters($id)
1244:     {
1245:         if (isset($this->globalParams[$id])) {
1246:             $res = $this->globalParams[$id];
1247:             unset($this->globalParams[$id]);
1248:             return $res;
1249: 
1250:         } else {
1251:             return array();
1252:         }
1253:     }
1254: 
1255: 
1256:     /********************* flash session ****************d*g**/
1257: 
1258: 
1259:     /**
1260:      * Checks if a flash session namespace exists.
1261:      * @return bool
1262:      */
1263:     public function hasFlashSession()
1264:     {
1265:         return !empty($this->params[self::FLASH_KEY])
1266:             && $this->getSession()->hasSection('Nette.Application.Flash/' . $this->params[self::FLASH_KEY]);
1267:     }
1268: 
1269: 
1270:     /**
1271:      * Returns session namespace provided to pass temporary data between redirects.
1272:      * @return Nette\Http\SessionSection
1273:      */
1274:     public function getFlashSession()
1275:     {
1276:         if (empty($this->params[self::FLASH_KEY])) {
1277:             $this->params[self::FLASH_KEY] = Nette\Utils\Strings::random(4);
1278:         }
1279:         return $this->getSession('Nette.Application.Flash/' . $this->params[self::FLASH_KEY]);
1280:     }
1281: 
1282: 
1283:     /********************* services ****************d*g**/
1284: 
1285: 
1286:     /**
1287:      * @return void
1288:      */
1289:     public function injectPrimary(Nette\DI\Container $context)
1290:     {
1291:         $this->context = $context;
1292:     }
1293: 
1294: 
1295:     /**
1296:      * Gets the context.
1297:      * @return \SystemContainer|Nette\DI\Container
1298:      */
1299:     public function getContext()
1300:     {
1301:         return $this->context;
1302:     }
1303: 
1304: 
1305:     /**
1306:      * @deprecated
1307:      */
1308:     public function getService($name)
1309:     {
1310:         return $this->context->getService($name);
1311:     }
1312: 
1313: 
1314:     /**
1315:      * @return Nette\Http\Request
1316:      */
1317:     protected function getHttpRequest()
1318:     {
1319:         return $this->context->getByType('Nette\Http\IRequest');
1320:     }
1321: 
1322: 
1323:     /**
1324:      * @return Nette\Http\Response
1325:      */
1326:     protected function getHttpResponse()
1327:     {
1328:         return $this->context->getByType('Nette\Http\IResponse');
1329:     }
1330: 
1331: 
1332:     /**
1333:      * @return Nette\Http\Context
1334:      */
1335:     protected function getHttpContext()
1336:     {
1337:         return $this->context->getByType('Nette\Http\Context');
1338:     }
1339: 
1340: 
1341:     /**
1342:      * @return Nette\Application\Application
1343:      */
1344:     public function getApplication()
1345:     {
1346:         return $this->context->getByType('Nette\Application\Application');
1347:     }
1348: 
1349: 
1350:     /**
1351:      * @param  string
1352:      * @return Nette\Http\Session|Nette\Http\SessionSection
1353:      */
1354:     public function getSession($namespace = NULL)
1355:     {
1356:         $handler = $this->context->getByType('Nette\Http\Session');
1357:         return $namespace === NULL ? $handler : $handler->getSection($namespace);
1358:     }
1359: 
1360: 
1361:     /**
1362:      * @return Nette\Security\User
1363:      */
1364:     public function getUser()
1365:     {
1366:         return $this->context->getByType('Nette\Security\User');
1367:     }
1368: 
1369: }
1370: 
Nette Framework 2.0.14 API API documentation generated by ApiGen 2.8.0