PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/Template.php

https://gitlab.com/trungthao379/phpmyadmin
PHP | 166 lines | 85 code | 12 blank | 69 comment | 12 complexity | ec7ab0adf3854d4eff6e4d950eea14f4 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * hold PMA\libraries\Template class
  5. *
  6. * @package PMA\libraries
  7. */
  8. namespace PMA\libraries;
  9. /**
  10. * Class Template
  11. *
  12. * Handle front end templating
  13. *
  14. * @package PMA\libraries
  15. */
  16. class Template
  17. {
  18. /**
  19. * Name of the template
  20. */
  21. protected $name = null;
  22. /**
  23. * Data associated with the template
  24. */
  25. protected $data;
  26. /**
  27. * Helper functions for the template
  28. */
  29. protected $helperFunctions;
  30. const BASE_PATH = 'templates/';
  31. /**
  32. * Template constructor
  33. *
  34. * @param string $name Template name
  35. * @param array $data Variables to be provided to the template
  36. * @param array $helperFunctions Helper functions to be used by template
  37. */
  38. protected function __construct($name, $data = array(), $helperFunctions = array())
  39. {
  40. $this->name = $name;
  41. $this->data = $data;
  42. $this->helperFunctions = $helperFunctions;
  43. }
  44. /**
  45. * Template getter
  46. *
  47. * @param string $name Template name
  48. * @param array $data Variables to be provided to the template
  49. * @param array $helperFunctions Helper functions to be used by template
  50. *
  51. * @return Template
  52. */
  53. public static function get($name, $data = array(), $helperFunctions = array())
  54. {
  55. return new Template($name, $data, $helperFunctions);
  56. }
  57. /**
  58. * Adds more entries to the data for this template
  59. *
  60. * @param array|string $data containing data array or data key
  61. * @param string $value containing data value
  62. */
  63. public function set($data, $value = null)
  64. {
  65. if(is_array($data) && ! $value) {
  66. $this->data = array_merge(
  67. $this->data,
  68. $data
  69. );
  70. } else if (is_string($data)) {
  71. $this->data[$data] = $value;
  72. }
  73. }
  74. /**
  75. * Adds a function for use by the template
  76. *
  77. * @param string $funcName function name
  78. * @param callable $funcDef function definition
  79. */
  80. public function setHelper($funcName, $funcDef)
  81. {
  82. if (! isset($this->helperFunctions[$funcName])) {
  83. $this->helperFunctions[$funcName] = $funcDef;
  84. } else {
  85. throw new \LogicException(
  86. 'The function "' . $funcName . '" is already associated with the template.'
  87. );
  88. }
  89. }
  90. /**
  91. * Removes a function
  92. *
  93. * @param string $funcName function name
  94. */
  95. public function removeHelper($funcName)
  96. {
  97. if (isset($this->helperFunctions[$funcName])) {
  98. unset($this->helperFunctions[$funcName]);
  99. } else {
  100. throw new \LogicException(
  101. 'The function "' . $funcName . '" is not associated with the template.'
  102. );
  103. }
  104. }
  105. /**
  106. * Magic call to locally inaccessible but associated helper functions
  107. *
  108. * @param string $funcName function name
  109. * @param array $arguments function arguments
  110. */
  111. public function __call($funcName, $arguments)
  112. {
  113. if (isset($this->helperFunctions[$funcName])) {
  114. return call_user_func_array($this->helperFunctions[$funcName], $arguments);
  115. } else {
  116. throw new \LogicException(
  117. 'The function "' . $funcName . '" is not associated with the template.'
  118. );
  119. }
  120. }
  121. /**
  122. * Render template
  123. *
  124. * @param array $data Variables to be provided to the template
  125. * @param array $helperFunctions Helper functions to be used by template
  126. *
  127. * @return string
  128. */
  129. public function render($data = array(), $helperFunctions = array())
  130. {
  131. $template = static::BASE_PATH . $this->name . '.phtml';
  132. try {
  133. $this->set($data);
  134. extract($this->data);
  135. $this->helperFunctions = array_merge(
  136. $this->helperFunctions,
  137. $helperFunctions
  138. );
  139. ob_start();
  140. if (file_exists($template)) {
  141. include $template;
  142. } else {
  143. throw new \LogicException(
  144. 'The template "' . $template . '" not found.'
  145. );
  146. }
  147. $content = ob_get_clean();
  148. return $content;
  149. } catch (\LogicException $e) {
  150. ob_end_clean();
  151. throw new \LogicException($e->getMessage());
  152. }
  153. }
  154. }