PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/A/Cli/View.php

http://skeleton.googlecode.com/
PHP | 309 lines | 257 code | 35 blank | 17 comment | 33 complexity | 018a017ca5ccf3e08d148e09f881592a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * View.php
  4. *
  5. * @license http://www.opensource.org/licenses/bsd-license.php BSD
  6. * @link http://skeletonframework.com/
  7. */
  8. /**
  9. * A_Cli_View
  10. *
  11. * Base MVC View class for a whole or partial CLI response character encoding, quoting, escaping, and content.
  12. *
  13. * @package A_Cli
  14. */
  15. class A_Cli_View implements A_Renderer
  16. {
  17. protected $data = array();
  18. protected $template = null;
  19. protected $template_type = 'templates';
  20. protected $template_path = 'templates';
  21. protected $template_scope = 'module';
  22. protected $content = ''; // buffer set manually or by render()
  23. protected $renderer = null;
  24. protected $escape_quote_style = ENT_QUOTES;
  25. protected $escape_output = false;
  26. protected $character_set = 'UTF-8';
  27. protected $locator = null;
  28. protected $load;
  29. protected $flash;
  30. protected $paths = array(); // cache array of paths calculated by Mapper
  31. protected $helpers = array();
  32. protected $helperClass = array(
  33. 'datetime'=>'A_Datetime',
  34. 'json'=>'A_Json',
  35. 'pagination'=>'A_Pagination_View_Standard',
  36. 'url'=>'',
  37. );
  38. protected $use_local_vars = true;
  39. public function __construct($locator=null)
  40. {
  41. $this->locator = $locator;
  42. }
  43. public function setLocator($locator)
  44. {
  45. $this->locator = $locator;
  46. }
  47. public function setCharacterSet($character_set)
  48. {
  49. $this->character_set = $character_set;
  50. return $this;
  51. }
  52. public function setQuoteStyle($escape_quote_style)
  53. {
  54. $this->escape_quote_style = $escape_quote_style;
  55. return $this;
  56. }
  57. public function setEscape($escape_output)
  58. {
  59. $this->escape_output = $escape_output;
  60. return $this;
  61. }
  62. public function useLocalVars($use_local_vars)
  63. {
  64. $this->use_local_vars = $use_local_vars;
  65. return $this;
  66. }
  67. public function setContent($content)
  68. {
  69. $this->content = $content;
  70. return $this;
  71. }
  72. public function getContent()
  73. {
  74. return $this->content;
  75. }
  76. public function setTemplate($template, $scope='')
  77. {
  78. $this->template = $template;
  79. if ($scope) $this->template_scope = $scope;
  80. return $this;
  81. }
  82. public function setTemplateScope($scope)
  83. {
  84. $this->template_scope = $scope;
  85. return $this;
  86. }
  87. public function setTemplatePath($path)
  88. {
  89. $this->template_path = $path;
  90. return $this;
  91. }
  92. public function getTemplate()
  93. {
  94. return $this->template;
  95. }
  96. public function setRenderer($renderer)
  97. {
  98. $this->renderer = $renderer;
  99. return $this;
  100. }
  101. public function hasRenderer()
  102. {
  103. return isset($this->renderer);
  104. }
  105. public function set($name, $value, $default=null)
  106. {
  107. if ($value !== null) {
  108. $this->data[$name] = $value;
  109. } elseif ($default !== null) {
  110. $this->data[$name] = $default;
  111. } else {
  112. unset($this->data[$name]);
  113. }
  114. return $this;
  115. }
  116. public function get($name)
  117. {
  118. return isset($this->data[$name]) ? $this->data[$name] : null;
  119. }
  120. public function has($name)
  121. {
  122. return isset($this->data[$name]);
  123. }
  124. public function import($data)
  125. {
  126. $this->data = array_merge($this->data, $data);
  127. return $this;
  128. }
  129. public function escape($content, $escape_quote_style=null)
  130. {
  131. return htmlspecialchars($content, $escape_quote_style==null ? $this->escape_quote_style : $escape_quote_style, $this->character_set);
  132. }
  133. public function _getPath($template)
  134. {
  135. if (substr($template, -4, 4) != '.php') {
  136. $template .= '.php';
  137. }
  138. // if Locator set by FC then we can get the Mapper
  139. if ($this->locator) {
  140. $mapper = $this->locator->get('Mapper');
  141. if ($mapper) {
  142. // get paths array if not cached
  143. if (!isset($this->paths[$this->template_type])) {
  144. $this->paths[$this->template_type] = $mapper->getPaths($this->template_type);
  145. }
  146. return $this->paths[$this->template_type][$this->template_scope] . $template;
  147. }
  148. }
  149. return $this->template_path . '/' . $template;
  150. }
  151. public function partial($template)
  152. {
  153. $template = $this->_getPath($template);
  154. return $this->escape_output ? $this->escape($this->_include($template)) : $this->_include($template);
  155. }
  156. public function partialLoop($template, $name, $data=null)
  157. {
  158. $template = $this->_getPath($template);
  159. $str = '';
  160. if ($data) {
  161. // $name and $data set so each element in $data set to $name
  162. foreach ($data as $value) {
  163. $this->data[$name] = $value;
  164. $str .= $this->_include($template);
  165. }
  166. } else {
  167. // $name but not $data, so $name contains $data. set() to $keys in each element array
  168. foreach ($name as $data) {
  169. if (is_array($data)) {
  170. foreach ($data as $key => $value) {
  171. $this->data[$key] = $value;
  172. }
  173. }
  174. $str .= $this->_include($template);
  175. }
  176. }
  177. return $this->escape_output ? $this->escape($str) : $str;
  178. }
  179. public function render($template='', $scope='')
  180. {
  181. if (! $template && $this->template) {
  182. $template = $this->template;
  183. }
  184. if ($scope) $this->template_scope = $scope;
  185. if ($template) {
  186. $this->content = $this->_include($this->_getPath($template));
  187. } elseif ($this->renderer) {
  188. if ($this->data) {
  189. foreach ($this->data as $name => $value) {
  190. if (is_object($this->data[$name]) && method_exists($this->data[$name], 'render')) {
  191. $this->renderer->set($name, $this->data[$name]->render());
  192. } else {
  193. $this->renderer->set($name, $value);
  194. }
  195. }
  196. }
  197. if (method_exists($this->renderer, 'render')) {
  198. $this->content = $this->renderer->render();
  199. }
  200. }
  201. return $this->escape_output ? $this->escape($this->content) : $this->content;
  202. }
  203. protected function _include()
  204. {
  205. ob_start();
  206. if ($this->use_local_vars)
  207. extract($this->data, EXTR_REFS);
  208. include func_get_arg(0);
  209. return ob_get_clean();
  210. }
  211. public function __get($name)
  212. {
  213. return isset($this->data[$name]) ? $this->data[$name] : null;
  214. }
  215. public function __set($name, $value)
  216. {
  217. return $this->set($name, $value);
  218. }
  219. public function __toString()
  220. {
  221. return $this->render();
  222. }
  223. protected function _load($scope=null)
  224. {
  225. if (isset($this->load)) {
  226. $this->load->load($scope);
  227. } else {
  228. $this->load = new A_Controller_Helper_Load($this->locator, $this, $scope);
  229. }
  230. return $this->load;
  231. }
  232. protected function _flash($name=null, $value=null)
  233. {
  234. if (!isset($this->flash)) {
  235. $this->flash = new A_Controller_Helper_Flash($this->locator);
  236. }
  237. if ($name) {
  238. if ($value) {
  239. $this->flash->set($name, $value);
  240. } else {
  241. return $this->flash->get($name);
  242. }
  243. }
  244. return $this->flash;
  245. }
  246. public function setHelper($name, $helper)
  247. {
  248. if ($name) {
  249. $this->helpers[$name] = $helper;
  250. }
  251. return $this;
  252. }
  253. public function setHelperClass($name, $class)
  254. {
  255. if ($name) {
  256. $this->helperClass[$name] = $class;
  257. }
  258. return $this;
  259. }
  260. protected function helper($name)
  261. {
  262. if (!isset($this->helpers[$name])) {
  263. if (isset($this->helperClass[$name])) {
  264. $class = $this->helperClass[$name];
  265. } else {
  266. $class = $name;
  267. }
  268. $this->helpers[$name] = $this->locator->get('', $class, '', $this->locator);
  269. }
  270. if (isset($this->helpers[$name])) {
  271. return $this->helpers[$name];
  272. }
  273. }
  274. }