Source for file PresenterLoader.php

Documentation is available at PresenterLoader.php

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