maj+ header
This commit is contained in:
+71
-15
@@ -3,7 +3,8 @@ namespace RocketTheme\Toolbox\File;
|
||||
|
||||
use Symfony\Component\Yaml\Exception\DumpException;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use \Symfony\Component\Yaml\Yaml as YamlParser;
|
||||
use Symfony\Component\Yaml\Yaml as YamlParser;
|
||||
use RocketTheme\Toolbox\Compat\Yaml\Yaml as FallbackYamlParser;
|
||||
|
||||
/**
|
||||
* Implements YAML File reader.
|
||||
@@ -17,7 +18,27 @@ class YamlFile extends File
|
||||
/**
|
||||
* @var array|File[]
|
||||
*/
|
||||
static protected $instances = array();
|
||||
static protected $instances = [];
|
||||
|
||||
static protected $globalSettings = [
|
||||
'compat' => true,
|
||||
'native' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Set/get settings.
|
||||
*
|
||||
* @param array $settings
|
||||
* @return array
|
||||
*/
|
||||
public static function globalSettings(array $settings = null)
|
||||
{
|
||||
if ($settings !== null) {
|
||||
static::$globalSettings = $settings;
|
||||
}
|
||||
|
||||
return static::$globalSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -29,6 +50,38 @@ class YamlFile extends File
|
||||
$this->extension = '.yaml';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set/get settings.
|
||||
*
|
||||
* @param array $settings
|
||||
* @return array
|
||||
*/
|
||||
public function settings(array $settings = null)
|
||||
{
|
||||
if ($settings !== null) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
return $this->settings + static::$globalSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get setting.
|
||||
*
|
||||
* @param string $setting
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function setting($setting, $default = null)
|
||||
{
|
||||
$value = parent::setting($setting);
|
||||
if (null === $value) {
|
||||
$value = isset(static::$globalSettings[$setting]) ? static::$globalSettings[$setting] : $default;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check contents and make sure it is in correct format.
|
||||
*
|
||||
@@ -43,7 +96,7 @@ class YamlFile extends File
|
||||
/**
|
||||
* Encode contents into RAW string.
|
||||
*
|
||||
* @param string $var
|
||||
* @param array $var
|
||||
* @return string
|
||||
* @throws DumpException
|
||||
*/
|
||||
@@ -61,24 +114,27 @@ class YamlFile extends File
|
||||
*/
|
||||
protected function decode($var)
|
||||
{
|
||||
$data = false;
|
||||
|
||||
// Try native PECL YAML PHP extension first if available.
|
||||
if ($this->setting('native') && function_exists('yaml_parse')) {
|
||||
if ($this->setting('compat', true)) {
|
||||
// Fix illegal @ start character.
|
||||
$data = preg_replace('/ (@[\w\.\-]*)/', " '\${1}'", $var);
|
||||
} else {
|
||||
$data = $var;
|
||||
}
|
||||
|
||||
if ($this->setting('native', true) && function_exists('yaml_parse')) {
|
||||
// Safely decode YAML.
|
||||
$saved = @ini_get('yaml.decode_php');
|
||||
@ini_set('yaml.decode_php', 0);
|
||||
$data = @yaml_parse($data);
|
||||
$data = @yaml_parse($var);
|
||||
@ini_set('yaml.decode_php', $saved);
|
||||
|
||||
if ($data !== false) {
|
||||
return (array) $data;
|
||||
}
|
||||
}
|
||||
|
||||
return $data !== false ? $data : (array) YamlParser::parse($var);
|
||||
try {
|
||||
return (array) YamlParser::parse($var);
|
||||
} catch (ParseException $e) {
|
||||
if ($this->setting('compat', true)) {
|
||||
return (array) FallbackYamlParser::parse($var);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user