/class/Renderer.php

https://github.com/riaf/ethna · PHP · 336 lines · 138 code · 37 blank · 161 comment · 19 complexity · e53c2cd5c4c2baa70464cc7ab96c44d9 MD5 · raw file

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4. * Renderer.php
  5. *
  6. * @author Kazuhiro Hosoi <hosoi@gree.co.jp>
  7. * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  8. * @package Ethna
  9. * @version $Id: e3e9c8f68ab4a40e933215e67c8a90c383e7c09f $
  10. */
  11. // {{{ Ethna_Renderer
  12. /**
  13. * Template Renderer
  14. *
  15. * @author Kazuhiro Hosoi <hosoi@gree.co.jp>
  16. * @access public
  17. * @package Ethna
  18. */
  19. class Ethna_Renderer
  20. {
  21. /**#@+
  22. * @access private
  23. */
  24. /** @protected object Ethna_Controller controllerオブジェクト */
  25. protected $controller;
  26. /** @protected object Ethna_Controller controllerオブジェクト($controllerの省略形) */
  27. protected $ctl;
  28. /** @protected array [appid]-ini.phpのレンダラ設定 */
  29. protected $config;
  30. /** @protected string template directory */
  31. protected $template_dir;
  32. /** @protected string template engine */
  33. protected $engine;
  34. /** @protected string path of template engine */
  35. protected $engine_path = false;
  36. /** @protected string template file */
  37. protected $template;
  38. /** @protected string テンプレート変数 */
  39. protected $prop;
  40. /** @protected string レンダラプラグイン(Ethna_Pluginとは関係なし) */
  41. protected $plugin_registry;
  42. /** @protected object Ethna_Logger ログオブジェクト */
  43. protected $logger;
  44. /**
  45. * Ethna_Rendererクラスのコンストラクタ
  46. *
  47. * @access public
  48. */
  49. public function __construct($controller)
  50. {
  51. $this->controller = $controller;
  52. $this->ctl = $this->controller;
  53. $this->template_dir = null;
  54. $this->engine = null;
  55. $this->template = null;
  56. $this->prop = array();
  57. $this->plugin_registry = array();
  58. $config = $this->ctl->getConfig();
  59. $this->config = $config->get('renderer');
  60. $this->logger = $this->controller->getBackend()->getLogger();
  61. }
  62. /**
  63. * ビューを出力する
  64. *
  65. * @param string $template テンプレート
  66. * @param bool $capture true ならば出力を表示せずに返す
  67. *
  68. * @access public
  69. */
  70. function perform($template = null, $capture = false)
  71. {
  72. if ($template == null && $this->template == null) {
  73. return Ethna::raiseWarning('template is not defined');
  74. }
  75. if ($template != null) {
  76. $this->template = $template;
  77. }
  78. // テンプレートの有無のチェック
  79. if (is_readable($this->template_dir . $this->template) === false) {
  80. return Ethna::raiseWarning("template is not found: " . $this->template);
  81. }
  82. if ($capture === true) {
  83. ob_start();
  84. include_once $this->template_dir . $this->template;
  85. $captured = ob_get_contents();
  86. ob_end_clean();
  87. return $captured;
  88. } else {
  89. include_once $this->template_dir . $this->template;
  90. return true;
  91. }
  92. }
  93. /**
  94. * テンプレートエンジンを取得する
  95. *
  96. * @return object Template Engine.
  97. *
  98. * @access public
  99. */
  100. function getEngine()
  101. {
  102. return $this->engine;
  103. }
  104. /**
  105. * テンプレートディレクトリを取得する
  106. *
  107. * @return string Template Directory
  108. *
  109. * @access public
  110. */
  111. function getTemplateDir()
  112. {
  113. return $this->template_dir;
  114. }
  115. /**
  116. * テンプレート変数を取得する
  117. *
  118. * @param string $name 変数名
  119. *
  120. * @return mixed 変数
  121. *
  122. * @access public
  123. */
  124. function getProp($name)
  125. {
  126. if (isset($this->prop[$name])) {
  127. return $this->prop[$name];
  128. }
  129. return null;
  130. }
  131. /**
  132. * テンプレート変数を削除する
  133. *
  134. * @param name 変数名
  135. *
  136. * @access public
  137. */
  138. function removeProp($name)
  139. {
  140. if (isset($this->prop[$name])) {
  141. unset($this->prop[$name]);
  142. }
  143. }
  144. /**
  145. * テンプレート変数に配列を割り当てる
  146. *
  147. * @param array $array
  148. *
  149. * @access public
  150. */
  151. function setPropArray($array)
  152. {
  153. $this->prop = array_merge($this->prop, $array);
  154. }
  155. /**
  156. * テンプレート変数に配列を参照として割り当てる
  157. *
  158. * @param array $array
  159. *
  160. * @access public
  161. */
  162. function setPropArrayByRef(&$array)
  163. {
  164. $keys = array_keys($array);
  165. $count = sizeof($keys);
  166. for ($i = 0; $i < $count; $i++) {
  167. $this->prop[$keys[$i]] = $array[$keys[$i]];
  168. }
  169. }
  170. /**
  171. * テンプレート変数を割り当てる
  172. *
  173. * @param string $name 変数名
  174. * @param mixed $value 値
  175. *
  176. * @access public
  177. */
  178. function setProp($name, $value)
  179. {
  180. $this->prop[$name] = $value;
  181. }
  182. /**
  183. * テンプレート変数に参照を割り当てる
  184. *
  185. * @param string $name 変数名
  186. * @param mixed $value 値
  187. *
  188. * @access public
  189. */
  190. function setPropByRef($name, &$value)
  191. {
  192. $this->prop[$name] = $value;
  193. }
  194. /**
  195. * テンプレートを割り当てる
  196. *
  197. * @param string $template テンプレート名
  198. *
  199. * @access public
  200. */
  201. function setTemplate($template)
  202. {
  203. $this->template = $template;
  204. }
  205. /**
  206. * テンプレートディレクトリを割り当てる
  207. *
  208. * @param string $dir ディレクトリ名
  209. *
  210. * @access public
  211. */
  212. function setTemplateDir($dir)
  213. {
  214. $this->template_dir = $dir;
  215. if (substr($this->template_dir, -1) != '/') {
  216. $this->template_dir .= '/';
  217. }
  218. }
  219. /**
  220. * テンプレートの有無をチェックする
  221. *
  222. * @param string $template テンプレート名
  223. *
  224. * @access public
  225. */
  226. function templateExists($template)
  227. {
  228. if (substr($this->template_dir, -1) != '/') {
  229. $this->template_dir .= '/';
  230. }
  231. return (is_readable($this->template_dir . $template));
  232. }
  233. /**
  234. * プラグインをセットする
  235. *
  236. * @param string $name プラグイン名
  237. * @param string $type プラグインタイプ
  238. * @param string $plugin プラグイン本体
  239. *
  240. * @access public
  241. */
  242. function setPlugin($name, $type, $plugin)
  243. {
  244. $this->plugin_registry[$type][$name] = $plugin;
  245. }
  246. // {{{ proxy methods (for B.C.)
  247. /**
  248. * テンプレート変数を割り当てる(後方互換)
  249. *
  250. * @access public
  251. */
  252. function assign($name, $value)
  253. {
  254. $this->setProp($name, $value);
  255. }
  256. // }}}
  257. /**
  258. * テンプレート変数に参照を割り当てる(後方互換)
  259. *
  260. * @access public
  261. */
  262. function assign_by_ref($name, &$value)
  263. {
  264. $this->setPropByRef($name, $value);
  265. }
  266. /**
  267. * ビューを出力する
  268. *
  269. * @access public
  270. */
  271. function display($template = null)
  272. {
  273. return $this->perform($template);
  274. }
  275. // }}}
  276. // {{{ loadEngine
  277. /**
  278. * ビューを出力する
  279. *
  280. * @access public
  281. */
  282. protected function loadEngine(array $config)
  283. {
  284. // load template engine
  285. $engine_path = isset($config['path'])
  286. ? $config['path']
  287. : $this->engine_path;
  288. if ($engine_path) {
  289. if (file_exists_ex($engine_path)) {
  290. require_once $engine_path;
  291. }
  292. else {
  293. trigger_error("template engine is not available: path=" . $engine_path, E_USER_ERROR);
  294. }
  295. }
  296. }
  297. // }}}
  298. }
  299. // }}}