PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/XenForo/ViewRenderer/Abstract.php

https://github.com/hanguyenhuu/DTUI_201105
PHP | 302 lines | 113 code | 32 blank | 157 comment | 4 complexity | be68aa28b75aa97e5a1fac38eb977722 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Abstract handler for view rendering of a particular response type.
  4. * Handles rendering of different types of controller responses.
  5. *
  6. * @package XenForo_Mvc
  7. */
  8. abstract class XenForo_ViewRenderer_Abstract
  9. {
  10. /**
  11. * Response object. Generally should only be used to manipulate response codes if needed.
  12. *
  13. * @var Zend_Controller_Response_Http
  14. */
  15. protected $_response;
  16. /**
  17. * Request object. Can be used to manipulate output based in input parameters AT YOUR OWN RISK.
  18. * Strictly, making use of this breaks MVC principles, so avoid it if you can.
  19. *
  20. * @var Zend_Controller_Request_Http
  21. */
  22. protected $_request;
  23. /**
  24. * Application dependencies.
  25. *
  26. * @var XenForo_Dependencies_Abstract
  27. */
  28. protected $_dependencies;
  29. /**
  30. * Determines whether the container needs to be rendered. This may apply to an
  31. * entire renderer or just individual render types.
  32. *
  33. * @var boolean
  34. */
  35. protected $_needsContainer = true;
  36. /**
  37. * Constructor.
  38. *
  39. * @param XenForo_Dependencies_Abstract
  40. * @param Zend_Controller_Response_Http
  41. * @param Zend_Controller_Request_Http
  42. */
  43. public function __construct(XenForo_Dependencies_Abstract $dependencies, Zend_Controller_Response_Http $response, Zend_Controller_Request_Http $request)
  44. {
  45. $this->_dependencies = $dependencies;
  46. $this->_response = $response;
  47. $this->_request = $request;
  48. $this->_preloadContainerData();
  49. }
  50. /**
  51. * Renders a redirect. Most renderers will actually redirect, but some may not.
  52. *
  53. * @param integer Type of redirect. See {@link XenForo_ControllerResponse_Redirect}
  54. * @param string Target to redirect to
  55. * @param mixed Redirect message (unused by some redirect methods)
  56. * @param array Extra redirect parameters (unused by HTML)
  57. *
  58. * @return string Empty string (nothing to display)
  59. */
  60. public function renderRedirect($redirectType, $redirectTarget, $redirectMessage = null, array $redirectParams = array())
  61. {
  62. switch ($redirectType)
  63. {
  64. case XenForo_ControllerResponse_Redirect::RESOURCE_CREATED:
  65. case XenForo_ControllerResponse_Redirect::RESOURCE_UPDATED:
  66. case XenForo_ControllerResponse_Redirect::SUCCESS:
  67. $this->_response->setRedirect($redirectTarget, 303);
  68. break;
  69. case XenForo_ControllerResponse_Redirect::RESOURCE_CANONICAL:
  70. $this->_response->setRedirect($redirectTarget, 307);
  71. break;
  72. case XenForo_ControllerResponse_Redirect::RESOURCE_CANONICAL_PERMANENT:
  73. $this->_response->setRedirect($redirectTarget, 301);
  74. break;
  75. default:
  76. throw new XenForo_Exception('Unknown redirect type');
  77. }
  78. $this->_needsContainer = false;
  79. return '';
  80. }
  81. /**
  82. * Renders output of an error.
  83. *
  84. * @param string Text of the error to render
  85. *
  86. * @return string|false Rendered output. False if rendering wasn't possible (see {@link renderUnrepresentable()}).
  87. */
  88. abstract public function renderError($errorText);
  89. /**
  90. * Renders output of an message.
  91. *
  92. * @param string Text of the message to render
  93. *
  94. * @return string|false Rendered output. False if rendering wasn't possible (see {@link renderUnrepresentable()}).
  95. */
  96. abstract public function renderMessage($message);
  97. /**
  98. * Renders output of a view. Should instantiate the view object and render it.
  99. * Note that depending on response type, this class may have to manipulate the
  100. * view name or instantiate a different object.
  101. *
  102. * @param string Name of the view to create
  103. * @param array Key-value array of parameters for the view.
  104. * @param string Name of the template that will be used to display (may be ignored by view)
  105. * @param XenForo_ControllerResponse_View|null A sub-view that will be rendered internal to this view
  106. *
  107. * @return string|XenForo_Template_Abstract|false Rendered output. False if rendering wasn't possible (see {@link renderUnrepresentable()}).
  108. */
  109. abstract public function renderView($viewName, array $params = array(), $templateName = '', XenForo_ControllerResponse_View $subView = null);
  110. /**
  111. * Renders the container output for a page. This often represents the "chrome" of
  112. * a page, including aspects like the header and footer. The content from the other
  113. * render methods will generally be put inside this.
  114. *
  115. * Note that not all response types will have a container. In which case, they
  116. * should return the inner contents directly.
  117. *
  118. * @param string Contents from a previous render method
  119. * @param array Key-value pairs to manipulate the container
  120. *
  121. * @return string Rendered output
  122. */
  123. abstract public function renderContainer($contents, array $params = array());
  124. /**
  125. * Data that should be preloaded for the container. Templates/phrases may be
  126. * accidentally (or intentionally) rendered in the view or before the container
  127. * is set to be rendered. Preloading data here can allow all the data to be fetched
  128. * at once.
  129. */
  130. protected function _preloadContainerData()
  131. {
  132. }
  133. /**
  134. * Replaces the place holders for required externals with the actual requirements.
  135. * This approach is needed to ensure that all requirements are properly included,
  136. * even if they are included after the comment has been rendered.
  137. *
  138. * @param XenForo_Template_Abstract $template The container template object; used to get the requirements
  139. * @param string $rendered Already rendered output
  140. *
  141. * @return string
  142. */
  143. public function replaceRequiredExternalPlaceholders(XenForo_Template_Abstract $template, $rendered)
  144. {
  145. return str_replace(
  146. array(
  147. '<!--XenForo_Require:JS-->',
  148. '<!--XenForo_Require:CSS-->',
  149. '{/*<!--XenForo_Required_Scripts-->*/}',
  150. ),
  151. array(
  152. $template->getRequiredExternalsAsHtml('js'),
  153. $template->getRequiredExternalsAsHtml('css'),
  154. $template->getRequiredExternalsAsJson(),
  155. ),
  156. $rendered
  157. );
  158. }
  159. /**
  160. * Fallback for rendering an "unrepresentable" message. Method is called when
  161. * the concrete rendering function returns false or no concrete rendering function
  162. * is available.
  163. *
  164. * @return string Rendered output
  165. */
  166. abstract public function renderUnrepresentable();
  167. /**
  168. * General helper method to create and render a view object for the specified
  169. * response type. Returns null if no class can be loaded or no view method has
  170. * been defined. Otherwise, the return is defined by the view render method,
  171. * which should return either a string (rendered content) or false (unrepresentable).
  172. *
  173. * @param string View class name
  174. * @param string Response type (translated to method name as render$type)
  175. * @param array Key-value parameters to pass to view. May be modified by the prepareParams call within.
  176. * @param string Template name to pass to view (may be ignored by view)
  177. *
  178. * @return string|false|null
  179. */
  180. public function renderViewObject($class, $responseType, array &$params = array(), &$templateName = '')
  181. {
  182. $baseViewClass = $this->_dependencies->getBaseViewClassName();
  183. $class = XenForo_Application::resolveDynamicClass($class, 'view', $baseViewClass);
  184. if (!$class)
  185. {
  186. $class = $baseViewClass;
  187. }
  188. $view = new $class($this, $this->_response, $params, $templateName);
  189. if (!$view instanceof $baseViewClass)
  190. {
  191. throw new XenForo_Exception('View must be a child of ' . $baseViewClass);
  192. }
  193. $view->prepareParams();
  194. $responseType = ucfirst(strtolower($responseType));
  195. $renderMethod = 'render' . $responseType;
  196. if (method_exists($view, 'render' . $responseType))
  197. {
  198. $return = $view->$renderMethod();
  199. }
  200. else
  201. {
  202. $return = null;
  203. }
  204. $templateName = $view->getTemplateName();
  205. $params = $view->getParams();
  206. return $return;
  207. }
  208. /**
  209. * Renders a sub or child view.
  210. *
  211. * @param XenForo_ControllerResponse_View $subView
  212. *
  213. * @return string|XenForo_Template_Abstract|false
  214. */
  215. public function renderSubView(XenForo_ControllerResponse_View $subView)
  216. {
  217. return $this->renderView($subView->viewName, $subView->params, $subView->templateName, $subView->subView);
  218. }
  219. /**
  220. * Helper method to create a template object for rendering. Templates only represent
  221. * HTML output, so no response type is needed. However, they can be used in any response type.
  222. *
  223. * @param string Name of the template to create
  224. * @param array Key-value parameters to pass to the template
  225. *
  226. * @return XenForo_Template_Abstract
  227. */
  228. public function createTemplateObject($templateName, array $params = array())
  229. {
  230. return $this->_dependencies->createTemplateObject($templateName, $params);
  231. }
  232. /**
  233. * Preloads a template with the template handler for use later.
  234. *
  235. * @param string Template name
  236. */
  237. public function preloadTemplate($templateName)
  238. {
  239. return $this->_dependencies->preloadTemplate($templateName);
  240. }
  241. /**
  242. * Gets the 'needs container' setting.
  243. *
  244. * @return boolean
  245. */
  246. public function getNeedsContainer()
  247. {
  248. return $this->_needsContainer;
  249. }
  250. /**
  251. * Sets the 'needs container' setting
  252. * @param boolean $required
  253. *
  254. * @return boolean
  255. */
  256. public function setNeedsContainer($required)
  257. {
  258. $this->_needsContainer = $required;
  259. return $this->_needsContainer;
  260. }
  261. /**
  262. * Gets the dependencies handler object.
  263. *
  264. * @return XenForo_Dependencies_Abstract
  265. */
  266. public function getDependencyHandler()
  267. {
  268. return $this->_dependencies;
  269. }
  270. }