Namespaces

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

Classes

  • Application
  • PresenterFactory
  • Request

Interfaces

  • IPresenter
  • IPresenterFactory
  • IResponse
  • IRouter

Exceptions

  • AbortException
  • ApplicationException
  • BadRequestException
  • ForbiddenRequestException
  • InvalidPresenterException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Application;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Default presenter loader.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: class PresenterFactory implements IPresenterFactory
 24: {
 25:     /** @var bool */
 26:     public $caseSensitive = FALSE;
 27: 
 28:     /** @var string */
 29:     private $baseDir;
 30: 
 31:     /** @var array */
 32:     private $cache = array();
 33: 
 34:     /** @var Nette\DI\IContainer */
 35:     private $context;
 36: 
 37: 
 38: 
 39:     /**
 40:      * @param  string
 41:      */
 42:     public function __construct($baseDir, Nette\DI\IContainer $context)
 43:     {
 44:         $this->baseDir = $baseDir;
 45:         $this->context = $context;
 46:     }
 47: 
 48: 
 49: 
 50:     /**
 51:      * Create new presenter instance.
 52:      * @param  string  presenter name
 53:      * @return IPresenter
 54:      */
 55:     public function createPresenter($name)
 56:     {
 57:         $class = $this->getPresenterClass($name);
 58:         $presenter = new $class;
 59:         $presenter->setContext($this->context);
 60:         return $presenter;
 61:     }
 62: 
 63: 
 64: 
 65:     /**
 66:      * @param  string  presenter name
 67:      * @return string  class name
 68:      * @throws InvalidPresenterException
 69:      */
 70:     public function getPresenterClass(& $name)
 71:     {
 72:         if (isset($this->cache[$name])) {
 73:             list($class, $name) = $this->cache[$name];
 74:             return $class;
 75:         }
 76: 
 77:         if (!is_string($name) || !Nette\Utils\Strings::match($name, "#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#")) {
 78:             throw new InvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
 79:         }
 80: 
 81:         $class = $this->formatPresenterClass($name);
 82: 
 83:         if (!class_exists($class)) {
 84:             // internal autoloading
 85:             $file = $this->formatPresenterFile($name);
 86:             if (is_file($file) && is_readable($file)) {
 87:                 Nette\Utils\LimitedScope::load($file);
 88:             }
 89: 
 90:             if (!class_exists($class)) {
 91:                 throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
 92:             }
 93:         }
 94: 
 95:         $reflection = new Nette\Reflection\ClassType($class);
 96:         $class = $reflection->getName();
 97: 
 98:         if (!$reflection->implementsInterface('Nette\Application\IPresenter')) {
 99:             throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is not Nette\\Application\\IPresenter implementor.");
100:         }
101: 
102:         if ($reflection->isAbstract()) {
103:             throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
104:         }
105: 
106:         // canonicalize presenter name
107:         $realName = $this->unformatPresenterClass($class);
108:         if ($name !== $realName) {
109:             if ($this->caseSensitive) {
110:                 throw new InvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
111:             } else {
112:                 $this->cache[$name] = array($class, $realName);
113:                 $name = $realName;
114:             }
115:         } else {
116:             $this->cache[$name] = array($class, $realName);
117:         }
118: 
119:         return $class;
120:     }
121: 
122: 
123: 
124:     /**
125:      * Formats presenter class name from its name.
126:      * @param  string
127:      * @return string
128:      */
129:     public function formatPresenterClass($presenter)
130:     {
131:         return str_replace(':', 'Module\\', $presenter) . 'Presenter';
132:     }
133: 
134: 
135: 
136:     /**
137:      * Formats presenter name from class name.
138:      * @param  string
139:      * @return string
140:      */
141:     public function unformatPresenterClass($class)
142:     {
143:         return str_replace('Module\\', ':', substr($class, 0, -9));
144:     }
145: 
146: 
147: 
148:     /**
149:      * Formats presenter class file name.
150:      * @param  string
151:      * @return string
152:      */
153:     public function formatPresenterFile($presenter)
154:     {
155:         $path = '/' . str_replace(':', 'Module/', $presenter);
156:         return $this->baseDir . substr_replace($path, '/presenters', strrpos($path, '/'), 0) . 'Presenter.php';
157:     }
158: 
159: }
160: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0