Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NAppForm
  • NApplication
  • NCliRouter
  • NControl
  • NDownloadResponse
  • NForwardingResponse
  • NJsonResponse
  • NLink
  • NMultiRouter
  • NPresenter
  • NPresenterComponent
  • NPresenterLoader
  • NPresenterRequest
  • NRedirectingResponse
  • NRenderResponse
  • NRoute
  • NSimpleRouter

Interfaces

  • IPartiallyRenderable
  • IPresenter
  • IPresenterLoader
  • IPresenterResponse
  • IRenderable
  • IRouter
  • ISignalReceiver
  • IStatePersistent

Exceptions

  • NAbortException
  • NApplicationException
  • NBadRequestException
  • NBadSignalException
  • NForbiddenRequestException
  • NInvalidLinkException
  • NInvalidPresenterException
  • Overview
  • Package
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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 NPresenterLoader implements IPresenterLoader
 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: 
 33: 
 34:     /**
 35:      * @param  string
 36:      */
 37:     public function __construct($baseDir)
 38:     {
 39:         $this->baseDir = $baseDir;
 40:     }
 41: 
 42: 
 43: 
 44:     /**
 45:      * @param  string  presenter name
 46:      * @return string  class name
 47:      * @throws NInvalidPresenterException
 48:      */
 49:     public function getPresenterClass(& $name)
 50:     {
 51:         if (isset($this->cache[$name])) {
 52:             list($class, $name) = $this->cache[$name];
 53:             return $class;
 54:         }
 55: 
 56:         if (!is_string($name) || !preg_match("#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#", $name)) {
 57:             throw new NInvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
 58:         }
 59: 
 60:         $class = $this->formatPresenterClass($name);
 61: 
 62:         if (!class_exists($class)) {
 63:             // internal autoloading
 64:             $file = $this->formatPresenterFile($name);
 65:             if (is_file($file) && is_readable($file)) {
 66:                 NLimitedScope::load($file, TRUE);
 67:             }
 68: 
 69:             if (!class_exists($class)) {
 70:                 throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
 71:             }
 72:         }
 73: 
 74:         $reflection = new NClassReflection($class);
 75:         $class = $reflection->getName();
 76: 
 77:         if (!$reflection->implementsInterface('IPresenter')) {
 78:             throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is not IPresenter implementor.");
 79:         }
 80: 
 81:         if ($reflection->isAbstract()) {
 82:             throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
 83:         }
 84: 
 85:         // canonicalize presenter name
 86:         $realName = $this->unformatPresenterClass($class);
 87:         if ($name !== $realName) {
 88:             if ($this->caseSensitive) {
 89:                 throw new NInvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
 90:             } else {
 91:                 $this->cache[$name] = array($class, $realName);
 92:                 $name = $realName;
 93:             }
 94:         } else {
 95:             $this->cache[$name] = array($class, $realName);
 96:         }
 97: 
 98:         return $class;
 99:     }
100: 
101: 
102: 
103:     /**
104:      * Formats presenter class name from its name.
105:      * @param  string
106:      * @return string
107:      */
108:     public function formatPresenterClass($presenter)
109:     {
110:         return strtr($presenter, ':', '_') . 'Presenter';
111:         return str_replace(':', 'Module\\', $presenter) . 'Presenter';
112:     }
113: 
114: 
115: 
116:     /**
117:      * Formats presenter name from class name.
118:      * @param  string
119:      * @return string
120:      */
121:     public function unformatPresenterClass($class)
122:     {
123:         return strtr(substr($class, 0, -9), '_', ':');
124:         return str_replace('Module\\', ':', substr($class, 0, -9));
125:     }
126: 
127: 
128: 
129:     /**
130:      * Formats presenter class file name.
131:      * @param  string
132:      * @return string
133:      */
134:     public function formatPresenterFile($presenter)
135:     {
136:         $path = '/' . str_replace(':', 'Module/', $presenter);
137:         return $this->baseDir . substr_replace($path, '/presenters', strrpos($path, '/'), 0) . 'Presenter.php';
138:     }
139: 
140: }
141: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0