3 namespace Acme\DemoBundle\Twig\Extension
;
5 use CG\Core\ClassUtils
;
7 class DemoExtension
extends \Twig_Extension
10 protected $controller;
12 public function __construct(\Twig_LoaderInterface
$loader)
14 $this->loader
= $loader;
17 public function setController($controller)
19 $this->controller
= $controller;
25 public function getFunctions()
28 new \
Twig_SimpleFunction('code', array($this, 'getCode'), array('is_safe' => array('html'))),
32 public function getCode($template)
34 // highlight_string highlights php code only if '<?php' tag is present.
35 $controller = highlight_string("<?php".$this->getControllerCode(), true);
36 $controller = str_replace('<span style="color: #0000BB"><?php </span>', ' ', $controller);
38 $template = htmlspecialchars($this->getTemplateCode($template), ENT_QUOTES
, 'UTF-8');
40 // remove the code block
41 $template = str_replace('{% set code = code(_self) %}', '', $template);
44 <p><strong>Controller Code</strong></p>
45 <pre>$controller</pre>
47 <p><strong>Template Code</strong></p>
52 protected function getControllerCode()
54 $class = get_class($this->controller
[0]);
55 if (class_exists('CG\Core\ClassUtils')) {
56 $class = ClassUtils
::getUserClass($class);
59 $r = new \
ReflectionClass($class);
60 $m = $r->getMethod($this->controller
[1]);
62 $code = file($r->getFilename());
64 return ' '.$m->getDocComment()."\n".implode('', array_slice($code, $m->getStartline() - 1, $m->getEndLine() - $m->getStartline() +
1));
67 protected function getTemplateCode($template)
69 return $this->loader
->getSource($template->getTemplateName());
73 * Returns the name of the extension.
75 * @return string The extension name
77 public function getName()