/shopaholic/lib/Nette/Application/PresenterLoader.php
PHP | 143 lines | 61 code | 32 blank | 50 comment | 13 complexity | 6965124db70e0a61e8e7669b2ef8302c MD5 | raw file
Possible License(s): WTFPL
- <?php
-
- /**
- * Nette Framework
- *
- * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "Nette license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://nettephp.com
- *
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @license http://nettephp.com/license Nette license
- * @link http://nettephp.com
- * @category Nette
- * @package Nette\Application
- * @version $Id: PresenterLoader.php 246 2009-03-30 04:00:21Z david@grudl.com $
- */
-
-
-
- require_once dirname(__FILE__) . '/../Application/IPresenterLoader.php';
-
-
-
- /**
- * Default presenter loader.
- *
- * @author David Grudl
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @package Nette\Application
- */
- class PresenterLoader implements IPresenterLoader
- {
- /** @var bool */
- public $caseSensitive = FALSE;
-
- /** @var array */
- private $cache = array();
-
-
-
- /**
- * @param string presenter name
- * @return string class name
- * @throws InvalidPresenterException
- */
- public function getPresenterClass(& $name)
- {
- if (isset($this->cache[$name])) {
- list($class, $name) = $this->cache[$name];
- return $class;
- }
-
- if (!is_string($name) || !preg_match("#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#", $name)) {
- throw new InvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
- }
-
- $class = $this->formatPresenterClass($name);
-
- if (!class_exists($class)) {
- // internal autoloading
- $file = $this->formatPresenterFile($name);
- if (is_file($file) && is_readable($file)) {
- LimitedScope::load($file);
- }
-
- if (!class_exists($class)) {
- throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
- }
- }
-
- $reflection = new ReflectionClass($class);
- $class = $reflection->getName();
-
- if (!$reflection->implementsInterface('IPresenter')) {
- throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is not Nette\\Application\\IPresenter implementor.");
- }
-
- if ($reflection->isAbstract()) {
- throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
- }
-
- // canonicalize presenter name
- $realName = $this->unformatPresenterClass($class);
- if ($name !== $realName) {
- if ($this->caseSensitive) {
- throw new InvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
- } else {
- $this->cache[$name] = array($class, $realName);
- $name = $realName;
- }
- } else {
- $this->cache[$name] = array($class, $realName);
- }
-
- return $class;
- }
-
-
-
- /**
- * Formats presenter class name from its name.
- * @param string
- * @return string
- */
- public function formatPresenterClass($presenter)
- {
- // PHP 5.3
-
- return strtr($presenter, ':', '_') . 'Presenter';
- }
-
-
-
- /**
- * Formats presenter name from class name.
- * @param string
- * @return string
- */
- public function unformatPresenterClass($class)
- {
- // PHP 5.3
-
- return strtr(substr($class, 0, -9), '_', ':');
- }
-
-
-
- /**
- * Formats presenter class file name.
- * @param string
- * @return string
- */
- public function formatPresenterFile($presenter)
- {
- $presenter = str_replace(':', 'Module/', $presenter);
- $presenter = Environment::getVariable('presentersDir') . '/' . $presenter . 'Presenter.php';
- return $presenter;
- }
-
- }