diff --git a/CHANGELOG.md b/CHANGELOG.md index f508ae06a9..5148febfeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/system/src/Grav/Framework/File/Formatter/FormatterInterface.php b/system/src/Grav/Framework/File/Formatter/FormatterInterface.php index 55c9b950d3..1fc2afae85 100644 --- a/system/src/Grav/Framework/File/Formatter/FormatterInterface.php +++ b/system/src/Grav/Framework/File/Formatter/FormatterInterface.php @@ -10,6 +10,13 @@ interface FormatterInterface { + /** + * Get file extension with dot. + * + * @return string + */ + public function getFileExtension(); + /** * Encode data into a string. * diff --git a/system/src/Grav/Framework/File/Formatter/IniFormatter.php b/system/src/Grav/Framework/File/Formatter/IniFormatter.php index 38d9014dba..ff6d99fd62 100644 --- a/system/src/Grav/Framework/File/Formatter/IniFormatter.php +++ b/system/src/Grav/Framework/File/Formatter/IniFormatter.php @@ -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']; } /** @@ -32,6 +49,7 @@ public function encode($data) $value ) . "\"\n"; } + return $string; } @@ -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; diff --git a/system/src/Grav/Framework/File/Formatter/JsonFormatter.php b/system/src/Grav/Framework/File/Formatter/JsonFormatter.php index 0808fdea6a..227bd5b8f6 100644 --- a/system/src/Grav/Framework/File/Formatter/JsonFormatter.php +++ b/system/src/Grav/Framework/File/Formatter/JsonFormatter.php @@ -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 @@ public function getFileExtension() */ 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 @@ public function encode($data) */ 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; } } diff --git a/system/src/Grav/Framework/File/Formatter/MarkdownFormatter.php b/system/src/Grav/Framework/File/Formatter/MarkdownFormatter.php index 49e82fc21d..3d5c55b8ff 100644 --- a/system/src/Grav/Framework/File/Formatter/MarkdownFormatter.php +++ b/system/src/Grav/Framework/File/Formatter/MarkdownFormatter.php @@ -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 @@ 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']; } /** diff --git a/system/src/Grav/Framework/File/Formatter/SerializeFormatter.php b/system/src/Grav/Framework/File/Formatter/SerializeFormatter.php index 7e4894b927..1662dfee6e 100644 --- a/system/src/Grav/Framework/File/Formatter/SerializeFormatter.php +++ b/system/src/Grav/Framework/File/Formatter/SerializeFormatter.php @@ -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']; } /** @@ -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"]); } /** diff --git a/system/src/Grav/Framework/File/Formatter/YamlFormatter.php b/system/src/Grav/Framework/File/Formatter/YamlFormatter.php index 662843886b..6c80d99ee2 100644 --- a/system/src/Grav/Framework/File/Formatter/YamlFormatter.php +++ b/system/src/Grav/Framework/File/Formatter/YamlFormatter.php @@ -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 @@ public function __construct(array $config = []) ]; } + /** + * {@inheritdoc} + */ public function getFileExtension() { - return 'yaml'; + return $this->config['file_extension']; } /** @@ -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); } } @@ -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); } } }