PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/shopaholic/lib/Nette/Application/PresenterLoader.php

http://github.com/jakubkulhan/shopaholic
PHP | 143 lines | 61 code | 32 blank | 50 comment | 13 complexity | 6965124db70e0a61e8e7669b2ef8302c MD5 | raw file
Possible License(s): WTFPL
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Application
  17. * @version $Id: PresenterLoader.php 246 2009-03-30 04:00:21Z david@grudl.com $
  18. */
  19. require_once dirname(__FILE__) . '/../Application/IPresenterLoader.php';
  20. /**
  21. * Default presenter loader.
  22. *
  23. * @author David Grudl
  24. * @copyright Copyright (c) 2004, 2009 David Grudl
  25. * @package Nette\Application
  26. */
  27. class PresenterLoader implements IPresenterLoader
  28. {
  29. /** @var bool */
  30. public $caseSensitive = FALSE;
  31. /** @var array */
  32. private $cache = array();
  33. /**
  34. * @param string presenter name
  35. * @return string class name
  36. * @throws InvalidPresenterException
  37. */
  38. public function getPresenterClass(& $name)
  39. {
  40. if (isset($this->cache[$name])) {
  41. list($class, $name) = $this->cache[$name];
  42. return $class;
  43. }
  44. if (!is_string($name) || !preg_match("#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#", $name)) {
  45. throw new InvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
  46. }
  47. $class = $this->formatPresenterClass($name);
  48. if (!class_exists($class)) {
  49. // internal autoloading
  50. $file = $this->formatPresenterFile($name);
  51. if (is_file($file) && is_readable($file)) {
  52. LimitedScope::load($file);
  53. }
  54. if (!class_exists($class)) {
  55. throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
  56. }
  57. }
  58. $reflection = new ReflectionClass($class);
  59. $class = $reflection->getName();
  60. if (!$reflection->implementsInterface('IPresenter')) {
  61. throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is not Nette\\Application\\IPresenter implementor.");
  62. }
  63. if ($reflection->isAbstract()) {
  64. throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
  65. }
  66. // canonicalize presenter name
  67. $realName = $this->unformatPresenterClass($class);
  68. if ($name !== $realName) {
  69. if ($this->caseSensitive) {
  70. throw new InvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
  71. } else {
  72. $this->cache[$name] = array($class, $realName);
  73. $name = $realName;
  74. }
  75. } else {
  76. $this->cache[$name] = array($class, $realName);
  77. }
  78. return $class;
  79. }
  80. /**
  81. * Formats presenter class name from its name.
  82. * @param string
  83. * @return string
  84. */
  85. public function formatPresenterClass($presenter)
  86. {
  87. // PHP 5.3
  88. return strtr($presenter, ':', '_') . 'Presenter';
  89. }
  90. /**
  91. * Formats presenter name from class name.
  92. * @param string
  93. * @return string
  94. */
  95. public function unformatPresenterClass($class)
  96. {
  97. // PHP 5.3
  98. return strtr(substr($class, 0, -9), '_', ':');
  99. }
  100. /**
  101. * Formats presenter class file name.
  102. * @param string
  103. * @return string
  104. */
  105. public function formatPresenterFile($presenter)
  106. {
  107. $presenter = str_replace(':', 'Module/', $presenter);
  108. $presenter = Environment::getVariable('presentersDir') . '/' . $presenter . 'Presenter.php';
  109. return $presenter;
  110. }
  111. }