YamlExtension.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Bridge\Twig\Extension;
  11. use Symfony\Component\Yaml\Dumper as YamlDumper;
  12. /**
  13. * Provides integration of the Yaml component with Twig.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class YamlExtension extends \Twig_Extension
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function getFilters()
  23. {
  24. return array(
  25. new \Twig_SimpleFilter('yaml_encode', array($this, 'encode')),
  26. new \Twig_SimpleFilter('yaml_dump', array($this, 'dump')),
  27. );
  28. }
  29. public function encode($input, $inline = 0, $dumpObjects = false)
  30. {
  31. static $dumper;
  32. if (null === $dumper) {
  33. $dumper = new YamlDumper();
  34. }
  35. return $dumper->dump($input, $inline, false, $dumpObjects);
  36. }
  37. public function dump($value, $inline = 0, $dumpObjects = false)
  38. {
  39. if (is_resource($value)) {
  40. return '%Resource%';
  41. }
  42. if (is_array($value) || is_object($value)) {
  43. return '%'.gettype($value).'% '.$this->encode($value, $inline, $dumpObjects);
  44. }
  45. return $this->encode($value, $inline, $dumpObjects);
  46. }
  47. /**
  48. * Returns the name of the extension.
  49. *
  50. * @return string The extension name
  51. */
  52. public function getName()
  53. {
  54. return 'yaml';
  55. }
  56. }