mirror of
https://github.com/getgrav/grav.git
synced 2026-07-19 23:31:56 +02:00
Formatters: Better error handling, allow custom file extension
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
* Set minimum requirements to [PHP 5.6.4](https://getgrav.org/blog/raising-php-requirements-2018)
|
||||
* Updated Doctrine Collections to 1.4
|
||||
* Updated Symfony Components to 3.4 (with compatibility mode to fall back to Symfony YAML 2.8)
|
||||
* Added new `Grav\Framework\File\Formatter` classes for encoding/decoding YAML, MarkDown, JSON, INI and PHP serialized formats
|
||||
* Added new `Grav\Framework\File\Formatter` classes for encoding/decoding YAML, Markdown, JSON, INI and PHP serialized strings
|
||||
|
||||
# v1.4.4
|
||||
## 04/12/2018
|
||||
|
||||
@@ -10,6 +10,13 @@ namespace Grav\Framework\Formatter;
|
||||
|
||||
interface FormatterInterface
|
||||
{
|
||||
/**
|
||||
* Get file extension with dot.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFileExtension();
|
||||
|
||||
/**
|
||||
* Encode data into a string.
|
||||
*
|
||||
|
||||
@@ -14,9 +14,26 @@ namespace Grav\Framework\Formatter;
|
||||
*/
|
||||
class IniFormatter implements FormatterInterface
|
||||
{
|
||||
/** @var array */
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* IniFormatter constructor.
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.ini'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return 'ini';
|
||||
return $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,6 +49,7 @@ class IniFormatter implements FormatterInterface
|
||||
$value
|
||||
) . "\"\n";
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
@@ -43,7 +61,7 @@ class IniFormatter implements FormatterInterface
|
||||
$decoded = @parse_ini_string($data);
|
||||
|
||||
if ($decoded === false) {
|
||||
throw new \RuntimeException("Decoding INI format failed'");
|
||||
throw new \RuntimeException('Decoding INI failed');
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
|
||||
@@ -20,14 +20,18 @@ class JsonFormatter implements FormatterInterface
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.json',
|
||||
'encode_options' => 0,
|
||||
'decode_assoc' => true
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return 'json';
|
||||
return $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,10 +39,13 @@ class JsonFormatter implements FormatterInterface
|
||||
*/
|
||||
public function encode($data)
|
||||
{
|
||||
$encoded = json_encode($data, $this->config['encode_options']);
|
||||
$encoded = @json_encode($data, $this->config['encode_options']);
|
||||
|
||||
if ($encoded === false) {
|
||||
throw new \RuntimeException('');
|
||||
throw new \RuntimeException('Encoding JSON failed');
|
||||
}
|
||||
|
||||
return $encoded;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,6 +53,12 @@ class JsonFormatter implements FormatterInterface
|
||||
*/
|
||||
public function decode($data)
|
||||
{
|
||||
return json_decode($data, $this->config['decode_assoc']);
|
||||
$decoded = @json_decode($data, $this->config['decode_assoc']);
|
||||
|
||||
if ($decoded === false) {
|
||||
throw new \RuntimeException('Decoding JSON failed');
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ class MarkdownFormatter implements FormatterInterface
|
||||
public function __construct(array $config = [], FormatterInterface $headerFormatter = null)
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.md',
|
||||
'header' => 'header',
|
||||
'body' => 'markdown',
|
||||
'raw' => 'frontmatter',
|
||||
@@ -31,9 +32,12 @@ class MarkdownFormatter implements FormatterInterface
|
||||
$this->headerFormatter = $headerFormatter ?: new YamlFormatter($this->config['formatter']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return 'md';
|
||||
return $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,9 +10,26 @@ namespace Grav\Framework\Formatter;
|
||||
|
||||
class SerializeFormatter implements FormatterInterface
|
||||
{
|
||||
/** @var array */
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* IniFormatter constructor.
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.ser'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return 'raw';
|
||||
return $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,7 +45,13 @@ class SerializeFormatter implements FormatterInterface
|
||||
*/
|
||||
public function decode($data)
|
||||
{
|
||||
return $this->preserveLines(unserialize($data), ['\\n', '\\r'], ["\n", "\r"]);
|
||||
$decoded = @unserialize($data);
|
||||
|
||||
if ($decoded === false) {
|
||||
throw new \RuntimeException('Decoding serialized data failed');
|
||||
}
|
||||
|
||||
return $this->preserveLines($decoded, ['\\n', '\\r'], ["\n", "\r"]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,7 @@ class YamlFormatter implements FormatterInterface
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.yaml',
|
||||
'inline' => 5,
|
||||
'indent' => 2,
|
||||
'native' => true,
|
||||
@@ -32,9 +33,12 @@ class YamlFormatter implements FormatterInterface
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return 'yaml';
|
||||
return $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,7 +54,7 @@ class YamlFormatter implements FormatterInterface
|
||||
YamlParser::DUMP_EXCEPTION_ON_INVALID_TYPE
|
||||
);
|
||||
} catch (DumpException $e) {
|
||||
throw new \RuntimeException($e->getMessage(), 500, $e);
|
||||
throw new \RuntimeException('Encoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +83,7 @@ class YamlFormatter implements FormatterInterface
|
||||
return (array) FallbackYamlParser::parse($data);
|
||||
}
|
||||
|
||||
throw new \RuntimeException($e->getMessage(), 500, $e);
|
||||
throw new \RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user