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