Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • Application
  • LinkGenerator
  • PresenterFactory
  • Request

Interfaces

  • IPresenter
  • IPresenterFactory
  • IResponse
  • IRouter

Exceptions

  • AbortException
  • ApplicationException
  • BadRequestException
  • ForbiddenRequestException
  • InvalidPresenterException
  • 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;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Default presenter loader.
 15:  *
 16:  * @author     David Grudl
 17:  */
 18: class PresenterFactory extends Nette\Object implements IPresenterFactory
 19: {
 20:     /** @deprecated */
 21:     public $caseSensitive = TRUE;
 22: 
 23:     /** @var array[] of module => splited mask */
 24:     private $mapping = array(
 25:         '*' => array('', '*Module\\', '*Presenter'),
 26:         'Nette' => array('NetteModule\\', '*\\', '*Presenter'),
 27:     );
 28: 
 29:     /** @var array */
 30:     private $cache = array();
 31: 
 32:     /** @var callable */
 33:     private $factory;
 34: 
 35: 
 36:     /**
 37:      * @param  callable  function(string $class): IPresenter
 38:      */
 39:     public function __construct($factory = NULL)
 40:     {
 41:         $this->factory = $factory ?: function($class) { return new $class; };
 42:     }
 43: 
 44: 
 45:     /**
 46:      * Creates new presenter instance.
 47:      * @param  string  presenter name
 48:      * @return IPresenter
 49:      */
 50:     public function createPresenter($name)
 51:     {
 52:         return call_user_func($this->factory, $this->getPresenterClass($name));
 53:     }
 54: 
 55: 
 56:     /**
 57:      * Generates and checks presenter class name.
 58:      * @param  string  presenter name
 59:      * @return string  class name
 60:      * @throws InvalidPresenterException
 61:      */
 62:     public function getPresenterClass(& $name)
 63:     {
 64:         if (isset($this->cache[$name])) {
 65:             return $this->cache[$name];
 66:         }
 67: 
 68:         if (!is_string($name) || !Nette\Utils\Strings::match($name, '#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*\z#')) {
 69:             throw new InvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
 70:         }
 71: 
 72:         $class = $this->formatPresenterClass($name);
 73:         if (!class_exists($class)) {
 74:             throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' was not found.");
 75:         }
 76: 
 77:         $reflection = new \ReflectionClass($class);
 78:         $class = $reflection->getName();
 79: 
 80:         if (!$reflection->implementsInterface('Nette\Application\IPresenter')) {
 81:             throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is not Nette\\Application\\IPresenter implementor.");
 82:         } elseif ($reflection->isAbstract()) {
 83:             throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
 84:         }
 85: 
 86:         $this->cache[$name] = $class;
 87: 
 88:         if ($name !== ($realName = $this->unformatPresenterClass($class))) {
 89:             trigger_error("Case mismatch on presenter name '$name', correct name is '$realName'.", E_USER_WARNING);
 90:             $name = $realName;
 91:         }
 92: 
 93:         return $class;
 94:     }
 95: 
 96: 
 97:     /**
 98:      * Sets mapping as pairs [module => mask]
 99:      * @return self
100:      */
101:     public function setMapping(array $mapping)
102:     {
103:         foreach ($mapping as $module => $mask) {
104:             if (!preg_match('#^\\\\?([\w\\\\]*\\\\)?(\w*\*\w*?\\\\)?([\w\\\\]*\*\w*)\z#', $mask, $m)) {
105:                 throw new Nette\InvalidStateException("Invalid mapping mask '$mask'.");
106:             }
107:             $this->mapping[$module] = array($m[1], $m[2] ?: '*Module\\', $m[3]);
108:         }
109:         return $this;
110:     }
111: 
112: 
113:     /**
114:      * Formats presenter class name from its name.
115:      * @param  string
116:      * @return string
117:      * @internal
118:      */
119:     public function formatPresenterClass($presenter)
120:     {
121:         $parts = explode(':', $presenter);
122:         $mapping = isset($parts[1], $this->mapping[$parts[0]])
123:             ? $this->mapping[array_shift($parts)]
124:             : $this->mapping['*'];
125: 
126:         while ($part = array_shift($parts)) {
127:             $mapping[0] .= str_replace('*', $part, $mapping[$parts ? 1 : 2]);
128:         }
129:         return $mapping[0];
130:     }
131: 
132: 
133:     /**
134:      * Formats presenter name from class name.
135:      * @param  string
136:      * @return string
137:      * @internal
138:      */
139:     public function unformatPresenterClass($class)
140:     {
141:         foreach ($this->mapping as $module => $mapping) {
142:             $mapping = str_replace(array('\\', '*'), array('\\\\', '(\w+)'), $mapping);
143:             if (preg_match("#^\\\\?$mapping[0]((?:$mapping[1])*)$mapping[2]\\z#i", $class, $matches)) {
144:                 return ($module === '*' ? '' : $module . ':')
145:                     . preg_replace("#$mapping[1]#iA", '$1:', $matches[1]) . $matches[3];
146:             }
147:         }
148:     }
149: 
150: }
151: 
Nette 2.3.1 API API documentation generated by ApiGen 2.8.0