PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Application/PresenterLoader.php

https://github.com/DocX/nette
PHP | 143 lines | 60 code | 31 blank | 52 comment | 13 complexity | 1336781e46edd398d3e56e4463a27778 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. */
  18. /*namespace Nette\Application;*/
  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. /*Nette\Loaders\*/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(/*Nette\Application\*/'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 str_replace(':', 'Module\\', $presenter) . 'Presenter';*/
  89. return strtr($presenter, ':', '_') . 'Presenter';
  90. }
  91. /**
  92. * Formats presenter name from class name.
  93. * @param string
  94. * @return string
  95. */
  96. public function unformatPresenterClass($class)
  97. {
  98. // PHP 5.3
  99. /*return str_replace('Module\\', ':', substr($class, 0, -9));*/
  100. return strtr(substr($class, 0, -9), '_', ':');
  101. }
  102. /**
  103. * Formats presenter class file name.
  104. * @param string
  105. * @return string
  106. */
  107. public function formatPresenterFile($presenter)
  108. {
  109. $path = '/' . str_replace(':', 'Module/', $presenter);
  110. return /*Nette\*/Environment::getVariable('appDir') . substr_replace($path, '/presenters', strrpos($path, '/'), 0) . 'Presenter.php';
  111. }
  112. }