Skip to content

Commit

Permalink
Require ext/json
Browse files Browse the repository at this point in the history
Instead of optionally requiring ext/json and/or zend-json, just require
ext/json. This simplifies internal logic tremendously, and allows
removal of a custom exception.
  • Loading branch information
weierophinney committed Feb 16, 2017
1 parent 8a23270 commit 1567bf0
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 124 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
},
"require": {
"php": "^5.6 || ^7.0",
"ext-json": "*",
"zendframework/zend-stdlib": "^2.7.7 || ^3.1",
"psr/container": "^1.0"
},
"require-dev": {
"zendframework/zend-coding-standard": "~1.0.0",
"zendframework/zend-filter": "^2.7.1",
"zendframework/zend-i18n": "^2.7.3",
"zendframework/zend-json": "^2.6.1 || ^3.0",
"zendframework/zend-servicemanager": "^2.7.8 || ^3.2.1",
"malukenho/docheader": "^0.1.5",
"phpunit/phpunit": "^5.7 || ^6.0"
Expand All @@ -32,7 +32,6 @@
"suggest": {
"zendframework/zend-filter": "^2.7.1; install if you want to use the Filter processor",
"zendframework/zend-i18n": "^2.7.3; install if you want to use the Translator processor",
"zendframework/zend-json": "^2.6.1 || ^3.0; install if ext/json is not present, and you want to use the JSON reader or writer",
"zendframework/zend-servicemanager": "^2.7.8 || ^3.2.1; if you need an extensible plugin manager for use with the Config Factory"
},
"minimum-stability": "dev",
Expand Down
55 changes: 3 additions & 52 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions src/Exception/MissingExtensionException.php

This file was deleted.

45 changes: 0 additions & 45 deletions src/Reader/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
namespace Zend\Config\Reader;

use Zend\Config\Exception;
use Zend\Json\Exception as JsonException;
use Zend\Json\Json as JsonFormat;

/**
* JSON config reader.
Expand Down Expand Up @@ -103,35 +101,8 @@ protected function process(array $data)
* @param string $data
* @return array
* @throws Exception\RuntimeException for any decoding errors.
* @throws Exception\MissingExtensionException if neither ext/json nor
* zend-json are available.
*/
private function decode($data)
{
if (function_exists('json_decode')) {
return $this->decodeViaExtension($data);
}

if (class_exists(JsonFormat::class)) {
return $this->decodeViaJsonComponent($data);
}

throw new Exception\MissingExtensionException(
'Cannot decode JSON config: missing ext/json. Compile PHP with '
. 'ext/json, or install zendframework/zend-json'
);
}

/**
* Decode the JSON data via ext/json
*
* @param string $data
* @return array
* @throws Exception\RuntimeException if a non-array/non-object was returned
* by the configuration data.
* @throws Exception\RuntimeException if an error occured during decoding.
*/
private function decodeViaExtension($data)
{
$config = json_decode($data, true);

Expand All @@ -151,20 +122,4 @@ private function decodeViaExtension($data)

throw new Exception\RuntimeException(json_last_error_msg());
}

/**
* Use zend-json to decode the configuration data.
*
* @param string $data
* @return array
* @throws Exception\RuntimeException if unable to decode.
*/
private function decodeViaJsonComponent($data)
{
try {
return JsonFormat::decode($data, JsonFormat::TYPE_ARRAY);
} catch (JsonException\RuntimeException $e) {
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}
}
16 changes: 5 additions & 11 deletions src/Writer/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@ class Json extends AbstractWriter
*
* @param array $config
* @return string
* @throws Exception\MissingExtensionException if neither ext/json nor
* zendframework/zend-json are available.
* @throws Exception\RuntimeException if encoding errors occur.
*/
public function processConfig(array $config)
{
if (function_exists('json_encode')) {
return json_encode($config);
}
$serialized = json_encode($config, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

if (class_exists(JsonFormat::class)) {
return JsonFormat::encode($config);
if (false === $serialized) {
throw new Exception\RuntimeException(json_last_error_msg());
}

throw new Exception\MissingExtensionException(
'Cannot write JSON config: missing ext/json. Compile PHP with '
. 'ext/json, or install zendframework/zend-json'
);
return $serialized;
}
}

0 comments on commit 1567bf0

Please sign in to comment.