/Twig/Extension/EchoExtension.php

https://github.com/jaugustin/AdmingeneratorGeneratorBundle · PHP · 266 lines · 186 code · 45 blank · 35 comment · 20 complexity · 614edf01744e25a44f2dd98995618995 MD5 · raw file

  1. <?php
  2. namespace Admingenerator\GeneratorBundle\Twig\Extension;
  3. use Symfony\Component\HttpKernel\KernelInterface;
  4. class EchoExtension extends \Twig_Extension
  5. {
  6. protected $loader;
  7. protected $controller;
  8. public function __construct(\Twig_LoaderInterface $loader)
  9. {
  10. $this->loader = $loader;
  11. }
  12. public function setController($controller)
  13. {
  14. $this->controller = $controller;
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function getFunctions()
  20. {
  21. return array(
  22. 'echo_twig' => new \Twig_Function_Method($this, 'getEchoTwig'),
  23. 'echo_block' => new \Twig_Function_Method($this, 'getEchoBlock'),
  24. 'echo_endblock' => new \Twig_Function_Method($this, 'getEchoEndBlock'),
  25. 'echo_for' => new \Twig_Function_Method($this, 'getEchoFor'),
  26. 'echo_endfor' => new \Twig_Function_Method($this, 'getEchoEndFor'),
  27. 'echo_extends' => new \Twig_Function_Method($this, 'getEchoExtends'),
  28. 'echo_if' => new \Twig_Function_Method($this, 'getEchoIf'),
  29. 'echo_if_granted' => new \Twig_Function_Method($this, 'getEchoIfGranted'),
  30. 'echo_else' => new \Twig_Function_Method($this, 'getEchoElse'),
  31. 'echo_elseif' => new \Twig_Function_Method($this, 'getEchoElseIf'),
  32. 'echo_endif' => new \Twig_Function_Method($this, 'getEchoEndIf'),
  33. 'echo_path' => new \Twig_Function_Method($this, 'getEchoPath'),
  34. 'echo_set' => new \Twig_Function_Method($this, 'getEchoSet'),
  35. 'echo_trans' => new \Twig_Function_Method($this, 'getEchoTrans'),
  36. 'echo_twig_assoc' => new \Twig_Function_Method($this, 'getEchoTwigAssoc'),
  37. );
  38. }
  39. public function getFilters()
  40. {
  41. return array(
  42. 'as_php' => new \Twig_Filter_Method($this, 'asPhp'),
  43. 'convert_as_form' => new \Twig_Filter_Method($this, 'convertAsForm'),
  44. );
  45. }
  46. /**
  47. * Try to convert options of form given as string from yaml to a good object
  48. *
  49. * eg type option for collection type
  50. *
  51. * @param string $options the string as php
  52. * @param string $formType the form type
  53. *
  54. * @return string the new options
  55. */
  56. public function convertAsForm($options, $formType)
  57. {
  58. if ('collection' == $formType) {
  59. preg_match("/'type' => '(.+?)'/i", $options, $matches);
  60. if (count($matches) > 0) {
  61. $options = str_replace("'type' => '".$matches[1]."'", '\'type\' => new '.stripslashes($matches[1]).'()', $options);
  62. }
  63. }
  64. if ('model' == $formType) {
  65. preg_match("/'query' => '(.+?)',/i", $options, $matches);
  66. if (count($matches) > 0) {
  67. $options = str_replace("'query' => '".$matches[1]."'", '\'query\' => '.stripslashes($matches[1]), $options);
  68. }
  69. }
  70. if ('choice' == $formType) {
  71. preg_match("/'choices' => '(.+?)',/i", $options, $matches);
  72. if (count($matches) > 0) {
  73. $options = str_replace("'choices' => '".$matches[1]."'", '\'choices\' => '.stripslashes($matches[1]), $options);
  74. }
  75. }
  76. return $options;
  77. }
  78. public function asPhp($variable)
  79. {
  80. if (!is_array($variable)) {
  81. return $this->export($variable);
  82. }
  83. $str = $this->export($variable);
  84. preg_match_all('/[^> ]+::__set_state\(array\((.+),\'loaded/i', $str, $matches);
  85. if (isset($matches[1][0])) {
  86. $params = 'return array('.$matches[1][0].')';
  87. $params = eval($params. '?>');
  88. $str_param = '';
  89. foreach ($params as $p) {
  90. if ('' !== $str_param ) {
  91. $str_param .= ', ';
  92. }
  93. $str_param .= $this->export($p);
  94. }
  95. $str = preg_replace("/([^> ]+)::__set_state\(/i", ' new \\\$0', $str);
  96. $str = str_replace('::__set_state', '', $str);
  97. $str = str_replace('array('.$matches[1][0].',\'loaded\' => false, )', $str_param, $str);
  98. }
  99. return $str;
  100. }
  101. public function export($variable)
  102. {
  103. return str_replace(array("\n", 'array (', ' '), array('', 'array(', ''), var_export($variable, true));
  104. }
  105. public function getEchoTrans($str, $catalog = 'Admingenerator')
  106. {
  107. return '{% trans from "'.$catalog.'" %}'.$str.'{% endtrans %}';
  108. }
  109. public function getEchoSet($var, $value, $value_as_string = true)
  110. {
  111. if ($value_as_string) {
  112. return strtr('{% set %%var%% = "%%value%%" %}',array('%%var%%' => $var, '%%value%%' => $value));
  113. } else {
  114. return strtr('{% set %%var%% = %%value%% %}',array('%%var%%' => $var, '%%value%%' => $value));
  115. }
  116. }
  117. public function getEchopath($path, $params = null)
  118. {
  119. if (null === $params) {
  120. return strtr('{{ path("%%path%%") }}',array('%%path%%' => $path));
  121. }
  122. return strtr('{{ path("%%path%%", %%params%%) }}',array('%%path%%' => $path, '%%params%%'=>$params));
  123. }
  124. public function getEchoIfGranted($credentials, $modelName = null)
  125. {
  126. if (null === $modelName) {
  127. return $this->getEchoIf('is_expr_granted(\''.$credentials.'\')');
  128. }
  129. return $this->getEchoIf('is_expr_granted(\''.$credentials.'\', '.$modelName.')');
  130. }
  131. public function getEchoIf($condition)
  132. {
  133. if ( is_bool( $condition ) ) {
  134. $condition = intval( $condition );
  135. }
  136. return str_replace('%%condition%%', $condition, '{% if %%condition%% %}');
  137. }
  138. public function getEchoElseIf($condition)
  139. {
  140. if ( is_bool( $condition ) ) {
  141. $condition = intval( $condition );
  142. }
  143. return str_replace('%%condition%%', $condition, '{% elseif %%condition%% %}');
  144. }
  145. public function getEchoElse()
  146. {
  147. return '{% else %}';
  148. }
  149. public function getEchoEndIf()
  150. {
  151. return '{% endif %}';
  152. }
  153. public function getEchoTwig($str)
  154. {
  155. return sprintf('{{ %s }}', $str);
  156. }
  157. public function getEchoBlock($name)
  158. {
  159. return str_replace('%%name%%', $name, '{% block %%name%% %}');
  160. }
  161. public function getEchoExtends($name)
  162. {
  163. return str_replace('%%name%%', $name, '{% extends "%%name%%" %}');
  164. }
  165. public function getEchoEndBlock()
  166. {
  167. return '{% endblock %}';
  168. }
  169. public function getEchoFor($object, $in)
  170. {
  171. return strtr('{% for %%object%% in %%in%% %}', array('%%object%%' => $object, '%%in%%' => $in ));
  172. }
  173. public function getEchoEndFor()
  174. {
  175. return '{% endfor %}';
  176. }
  177. /**
  178. * Converts an assoc array to a twig array expression (string).
  179. * Only in case a value contains '{{' and '}}' the value won't be
  180. * wrapped in quotes.
  181. *
  182. * An array like:
  183. * <code>
  184. * $array = array('a' => 'b', 'c' => 'd');
  185. * </code>
  186. *
  187. * Will be converted to:
  188. * <code>
  189. * "{ a: 'b', c: 'd' }"
  190. * </code>
  191. *
  192. * @return string The parameters to be used in a URL
  193. */
  194. public function getEchoTwigAssoc(array $arr)
  195. {
  196. $contents = array();
  197. foreach ($arr as $key => $value)
  198. {
  199. if (!strstr($value, '{{')
  200. || !strstr($value, '}}'))
  201. {
  202. $value = "'$value'";
  203. }
  204. else
  205. {
  206. $value = trim(str_replace(array('{{', '}}'), '', $value));
  207. }
  208. $contents[] = "$key: $value";
  209. }
  210. return '{ ' . implode(', ', $contents) . ' }';
  211. }
  212. /**
  213. * Returns the name of the extension.
  214. *
  215. * @return string The extension name
  216. */
  217. public function getName()
  218. {
  219. return 'echo';
  220. }
  221. }