Packages

  • 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

  • NApplication
  • NPresenterFactory
  • NPresenterRequest

Interfaces

  • IPresenter
  • IPresenterFactory
  • IPresenterResponse
  • IRouter

Exceptions

  • NAbortException
  • NApplicationException
  • NBadRequestException
  • NForbiddenRequestException
  • NInvalidPresenterException
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 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 extends NObject 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 NDIContainer */
 33:     private $container;
 34: 
 35: 
 36:     /**
 37:      * @param  string
 38:      */
 39:     public function __construct($baseDir, NDIContainer $container)
 40:     {
 41:         $this->baseDir = $baseDir;
 42:         $this->container = $container;
 43:     }
 44: 
 45: 
 46:     /**
 47:      * Creates new presenter instance.
 48:      * @param  string  presenter name
 49:      * @return IPresenter
 50:      */
 51:     public function createPresenter($name)
 52:     {
 53:         $presenter = $this->container->createInstance($this->getPresenterClass($name));
 54:         if (method_exists($presenter, 'setContext')) {
 55:             $this->container->callMethod(array($presenter, 'setContext'));
 56:         }
 57:         foreach (array_reverse(get_class_methods($presenter)) as $method) {
 58:             if (substr($method, 0, 6) === 'inject') {
 59:                 $this->container->callMethod(array($presenter, $method));
 60:             }
 61:         }
 62: 
 63:         if ($presenter instanceof NPresenter && $presenter->invalidLinkMode === NULL) {
 64:             $presenter->invalidLinkMode = $this->container->parameters['debugMode'] ? NPresenter::INVALID_LINK_WARNING : NPresenter::INVALID_LINK_SILENT;
 65:         }
 66:         return $presenter;
 67:     }
 68: 
 69: 
 70:     /**
 71:      * Generates and checks presenter class name.
 72:      * @param  string  presenter name
 73:      * @return string  class name
 74:      * @throws NInvalidPresenterException
 75:      */
 76:     public function getPresenterClass(& $name)
 77:     {
 78:         if (isset($this->cache[$name])) {
 79:             list($class, $name) = $this->cache[$name];
 80:             return $class;
 81:         }
 82: 
 83:         if (!is_string($name) || !NStrings::match($name, '#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*\z#')) {
 84:             throw new NInvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
 85:         }
 86: 
 87:         $class = $this->formatPresenterClass($name);
 88: 
 89:         if (!class_exists($class)) {
 90:             // internal autoloading
 91:             $file = $this->formatPresenterFile($name);
 92:             if (is_file($file) && is_readable($file)) {
 93:                 NLimitedScope::load($file, TRUE);
 94:             }
 95: 
 96:             if (!class_exists($class)) {
 97:                 throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
 98:             }
 99:         }
100: 
101:         $reflection = new NClassReflection($class);
102:         $class = $reflection->getName();
103: 
104:         if (!$reflection->implementsInterface('IPresenter')) {
105:             throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is not IPresenter implementor.");
106:         }
107: 
108:         if ($reflection->isAbstract()) {
109:             throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
110:         }
111: 
112:         // canonicalize presenter name
113:         $realName = $this->unformatPresenterClass($class);
114:         if ($name !== $realName) {
115:             if ($this->caseSensitive) {
116:                 throw new NInvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
117:             } else {
118:                 $this->cache[$name] = array($class, $realName);
119:                 $name = $realName;
120:             }
121:         } else {
122:             $this->cache[$name] = array($class, $realName);
123:         }
124: 
125:         return $class;
126:     }
127: 
128: 
129:     /**
130:      * Formats presenter class name from its name.
131:      * @param  string
132:      * @return string
133:      */
134:     public function formatPresenterClass($presenter)
135:     {
136:         return strtr($presenter, ':', '_') . 'Presenter';
137:         return str_replace(':', 'Module\\', $presenter) . 'Presenter';
138:     }
139: 
140: 
141:     /**
142:      * Formats presenter name from class name.
143:      * @param  string
144:      * @return string
145:      */
146:     public function unformatPresenterClass($class)
147:     {
148:         return strtr(substr($class, 0, -9), '_', ':');
149:         return str_replace('Module\\', ':', substr($class, 0, -9));
150:     }
151: 
152: 
153:     /**
154:      * Formats presenter class file name.
155:      * @param  string
156:      * @return string
157:      */
158:     public function formatPresenterFile($presenter)
159:     {
160:         $path = '/' . str_replace(':', 'Module/', $presenter);
161:         return $this->baseDir . substr_replace($path, '/presenters', strrpos($path, '/'), 0) . 'Presenter.php';
162:     }
163: 
164: }
165: 
Nette Framework 2.0.13 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0