moved integration between the Yaml component and Twig to a Symfony Bridge

This commit is contained in:
Fabien Potencier 2011-03-23 15:50:55 +01:00
parent e912b347f0
commit 82dec51b30
5 changed files with 73 additions and 29 deletions

View File

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Twig\Extension;
use Symfony\Component\Yaml\Dumper as YamlDumper;
/**
* Provides integration of the Yaml component with Twig.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class YamlExtension extends \Twig_Extension
{
/**
* {@inheritdoc}
*/
public function getFilters()
{
return array(
'yaml_encode' => new \Twig_Filter_Method($this, 'encode'),
'yaml_dump' => new \Twig_Filter_Method($this, 'dump'),
);
}
public function encode($input, $inline = 0)
{
static $dumper;
if (null === $dumper) {
$dumper = new YamlDumper();
}
return $dumper->dump($input, $inline);
}
public function dump($value)
{
if (is_resource($value)) {
return '%Resource%';
}
if (is_array($value) || is_object($value)) {
return '%'.gettype($value).'% '.$this->encode($value);
}
return $value;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'yaml';
}
}

View File

@ -14,7 +14,6 @@ namespace Symfony\Bundle\TwigBundle\Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\TokenParser\IncludeTokenParser;
use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
use Symfony\Component\Yaml\Dumper as YamlDumper;
/**
*
@ -45,8 +44,6 @@ class TemplatingExtension extends \Twig_Extension
public function getFilters()
{
return array(
'yaml_encode' => new \Twig_Filter_Method($this, 'yamlEncode'),
'dump' => new \Twig_Filter_Method($this, 'dump'),
'abbr_class' => new \Twig_Filter_Method($this, 'abbrClass', array('is_safe' => array('html'))),
'abbr_method' => new \Twig_Filter_Method($this, 'abbrMethod', array('is_safe' => array('html'))),
'format_args' => new \Twig_Filter_Method($this, 'formatArgs', array('is_safe' => array('html'))),
@ -111,17 +108,6 @@ class TemplatingExtension extends \Twig_Extension
);
}
public function yamlEncode($input, $inline = 0)
{
static $dumper;
if (null === $dumper) {
$dumper = new YamlDumper();
}
return $dumper->dump($input, $inline);
}
public function abbrClass($class)
{
return $this->container->get('templating.helper.code')->abbrClass($class);
@ -162,19 +148,6 @@ class TemplatingExtension extends \Twig_Extension
return $this->container->get('templating.helper.code')->formatFileFromText($text);
}
public function dump($value)
{
if (is_resource($value)) {
return '%Resource%';
}
if (is_array($value) || is_object($value)) {
return '%'.gettype($value).'% '.$this->yamlEncode($value);
}
return $value;
}
/**
* Returns the name of the extension.
*

View File

@ -48,6 +48,10 @@
<argument type="service" id="router" />
</service>
<service id="twig.extension.yaml" class="Symfony\Bridge\Twig\Extension\YamlExtension" public="false">
<tag name="twig.extension" />
</service>
<service id="twig.extension.form" class="Symfony\Bundle\TwigBundle\Extension\FormExtension" public="false">
<tag name="twig.extension" />
<argument>%twig.form.resources%</argument>

View File

@ -99,7 +99,7 @@
{% for key, value in collector.sessionattributes %}
<tr>
<th>{{ key }}</th>
<td>{{ value|dump }}</td>
<td>{{ value|yaml_dump }}</td>
</tr>
{% endfor %}
</table>

View File

@ -6,7 +6,7 @@
{% for key in bag.keys %}
<tr>
<th>{{ key }}</th>
<td>{{ bag.get(key)|dump }}</td>
<td>{{ bag.get(key)|yaml_dump }}</td>
</tr>
{% endfor %}
</table>