PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Twig.php

https://gitlab.com/sulistiana/web-profile-arsy
PHP | 238 lines | 152 code | 32 blank | 54 comment | 10 complexity | 92c5b691899a6231484fccbdee65c9e6 MD5 | raw file
  1. <?php
  2. /**
  3. * Part of CodeIgniter Simple and Secure Twig
  4. *
  5. * @author Kenji Suzuki <https://github.com/kenjis>
  6. * @license MIT License
  7. * @copyright 2015 Kenji Suzuki
  8. * @link https://github.com/kenjis/codeigniter-ss-twig
  9. */
  10. // If you don't use Composer, uncomment below
  11. require_once APPPATH . 'third_party/Twig-1.24.1/lib/Twig/Autoloader.php';
  12. Twig_Autoloader::register();
  13. class Twig
  14. {
  15. private $config = [];
  16. private $functions_asis = [
  17. 'base_url', 'site_url'
  18. ];
  19. private $functions_safe = [
  20. 'form_open', 'form_close', 'form_error', 'set_value', 'form_hidden'
  21. ];
  22. /**
  23. * @var bool Whether functions are added or not
  24. */
  25. private $functions_added = FALSE;
  26. /**
  27. * @var Twig_Environment
  28. */
  29. private $twig;
  30. /**
  31. * @var Twig_Loader_Filesystem
  32. */
  33. private $loader;
  34. public function __construct($params = [])
  35. {
  36. // default config
  37. $this->config = [
  38. 'paths' => [VIEWPATH],
  39. 'cache' => APPPATH . '/cache/twig',
  40. ];
  41. $this->config = array_merge($this->config, $params);
  42. if (isset($params['functions']))
  43. {
  44. $this->functions_asis =
  45. array_unique(
  46. array_merge($this->functions_asis, $params['functions'])
  47. );
  48. }
  49. if (isset($params['functions_safe']))
  50. {
  51. $this->functions_safe =
  52. array_unique(
  53. array_merge($this->functions_safe, $params['functions_safe'])
  54. );
  55. }
  56. }
  57. protected function resetTwig()
  58. {
  59. $this->twig = null;
  60. $this->createTwig();
  61. }
  62. protected function createTwig()
  63. {
  64. // $this->twig is singleton
  65. if ($this->twig !== null)
  66. {
  67. return;
  68. }
  69. if (ENVIRONMENT === 'production')
  70. {
  71. $debug = FALSE;
  72. }
  73. else
  74. {
  75. $debug = TRUE;
  76. }
  77. if ($this->loader === null)
  78. {
  79. $this->loader = new \Twig_Loader_Filesystem($this->config['paths']);
  80. }
  81. $twig = new \Twig_Environment($this->loader, [
  82. 'cache' => $this->config['cache'],
  83. 'debug' => $debug,
  84. 'autoescape' => TRUE,
  85. ]);
  86. if ($debug)
  87. {
  88. $twig->addExtension(new \Twig_Extension_Debug());
  89. }
  90. $this->twig = $twig;
  91. }
  92. protected function setLoader($loader)
  93. {
  94. $this->loader = $loader;
  95. }
  96. /**
  97. * Registers a Global
  98. *
  99. * @param string $name The global name
  100. * @param mixed $value The global value
  101. */
  102. public function addGlobal($name, $value)
  103. {
  104. $this->createTwig();
  105. $this->twig->addGlobal($name, $value);
  106. }
  107. /**
  108. * Renders Twig Template and Set Output
  109. *
  110. * @param string $view Template filename without `.twig`
  111. * @param array $params Array of parameters to pass to the template
  112. */
  113. public function display($view, $params = [])
  114. {
  115. $CI =& get_instance();
  116. $CI->output->set_output($this->render($view, $params));
  117. }
  118. /**
  119. * Renders Twig Template and Returns as String
  120. *
  121. * @param string $view Template filename without `.twig`
  122. * @param array $params Array of parameters to pass to the template
  123. * @return string
  124. */
  125. public function render($view, $params = [])
  126. {
  127. $this->createTwig();
  128. // We call addFunctions() here, because we must call addFunctions()
  129. // after loading CodeIgniter functions in a controller.
  130. $this->addFunctions();
  131. $view = $view . '.twig';
  132. return $this->twig->render($view, $params);
  133. }
  134. protected function addFunctions()
  135. {
  136. // Runs only once
  137. if ($this->functions_added)
  138. {
  139. return;
  140. }
  141. // as is functions
  142. foreach ($this->functions_asis as $function)
  143. {
  144. if (function_exists($function))
  145. {
  146. $this->twig->addFunction(
  147. new \Twig_SimpleFunction(
  148. $function,
  149. $function
  150. )
  151. );
  152. }
  153. }
  154. // safe functions
  155. foreach ($this->functions_safe as $function)
  156. {
  157. if (function_exists($function))
  158. {
  159. $this->twig->addFunction(
  160. new \Twig_SimpleFunction(
  161. $function,
  162. $function,
  163. ['is_safe' => ['html']]
  164. )
  165. );
  166. }
  167. }
  168. // customized functions
  169. if (function_exists('anchor'))
  170. {
  171. $this->twig->addFunction(
  172. new \Twig_SimpleFunction(
  173. 'anchor',
  174. [$this, 'safe_anchor'],
  175. ['is_safe' => ['html']]
  176. )
  177. );
  178. }
  179. $this->functions_added = TRUE;
  180. }
  181. /**
  182. * @param string $uri
  183. * @param string $title
  184. * @param array $attributes [changed] only array is acceptable
  185. * @return string
  186. */
  187. public function safe_anchor($uri = '', $title = '', $attributes = [])
  188. {
  189. $uri = html_escape($uri);
  190. $title = html_escape($title);
  191. $new_attr = [];
  192. foreach ($attributes as $key => $val)
  193. {
  194. $new_attr[html_escape($key)] = html_escape($val);
  195. }
  196. return anchor($uri, $title, $new_attr);
  197. }
  198. /**
  199. * @return \Twig_Environment
  200. */
  201. public function getTwig()
  202. {
  203. $this->createTwig();
  204. return $this->twig;
  205. }
  206. }