PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/Base/View.php

https://github.com/ilyich/iqyou
PHP | 224 lines | 197 code | 25 blank | 2 comment | 30 complexity | b665edecd2706c1d30c21dbb0e7295e7 MD5 | raw file
  1. <?php
  2. // Это замена для Zend_View (теоретически немного более быстрая, но может быть не полностью совместимая)
  3. class Base_View
  4. {
  5. private $_helperPath = 'application/views/helpers/';
  6. protected $_path = null;
  7. private $_file = null;
  8. private $_request = null;
  9. private $_encoding = 'CP1251';
  10. private $_strictVars = false;
  11. private $_helper;
  12. private $_helperClass;
  13. public function __construct($config = array())
  14. {
  15. $this->setScriptPath(null);
  16. if (array_key_exists('scriptPath', $config)) {
  17. $this->addScriptPath($config['scriptPath']);
  18. unset($config['scriptPath']);
  19. }
  20. if (array_key_exists('helperPath', $config)) {
  21. $this->addHelperPath($config['helperPath']);
  22. unset($config['helperPath']);
  23. }
  24. if (array_key_exists('encoding', $config)) {
  25. $this->setEncoding($config['encoding']);
  26. unset($config['encoding']);
  27. }
  28. if (!empty($config)) {
  29. trigger_error('Base_View __construct config is not empty!');
  30. }
  31. }
  32. public function render($name)
  33. {
  34. $this->_file = $this->_script($name);
  35. unset($name);
  36. if (class_exists('Base_Service_Profiler_Log')) {
  37. Base_Service_Profiler_Log::logGeneral('Start render template '.$this->_file);
  38. }
  39. ob_start();
  40. include $this->_file;
  41. return ob_get_clean();
  42. }
  43. public function p($key, $default = '')
  44. {
  45. $this->_request = Base_Context::getInstance()->getRequest();
  46. if ($key == '*') {
  47. return $this->_request->getParams();
  48. }
  49. return $this->_request->getParam($key, $default);
  50. }
  51. public function __call($name, $args)
  52. {
  53. $this->_helper = ucfirst($name);
  54. $this->_helperClass = 'Zend_View_Helper_'. $this->_helper;
  55. include_once $this->_helperPath . $this->_helper . '.php';
  56. $this->_helper = new $this->_helperClass;
  57. return call_user_func_array(array($this->_helper, $name), $args);
  58. }
  59. public function setScriptPath($path)
  60. {
  61. $this->_path = array();
  62. $this->_addPath($path);
  63. return $this;
  64. }
  65. public function addScriptPath($path)
  66. {
  67. $this->_addPath($path);
  68. return $this;
  69. }
  70. public function setBasePath($path)
  71. {
  72. $path = rtrim($path, '/');
  73. $path = rtrim($path, '\\');
  74. $path .= DIRECTORY_SEPARATOR;
  75. $this->setScriptPath($path . 'scripts');
  76. return $this;
  77. }
  78. public function setEncoding($encoding)
  79. {
  80. $this->_encoding = $encoding;
  81. return $this;
  82. }
  83. public function strictVars($flag = true)
  84. {
  85. $this->_strictVars = ($flag) ? true : false;
  86. return $this;
  87. }
  88. public function addHelperPath($path = null)
  89. {
  90. $this->_helperPath = $path === null ? $this->_helperPath : $path;
  91. }
  92. public function __get($key)
  93. {
  94. if ($this->_strictVars) {
  95. trigger_error('Key "' . $key . '" does not exist', E_USER_NOTICE);
  96. }
  97. return null;
  98. }
  99. public function __isset($key)
  100. {
  101. if ('_' != substr($key, 0, 1)) {
  102. return isset($this->$key);
  103. }
  104. return false;
  105. }
  106. public function __set($key, $val)
  107. {
  108. if ('_' != substr($key, 0, 1)) {
  109. $this->$key = $val;
  110. return;
  111. }
  112. throw new Base_Exception('Setting private or protected class members is not allowed');
  113. }
  114. public function __unset($key)
  115. {
  116. if ('_' != substr($key, 0, 1) && isset($this->$key)) {
  117. unset($this->$key);
  118. }
  119. }
  120. protected function _addPath($path)
  121. {
  122. if (is_array($path)) {
  123. trigger_error('Base_View _addPath path is array');
  124. }
  125. foreach ((array) $path as $dir) {
  126. $dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $dir);
  127. $dir = rtrim($dir, DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  128. array_unshift($this->_path, $dir);
  129. if (count($this->_path) >= 2) {
  130. trigger_error('Base_View _path is array with more than 1 element');
  131. }
  132. }
  133. }
  134. private function _script($name)
  135. {
  136. if (0 == count($this->_path)) {
  137. throw new Base_Exception('no view script directory set; unable to determine location for view script');
  138. }
  139. foreach ($this->_path as $dir) {
  140. // пето-костыль для переезда на 3х колоночную верстку, простите меня люди добрые :( @darazum
  141. if (Base_Context::getInstance() && Base_Context::getInstance()->getRequest() && Base_Context::getInstance()->getRequest()->getModuleName() == 'Pet' && Pet_Service_Base::isFS2Pet() && (strstr($dir, 'App/Pet/Templates/') !== false)) {
  142. $newDir = str_replace('App/Pet/Templates/', 'App/Pet/FS2Templates/', $dir);
  143. if (is_readable($newDir . $name)) {
  144. if (!PRODUCTION || mt_rand(0, 100) == 1) {
  145. Pet_Dao_Base::saveFs2TplLog($newDir . $name);
  146. }
  147. return $newDir . $name;
  148. }
  149. if (is_readable($dir . $name)) {
  150. return $dir . $name;
  151. }
  152. } else {
  153. if (is_readable($dir . $name)) {
  154. return $dir . $name;
  155. }
  156. }
  157. }
  158. $message = "script '$name' not found in path (". implode(PATH_SEPARATOR, $this->_path) .")";
  159. throw new Base_Exception($message);
  160. }
  161. public function assign($spec, $value = null)
  162. {
  163. if (is_string($spec)) {
  164. if ('_' == substr($spec, 0, 1)) {
  165. trigger_error('Base_View: Setting private or protected class members is not allowed!');
  166. return $this;
  167. }
  168. $this->$spec = $value;
  169. } elseif (is_array($spec)) {
  170. $error = false;
  171. foreach ($spec as $key => $val) {
  172. if ('_' == substr($key, 0, 1)) {
  173. $error = true;
  174. break;
  175. }
  176. $this->$key = $val;
  177. }
  178. if ($error) {
  179. trigger_error('Base_View: Setting private or protected class members is not allowed!');
  180. return $this;
  181. }
  182. } else {
  183. trigger_error('Base_View: assign() expects a string or array, received ' . gettype($spec));
  184. return $this;
  185. }
  186. return $this;
  187. }
  188. }