Skip to content

Commit

Permalink
Formatters: Better error handling, allow custom file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Apr 24, 2018
1 parent 895e145 commit d2e700e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

interface FormatterInterface
{
/**
* Get file extension with dot.
*
* @return string
*/
public function getFileExtension();

/**
* Encode data into a string.
*
Expand Down
22 changes: 20 additions & 2 deletions system/src/Grav/Framework/File/Formatter/IniFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,26 @@
*/
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'];
}

/**
Expand All @@ -32,6 +49,7 @@ public function encode($data)
$value
) . "\"\n";
}

return $string;
}

Expand All @@ -43,7 +61,7 @@ public function decode($data)
$decoded = @parse_ini_string($data);

if ($decoded === false) {
throw new \RuntimeException("Decoding INI format failed'");
throw new \RuntimeException('Decoding INI failed');
}

return $decoded;
Expand Down
21 changes: 17 additions & 4 deletions system/src/Grav/Framework/File/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,45 @@ 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'];
}

/**
* {@inheritdoc}
*/
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;
}

/**
* {@inheritdoc}
*/
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -31,9 +32,12 @@ public function __construct(array $config = [], FormatterInterface $headerFormat
$this->headerFormatter = $headerFormatter ?: new YamlFormatter($this->config['formatter']);
}

/**
* {@inheritdoc}
*/
public function getFileExtension()
{
return 'md';
return $this->config['file_extension'];
}

/**
Expand Down
27 changes: 25 additions & 2 deletions system/src/Grav/Framework/File/Formatter/SerializeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@

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'];
}

/**
Expand All @@ -28,7 +45,13 @@ public function encode($data)
*/
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"]);
}

/**
Expand Down
10 changes: 7 additions & 3 deletions system/src/Grav/Framework/File/Formatter/YamlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ class YamlFormatter implements FormatterInterface
public function __construct(array $config = [])
{
$this->config = $config + [
'file_extension' => '.yaml',
'inline' => 5,
'indent' => 2,
'native' => true,
'compat' => true
];
}

/**
* {@inheritdoc}
*/
public function getFileExtension()
{
return 'yaml';
return $this->config['file_extension'];
}

/**
Expand All @@ -50,7 +54,7 @@ public function encode($data)
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);
}
}

Expand Down Expand Up @@ -79,7 +83,7 @@ public function decode($data)
return (array) FallbackYamlParser::parse($data);
}

throw new \RuntimeException($e->getMessage(), 500, $e);
throw new \RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
}
}
}

0 comments on commit d2e700e

Please sign in to comment.