PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/colaphp/Cola/View.php

http://github.com/eryx/php-framework-benchmark
PHP | 277 lines | 186 code | 20 blank | 71 comment | 11 complexity | 9e2ad26bdba7c306f0fd75f5c77cf046 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  1. <?php
  2. /**
  3. *
  4. */
  5. class Cola_View
  6. {
  7. /**
  8. * Base path of views
  9. *
  10. * @var string
  11. */
  12. protected $_basePath = '';
  13. /**
  14. * Widgets Home
  15. *
  16. * @var string
  17. */
  18. protected $_widgetsHome = '';
  19. /**
  20. * Constructor
  21. *
  22. */
  23. public function __construct($params = array())
  24. {
  25. if (isset($params['basePath'])) {
  26. $this->_basePath = $params['basePath'];
  27. }
  28. }
  29. /**
  30. * Set base path of views
  31. *
  32. * @param string $path
  33. */
  34. public function setBasePath($path)
  35. {
  36. $this->_basePath = $path;
  37. }
  38. /**
  39. * Get base path of views
  40. *
  41. * @return string
  42. */
  43. public function getBasePath()
  44. {
  45. return $this->_basePath;
  46. }
  47. /**
  48. * Render view
  49. *
  50. */
  51. protected function _render($tpl, $dir = null)
  52. {
  53. if (null === $dir) $dir = $this->_basePath;
  54. if ($dir) $dir = rtrim($dir, '/\\') . DIRECTORY_SEPARATOR;
  55. ob_start();
  56. ob_implicit_flush(0);
  57. $file = $dir . $tpl;
  58. include $file;
  59. return ob_get_clean();
  60. }
  61. /**
  62. * Fetch
  63. *
  64. * @param string $tpl
  65. * @param string $dir
  66. * @return string
  67. */
  68. public function fetch($tpl, $dir = null)
  69. {
  70. return $this->_render($tpl, $dir);
  71. }
  72. /**
  73. * Display
  74. *
  75. * @param string $tpl
  76. * @param string $dir
  77. */
  78. public function display($tpl, $dir = null)
  79. {
  80. echo $this->_render($tpl, $dir);
  81. }
  82. /**
  83. * Slot
  84. *
  85. * @param string $tpl
  86. * @param mixed $data
  87. * @return string
  88. */
  89. public function slot($file, $data = null)
  90. {
  91. ob_start();
  92. ob_implicit_flush(0);
  93. include $file;
  94. return ob_get_clean();
  95. }
  96. /**
  97. * Set widgets home dir
  98. *
  99. * @param strong $dir
  100. * @return Cola_View
  101. */
  102. public function setWidgetsHome($dir)
  103. {
  104. $this->_widgetsHome = $dir;
  105. return $this;
  106. }
  107. /**
  108. * Get widgets Home
  109. *
  110. * @return string
  111. */
  112. public function getWidgetsHome()
  113. {
  114. return $this->_widgetsHome;
  115. }
  116. /**
  117. * Widget
  118. *
  119. * @param string $name
  120. * @param array $data
  121. * @return Cola_Com_Widget
  122. */
  123. public function widget($name, $data = null)
  124. {
  125. if (empty($this->_widgetsHome) && $widgetsHome = Cola::config('_widgetsHome')) {
  126. $this->_widgetsHome = $widgetsHome;
  127. }
  128. $class = ucfirst($name) . 'Widget';
  129. if (!Cola::loadClass($class, $this->_widgetsHome)) {
  130. throw new Cola_Exception("Can not find widget:{$class}");
  131. }
  132. $widget = new $class($data);
  133. return $widget;
  134. }
  135. /**
  136. * Escape
  137. *
  138. * @param string $str
  139. * @param string $type
  140. * @param string $encoding
  141. * @return string
  142. */
  143. public static function escape($str, $type = 'html', $encoding = 'UTF-8')
  144. {
  145. switch ($type) {
  146. case 'html':
  147. return htmlspecialchars($str, ENT_QUOTES, $encoding);
  148. case 'htmlall':
  149. return htmlentities($str, ENT_QUOTES, $encoding);
  150. case 'javascript':
  151. return strtr($str, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
  152. case 'mail':
  153. return str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $str);
  154. default:
  155. return $str;
  156. }
  157. }
  158. /**
  159. * Truncate
  160. *
  161. * @param string $str
  162. * @param int $limit
  163. * @param string $encoding
  164. * @param string $suffix
  165. * @param string $regex
  166. * @return string
  167. */
  168. public static function truncate($str, $limit, $encoding = 'UTF-8', $suffix = '...', $regex = null)
  169. {
  170. if (function_exists('mb_strwidth')) {
  171. return self::mbTruncate($str, $limit, $encoding, $suffix);
  172. }
  173. return self::regexTruncate($str, $limit, $encoding, $suffix, $regex = null);
  174. }
  175. /**
  176. * Truncate with mbstring
  177. *
  178. * @param string $str
  179. * @param int $limit
  180. * @param string $encoding
  181. * @param string $suffix
  182. * @return string
  183. */
  184. public static function mbTruncate($str, $limit, $encoding = 'UTF-8', $suffix = '...')
  185. {
  186. if (mb_strwidth($str, $encoding) <= $limit) return $str;
  187. $limit -= mb_strwidth($suffix, $encoding);
  188. $tmp = mb_strimwidth($str, 0, $limit, '', $encoding);
  189. return $tmp . $suffix;
  190. }
  191. /**
  192. * Truncate with regex
  193. *
  194. * @param string $str
  195. * @param int $limit
  196. * @param string $encoding
  197. * @param string $suffix
  198. * @param string $regex
  199. * @return string
  200. */
  201. public static function regexTruncate($str, $limit, $encoding = 'UTF-8', $suffix = '...', $regex = null)
  202. {
  203. $defaultRegex = array(
  204. 'UTF-8' => "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/",
  205. 'GB2312' => "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/",
  206. 'GBK' => "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/",
  207. 'BIG5' => "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"
  208. );
  209. $encoding = strtoupper($encoding);
  210. if (null === $regex && !isset($defaultRegex[$encoding])) {
  211. throw new Exception("Truncate failed: not supported encoding, you should supply a regex for $encoding encoding");
  212. }
  213. $regex || $regex = $defaultRegex[$encoding];
  214. preg_match_all($regex, $str, $match);
  215. $trueLimit = $limit - strlen($suffix);
  216. $len = $pos = 0;
  217. foreach ($match[0] as $word) {
  218. $len += strlen($word) > 1 ? 2 : 1;
  219. if ($len > $trueLimit) continue;
  220. $pos ++;
  221. }
  222. if ($len <= $limit) return $str;
  223. return join("",array_slice($match[0], 0, $pos)) . $suffix;
  224. }
  225. /**
  226. * Dynamic set vars
  227. *
  228. * @param string $key
  229. * @param mixed $value
  230. */
  231. public function __set($key, $value = null)
  232. {
  233. $this->$key = $value;
  234. }
  235. /**
  236. * Dynamic get vars
  237. *
  238. * @param string $key
  239. */
  240. public function __get($key)
  241. {
  242. return null;
  243. }
  244. }