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

Classes

Interfaces

Exceptions

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