2011-03-23 14:50:55 +00:00
|
|
|
<?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;
|
2016-02-12 16:33:32 +00:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2011-03-23 14:50:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
2013-08-05 21:49:39 +01:00
|
|
|
new \Twig_SimpleFilter('yaml_encode', array($this, 'encode')),
|
|
|
|
new \Twig_SimpleFilter('yaml_dump', array($this, 'dump')),
|
2011-03-23 14:50:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
|
|
|
public function encode($input, $inline = 0, $dumpObjects = false)
|
2011-03-23 14:50:55 +00:00
|
|
|
{
|
|
|
|
static $dumper;
|
|
|
|
|
|
|
|
if (null === $dumper) {
|
|
|
|
$dumper = new YamlDumper();
|
|
|
|
}
|
|
|
|
|
2016-02-05 09:56:37 +00:00
|
|
|
if (defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT')) {
|
2016-02-12 16:33:32 +00:00
|
|
|
return $dumper->dump($input, $inline, 0, is_bool($dumpObjects) ? Yaml::DUMP_OBJECT : 0);
|
2016-02-05 09:56:37 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:07:13 +01:00
|
|
|
return $dumper->dump($input, $inline, 0, false, $dumpObjects);
|
2011-03-23 14:50:55 +00:00
|
|
|
}
|
|
|
|
|
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
|
|
|
public function dump($value, $inline = 0, $dumpObjects = false)
|
2011-03-23 14:50:55 +00:00
|
|
|
{
|
|
|
|
if (is_resource($value)) {
|
|
|
|
return '%Resource%';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($value) || is_object($value)) {
|
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
|
|
|
return '%'.gettype($value).'% '.$this->encode($value, $inline, $dumpObjects);
|
2011-03-23 14:50:55 +00:00
|
|
|
}
|
|
|
|
|
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
|
|
|
return $this->encode($value, $inline, $dumpObjects);
|
2011-03-23 14:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-31 14:09:45 +01:00
|
|
|
* {@inheritdoc}
|
2011-03-23 14:50:55 +00:00
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'yaml';
|
|
|
|
}
|
|
|
|
}
|