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    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    Application
  32. 32:  */
  33. 33: class NPresenterLoader implements IPresenterLoader
  34. 34: {
  35. 35:     /** @var bool */
  36. 36:     public $caseSensitive = FALSE;
  37. 37:  
  38. 38:     /** @var array */
  39. 39:     private $cache array();
  40. 40:  
  41. 41:  
  42. 42:  
  43. 43:     /**
  44. 44:      * @param  string  presenter name
  45. 45:      * @return string  class name
  46. 46:      * @throws NInvalidPresenterException
  47. 47:      */
  48. 48:     public function getPresenterClass($name)
  49. 49:     {
  50. 50:         if (isset($this->cache[$name])) {
  51. 51:             list($class$name$this->cache[$name];
  52. 52:             return $class;
  53. 53:         }
  54. 54:  
  55. 55:         if (!is_string($name|| !preg_match("#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#"$name)) {
  56. 56:             throw new NInvalidPresenterException("NPresenter name must be alphanumeric string, '$name' is invalid.");
  57. 57:         }
  58. 58:  
  59. 59:         $class $this->formatPresenterClass($name);
  60. 60:  
  61. 61:         if (!class_exists($class)) {
  62. 62:             // internal autoloading
  63. 63:             $file $this->formatPresenterFile($name);
  64. 64:             if (is_file($file&& is_readable($file)) {
  65. 65:                 NLimitedScope::load($file);
  66. 66:             }
  67. 67:  
  68. 68:             if (!class_exists($class)) {
  69. 69:                 throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
  70. 70:             }
  71. 71:         }
  72. 72:  
  73. 73:         $reflection new ReflectionClass($class);
  74. 74:         $class $reflection->getName();
  75. 75:  
  76. 76:         if (!$reflection->implementsInterface('IPresenter')) {
  77. 77:             throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is not \Application\\IPresenter implementor.");
  78. 78:         }
  79. 79:  
  80. 80:         if ($reflection->isAbstract()) {
  81. 81:             throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
  82. 82:         }
  83. 83:  
  84. 84:         // canonicalize presenter name
  85. 85:         $realName $this->unformatPresenterClass($class);
  86. 86:         if ($name !== $realName{
  87. 87:             if ($this->caseSensitive{
  88. 88:                 throw new NInvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
  89. 89:             else {
  90. 90:                 $this->cache[$namearray($class$realName);
  91. 91:                 $name $realName;
  92. 92:             }
  93. 93:         else {
  94. 94:             $this->cache[$namearray($class$realName);
  95. 95:         }
  96. 96:  
  97. 97:         return $class;
  98. 98:     }
  99. 99:  
  100. 100:  
  101. 101:  
  102. 102:     /**
  103. 103:      * Formats presenter class name from its name.
  104. 104:      * @param  string 
  105. 105:      * @return string 
  106. 106:      */
  107. 107:     public function formatPresenterClass($presenter)
  108. 108:     {
  109. 109:         // PHP 5.3
  110. 110:         
  111. 111:         return strtr($presenter':''_''Presenter';
  112. 112:     }
  113. 113:  
  114. 114:  
  115. 115:  
  116. 116:     /**
  117. 117:      * Formats presenter name from class name.
  118. 118:      * @param  string 
  119. 119:      * @return string 
  120. 120:      */
  121. 121:     public function unformatPresenterClass($class)
  122. 122:     {
  123. 123:         // PHP 5.3
  124. 124:         
  125. 125:         return strtr(substr($class0-9)'_'':');
  126. 126:     }
  127. 127:  
  128. 128:  
  129. 129:  
  130. 130:     /**
  131. 131:      * Formats presenter class file name.
  132. 132:      * @param  string 
  133. 133:      * @return string 
  134. 134:      */
  135. 135:     public function formatPresenterFile($presenter)
  136. 136:     {
  137. 137:         $presenter str_replace(':''Module/'$presenter);
  138. 138:         $presenter NEnvironment::getVariable('presentersDir''/' $presenter 'Presenter.php';
  139. 139:         return $presenter;
  140. 140:     }
  141. 141: