Packages

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

Classes

  • NApplication
  • NPresenterFactory
  • NPresenterRequest

Interfaces

  • IPresenter
  • IPresenterFactory
  • IPresenterResponse
  • IRouter

Exceptions

  • NAbortException
  • NApplicationException
  • NBadRequestException
  • NForbiddenRequestException
  • NInvalidPresenterException
  • Overview
  • Package
  • 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:  * @package Nette\Application
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Default presenter loader.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Application
 20:  */
 21: class NPresenterFactory implements IPresenterFactory
 22: {
 23:     /** @var bool */
 24:     public $caseSensitive = FALSE;
 25: 
 26:     /** @var string */
 27:     private $baseDir;
 28: 
 29:     /** @var array */
 30:     private $cache = array();
 31: 
 32:     /** @var IDIContainer */
 33:     private $context;
 34: 
 35: 
 36: 
 37:     /**
 38:      * @param  string
 39:      */
 40:     public function __construct($baseDir, IDIContainer $context)
 41:     {
 42:         $this->baseDir = $baseDir;
 43:         $this->context = $context;
 44:     }
 45: 
 46: 
 47: 
 48:     /**
 49:      * Create new presenter instance.
 50:      * @param  string  presenter name
 51:      * @return IPresenter
 52:      */
 53:     public function createPresenter($name)
 54:     {
 55:         $class = $this->getPresenterClass($name);
 56:         $presenter = new $class;
 57:         $presenter->setContext($this->context);
 58:         return $presenter;
 59:     }
 60: 
 61: 
 62: 
 63:     /**
 64:      * @param  string  presenter name
 65:      * @return string  class name
 66:      * @throws NInvalidPresenterException
 67:      */
 68:     public function getPresenterClass(& $name)
 69:     {
 70:         if (isset($this->cache[$name])) {
 71:             list($class, $name) = $this->cache[$name];
 72:             return $class;
 73:         }
 74: 
 75:         if (!is_string($name) || !NStrings::match($name, "#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#")) {
 76:             throw new NInvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
 77:         }
 78: 
 79:         $class = $this->formatPresenterClass($name);
 80: 
 81:         if (!class_exists($class)) {
 82:             // internal autoloading
 83:             $file = $this->formatPresenterFile($name);
 84:             if (is_file($file) && is_readable($file)) {
 85:                 NLimitedScope::load($file);
 86:             }
 87: 
 88:             if (!class_exists($class)) {
 89:                 throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
 90:             }
 91:         }
 92: 
 93:         $reflection = new NClassReflection($class);
 94:         $class = $reflection->getName();
 95: 
 96:         if (!$reflection->implementsInterface('IPresenter')) {
 97:             throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is not IPresenter implementor.");
 98:         }
 99: 
100:         if ($reflection->isAbstract()) {
101:             throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
102:         }
103: 
104:         // canonicalize presenter name
105:         $realName = $this->unformatPresenterClass($class);
106:         if ($name !== $realName) {
107:             if ($this->caseSensitive) {
108:                 throw new NInvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
109:             } else {
110:                 $this->cache[$name] = array($class, $realName);
111:                 $name = $realName;
112:             }
113:         } else {
114:             $this->cache[$name] = array($class, $realName);
115:         }
116: 
117:         return $class;
118:     }
119: 
120: 
121: 
122:     /**
123:      * Formats presenter class name from its name.
124:      * @param  string
125:      * @return string
126:      */
127:     public function formatPresenterClass($presenter)
128:     {
129:         return strtr($presenter, ':', '_') . 'Presenter';
130:         return str_replace(':', 'Module\\', $presenter) . 'Presenter';
131:     }
132: 
133: 
134: 
135:     /**
136:      * Formats presenter name from class name.
137:      * @param  string
138:      * @return string
139:      */
140:     public function unformatPresenterClass($class)
141:     {
142:         return strtr(substr($class, 0, -9), '_', ':');
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 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0