/Twig/Extension/EchoExtension.php

https://github.com/damienalexandre/AdmingeneratorGeneratorBundle · PHP · 234 lines · 171 code · 44 blank · 19 comment · 22 complexity · f39cc5b52715e10ae6806cd6b073d942 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. );
  37. }
  38. public function getFilters()
  39. {
  40. return array(
  41. 'as_php' => new \Twig_Filter_Method($this, 'asPhp'),
  42. 'convert_as_form' => new \Twig_Filter_Method($this, 'convertAsForm'),
  43. );
  44. }
  45. /**
  46. * Try to convert options of form given as string from yaml to a good object
  47. *
  48. * eg type option for collection type
  49. *
  50. * @param string $options the string as php
  51. * @param string $formType the form type
  52. *
  53. * @return string the new options
  54. */
  55. public function convertAsForm($options, $formType)
  56. {
  57. if ('collection' == $formType) {
  58. preg_match("/'type' => '(.+?)'/i", $options, $matches);
  59. if (count($matches) > 0) {
  60. $options = str_replace("'type' => '".$matches[1]."'", '\'type\' => new '.stripslashes($matches[1]).'()', $options);
  61. }
  62. }
  63. if ('model' == $formType) {
  64. preg_match("/'query' => '(.+?)',/i", $options, $matches);
  65. if (count($matches) > 0) {
  66. $options = str_replace("'query' => '".$matches[1]."'", '\'query\' => '.stripslashes($matches[1]), $options);
  67. }
  68. }
  69. if ('choice' == $formType) {
  70. preg_match("/'choices' => '(.+?)',/i", $options, $matches);
  71. if (count($matches) > 0) {
  72. $options = str_replace("'choices' => '".$matches[1]."'", '\'choices\' => '.stripslashes($matches[1]), $options);
  73. }
  74. }
  75. return $options;
  76. }
  77. public function asPhp($variable)
  78. {
  79. if (!is_array($variable)) {
  80. return $this->export($variable);
  81. }
  82. $str = $this->export($variable);
  83. preg_match_all('/[^> ]+::__set_state\(array\((.+),\'loaded/i', $str, $matches);
  84. if (isset($matches[1][0])) {
  85. $params = 'return array('.$matches[1][0].')';
  86. $params = eval($params. '?>');
  87. $str_param = '';
  88. foreach ($params as $p) {
  89. if ('' !== $str_param ) {
  90. $str_param .= ', ';
  91. }
  92. $str_param .= $this->export($p);
  93. }
  94. $str = preg_replace("/([^> ]+)::__set_state\(/i", ' new \\\$0', $str);
  95. $str = str_replace('::__set_state', '', $str);
  96. $str = str_replace('array('.$matches[1][0].',\'loaded\' => false, )', $str_param, $str);
  97. }
  98. return $str;
  99. }
  100. public function export($variable)
  101. {
  102. return str_replace(array("\n", 'array (', ' '), array('', 'array(', ''), var_export($variable, true));
  103. }
  104. public function getEchoTrans($str)
  105. {
  106. return '{% trans from "Admingenerator" %}'.$str.'{% endtrans %}';
  107. }
  108. public function getEchoSet($var, $value, $value_as_string = true)
  109. {
  110. if ($value_as_string) {
  111. return strtr('{% set %%var%% = "%%value%%" %}',array('%%var%%' => $var, '%%value%%' => $value));
  112. } else {
  113. return strtr('{% set %%var%% = %%value%% %}',array('%%var%%' => $var, '%%value%%' => $value));
  114. }
  115. }
  116. public function getEchopath($path, $params = null)
  117. {
  118. if (null === $params) {
  119. return strtr('{{ path("%%path%%") }}',array('%%path%%' => $path));
  120. }
  121. return strtr('{{ path("%%path%%", %%params%%) }}',array('%%path%%' => $path, '%%params%%'=>$params));
  122. }
  123. public function getEchoIfGranted($credentials)
  124. {
  125. preg_match_all('/(\(*)([a-z\_]+)(\)*)/i', $credentials, $matches);
  126. if (count($matches[0]) == 1) {
  127. return $this->getEchoIf('is_granted(\''.$credentials.'\')');
  128. }
  129. $out = array();
  130. foreach ($matches[2] as $index => $matche) {
  131. if ( $matche == 'or' || $matche == 'and' ) {
  132. $out[$index] = $matche;
  133. } else {
  134. $out[$index] = 'is_granted(\''.$matche.'\')';
  135. }
  136. // Replace parenthesis
  137. $out[$index] = $matches[1][$index].$out[$index].$matches[3][$index];
  138. }
  139. return $this->getEchoIf(implode(' ', $out));
  140. }
  141. public function getEchoIf($condition)
  142. {
  143. return str_replace('%%condition%%', $condition, '{% if %%condition%% %}');
  144. }
  145. public function getEchoElseIf($condition)
  146. {
  147. return str_replace('%%condition%%', $condition, '{% elseif %%condition%% %}');
  148. }
  149. public function getEchoElse()
  150. {
  151. return '{% else %}';
  152. }
  153. public function getEchoEndIf()
  154. {
  155. return '{% endif %}';
  156. }
  157. public function getEchoTwig($str)
  158. {
  159. return sprintf('{{ %s }}', $str);
  160. }
  161. public function getEchoBlock($name)
  162. {
  163. return str_replace('%%name%%', $name, '{% block %%name%% %}');
  164. }
  165. public function getEchoExtends($name)
  166. {
  167. return str_replace('%%name%%', $name, '{% extends "%%name%%" %}');
  168. }
  169. public function getEchoEndBlock()
  170. {
  171. return '{% endblock %}';
  172. }
  173. public function getEchoFor($object, $in)
  174. {
  175. return strtr('{% for %%object%% in %%in%% %}', array('%%object%%' => $object, '%%in%%' => $in ));
  176. }
  177. public function getEchoEndFor()
  178. {
  179. return '{% endfor %}';
  180. }
  181. /**
  182. * Returns the name of the extension.
  183. *
  184. * @return string The extension name
  185. */
  186. public function getName()
  187. {
  188. return 'echo';
  189. }
  190. }