CodeExtension.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\TwigBundle\Extension;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class CodeExtension extends \Twig_Extension
  17. {
  18. private $container;
  19. /**
  20. * Constructor of Twig Extension to provide functions for code formatting
  21. *
  22. * @param Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper $helper Helper to use
  23. */
  24. public function __construct(ContainerInterface $container)
  25. {
  26. $this->container = $container;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getFilters()
  32. {
  33. return array(
  34. 'abbr_class' => new \Twig_Filter_Method($this, 'abbrClass', array('is_safe' => array('html'))),
  35. 'abbr_method' => new \Twig_Filter_Method($this, 'abbrMethod', array('is_safe' => array('html'))),
  36. 'format_args' => new \Twig_Filter_Method($this, 'formatArgs', array('is_safe' => array('html'))),
  37. 'format_args_as_text' => new \Twig_Filter_Method($this, 'formatArgsAsText'),
  38. 'file_excerpt' => new \Twig_Filter_Method($this, 'fileExcerpt', array('is_safe' => array('html'))),
  39. 'format_file' => new \Twig_Filter_Method($this, 'formatFile', array('is_safe' => array('html'))),
  40. 'format_file_from_text' => new \Twig_Filter_Method($this, 'formatFileFromText', array('is_safe' => array('html'))),
  41. 'file_link' => new \Twig_Filter_Method($this, 'getFileLink', array('is_safe' => array('html'))),
  42. );
  43. }
  44. public function abbrClass($class)
  45. {
  46. return $this->container->get('templating.helper.code')->abbrClass($class);
  47. }
  48. public function abbrMethod($method)
  49. {
  50. return $this->container->get('templating.helper.code')->abbrMethod($method);
  51. }
  52. public function formatArgs($args)
  53. {
  54. return $this->container->get('templating.helper.code')->formatArgs($args);
  55. }
  56. public function formatArgsAsText($args)
  57. {
  58. return $this->container->get('templating.helper.code')->formatArgsAsText($args);
  59. }
  60. public function fileExcerpt($file, $line)
  61. {
  62. return $this->container->get('templating.helper.code')->fileExcerpt($file, $line);
  63. }
  64. public function formatFile($file, $line, $text = null)
  65. {
  66. return $this->container->get('templating.helper.code')->formatFile($file, $line, $text);
  67. }
  68. public function getFileLink($file, $line)
  69. {
  70. return $this->container->get('templating.helper.code')->getFileLink($file, $line);
  71. }
  72. public function formatFileFromText($text)
  73. {
  74. return $this->container->get('templating.helper.code')->formatFileFromText($text);
  75. }
  76. public function getName()
  77. {
  78. return 'code';
  79. }
  80. }