Add parsers.use_php_yaml option to decide PHP Yaml extension behavior

This commit is contained in:
Giuseppe Criscione 2018-07-15 17:40:59 +02:00
parent 43a052cd53
commit 26156abc2f
2 changed files with 31 additions and 2 deletions

View File

@ -74,6 +74,7 @@ class Formwork
'content.path' => ROOT_PATH . 'content' . DS,
'content.extension' => '.md',
'files.allowed_extensions' => array('.jpg', '.jpeg', '.png', '.gif', '.svg', '.pdf'),
'parsers.use_php_yaml' => 'parse',
'templates.path' => ROOT_PATH . 'templates' . DS,
'templates.extension' => '.php',
'pages.index' => 'index',

View File

@ -2,14 +2,17 @@
namespace Formwork\Parsers;
use Formwork\Core\Formwork;
use Formwork\Utils\FileSystem;
use Spyc;
class YAML
{
public static $PHPYAMLmode;
public static function parse($input)
{
if (function_exists('yaml_parse')) {
if (function_exists('yaml_parse') && static::PHPYAMLmode('parse')) {
if (!preg_match('/^---\n/', $input)) {
$input = "---\n" . $input;
}
@ -25,7 +28,7 @@ class YAML
public static function encode($data)
{
if (function_exists('yaml_emit')) {
if (function_exists('yaml_emit') && static::PHPYAMLmode('emit')) {
if (empty($data)) {
return '';
}
@ -33,4 +36,29 @@ class YAML
}
return Spyc::YAMLDump($data, false, 0, true);
}
protected static function PHPYAMLmode($pattern)
{
if (is_null(static::$PHPYAMLmode)) {
$option = Formwork::instance()->option('parsers.use_php_yaml');
if ($option) {
switch (strtolower($option)) {
case 'all':
static::$PHPYAMLmode = 'all';
break;
case 'emit':
static::$PHPYAMLmode = 'emit';
break;
case 'parse':
static::$PHPYAMLmode = 'parse';
break;
case 'none':
default:
static::$PHPYAMLmode = false;
break;
}
}
}
return static::$PHPYAMLmode === $pattern || static::$PHPYAMLmode == 'all';
}
}