equal
deleted
inserted
replaced
|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Bridge\Twig\Extension; |
|
13 |
|
14 use Symfony\Component\Yaml\Dumper as YamlDumper; |
|
15 |
|
16 /** |
|
17 * Provides integration of the Yaml component with Twig. |
|
18 * |
|
19 * @author Fabien Potencier <fabien@symfony.com> |
|
20 */ |
|
21 class YamlExtension extends \Twig_Extension |
|
22 { |
|
23 /** |
|
24 * {@inheritdoc} |
|
25 */ |
|
26 public function getFilters() |
|
27 { |
|
28 return array( |
|
29 'yaml_encode' => new \Twig_Filter_Method($this, 'encode'), |
|
30 'yaml_dump' => new \Twig_Filter_Method($this, 'dump'), |
|
31 ); |
|
32 } |
|
33 |
|
34 public function encode($input, $inline = 0) |
|
35 { |
|
36 static $dumper; |
|
37 |
|
38 if (null === $dumper) { |
|
39 $dumper = new YamlDumper(); |
|
40 } |
|
41 |
|
42 return $dumper->dump($input, $inline); |
|
43 } |
|
44 |
|
45 public function dump($value) |
|
46 { |
|
47 if (is_resource($value)) { |
|
48 return '%Resource%'; |
|
49 } |
|
50 |
|
51 if (is_array($value) || is_object($value)) { |
|
52 return '%'.gettype($value).'% '.$this->encode($value); |
|
53 } |
|
54 |
|
55 return $value; |
|
56 } |
|
57 |
|
58 /** |
|
59 * Returns the name of the extension. |
|
60 * |
|
61 * @return string The extension name |
|
62 */ |
|
63 public function getName() |
|
64 { |
|
65 return 'yaml'; |
|
66 } |
|
67 } |