From a067c448bbe7abb3b325952d09519e6eab0ea078 Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 25 Jan 2024 15:12:05 +0000 Subject: [PATCH 1/4] Note JSON view helper changes in v3 preparation guide Signed-off-by: George Steel --- docs/book/v2/migration/preparing-for-v3.md | 12 +++++++++++- test/Helper/JsonTest.php | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/book/v2/migration/preparing-for-v3.md b/docs/book/v2/migration/preparing-for-v3.md index 6787e769c..2ce872bdf 100644 --- a/docs/book/v2/migration/preparing-for-v3.md +++ b/docs/book/v2/migration/preparing-for-v3.md @@ -30,7 +30,7 @@ try { } ``` -## Deprecations +## Deprecations ### Undocumented Behaviour @@ -44,3 +44,13 @@ This deprecation can be safely ignored but in order to prepare for its removal i In version 2, the `TemplatePathStack` template resolver automatically registers a stream wrapper for templates when the php.ini setting `short_open_tag` was turned off. The purpose of the stream wrapper was to convert template files using the short open tag `` to `` so that templates would continue to be processed in environments where short_open_tag was turned off. Since PHP 5.4.0, `` in environments where `short_open_tag` is **off**. To mitigate the impact of this removal, you should ensure that, where relevant, all of your templates use the full `expectNotToPerformAssertions(); $this->helper->__invoke(['foo'], ['enableJsonExprFinder' => 'anything other than true']); } + + public function testTheHelperWillPrettyPrintWhenRequired(): void + { + $input = [ + 'dory' => 'blue', + 'nemo' => 'orange', + ]; + $expect = json_encode($input, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT); + self::assertSame($expect, ($this->helper)->__invoke($input, ['prettyPrint' => true])); + } } From fe4e6ae8d0364679716f1a76798e25ba3503a51f Mon Sep 17 00:00:00 2001 From: George Steel Date: Fri, 26 Jan 2024 09:48:50 +0000 Subject: [PATCH 2/4] Deprecate the JSON view helper entirely Signed-off-by: George Steel --- docs/book/v2/helpers/json.md | 41 +++++++++------------- docs/book/v2/migration/preparing-for-v3.md | 8 ++--- src/Helper/Json.php | 2 ++ test/Helper/JsonTest.php | 5 +++ 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/docs/book/v2/helpers/json.md b/docs/book/v2/helpers/json.md index 4a4056a02..a676cc9c1 100644 --- a/docs/book/v2/helpers/json.md +++ b/docs/book/v2/helpers/json.md @@ -1,5 +1,11 @@ # Json +> WARNING: **Deprecated** +> +> The JSON view helper has been deprecated and will be removed in version 3.0. +> There is no replacement; +> however, it is trivial to encode data by using PHP's built-in [`json_encode`](https://www.php.net/json_encode) function. + When creating views that return JSON, it's important to also set the appropriate response header. The JSON view helper does exactly that. In addition, by default, it disables layouts (if currently enabled), as layouts generally aren't @@ -20,28 +26,13 @@ determine how to handle the content. json($this->data) ?> ``` -> WARNING: **Deprecated** -> -> ### Enabling encoding using Laminas\Json\Expr -> -> **This feature of the Json view helper has been deprecated in version 2.16 and will be removed in version 3.0.** -> -> The JSON helper accepts an array of options that will be passed to `Laminas\Json\Json::encode()` and -> used internally to encode data. -> `Laminas\Json\Json::encode` allows the encoding of native JSON expressions using `Laminas\Json\Expr` -> objects. This option is disabled by default. To enable this option, pass a boolean `true` to the -> `enableJsonExprFinder` key of the options array: -> -> ```php -> json($this->data, ['enableJsonExprFinder' => true]) ?> -> `` -> -> The JSON helper accepts an array of options that will be passed to `Laminas\Json\Json::encode()` and -> used internally to encode data. -> `Laminas\Json\Json::encode` allows the encoding of native JSON expressions using `Laminas\Json\Expr` -> objects. This option is disabled by default. To enable this option, pass a boolean `true` to the -> `enableJsonExprFinder` key of the options array: -> -> ```php -> json($this->data, ['enableJsonExprFinder' => true]) ?> -> ``` +### Enabling encoding using Laminas\Json\Expr + +The JSON helper accepts an array of options that will be passed to `Laminas\Json\Json::encode()` and used internally to encode data. +`Laminas\Json\Json::encode` allows the encoding of native JSON expressions using `Laminas\Json\Expr` objects. +This option is disabled by default. +To enable this option, pass a boolean `true` to the `enableJsonExprFinder` key of the options array: + +```php +json($this->data, ['enableJsonExprFinder' => true]) ?> +``` diff --git a/docs/book/v2/migration/preparing-for-v3.md b/docs/book/v2/migration/preparing-for-v3.md index 2ce872bdf..fed3b39cb 100644 --- a/docs/book/v2/migration/preparing-for-v3.md +++ b/docs/book/v2/migration/preparing-for-v3.md @@ -47,10 +47,8 @@ The impact of this future removal will affect templates that use a regular short ## View Helper Changes -### [Json View Helper](../helpers/json.md) +### Deprecated View Helpers Scheduled for Removal in 3.0 -In version 3, the JSON view helper will no longer set response headers _(For MVC requests)_ when used. -If you are using this feature, you will need to refactor your controllers to return the correct mime-type rather than relying on the view helper to do it for you. +The following view helpers are deprecated and will be removed in version 3.0 of `laminas-view`. -Currently, the [Json View Helper](../helpers/json.md) makes use of the [laminas-json](https://docs.laminas.dev/laminas-json/) library enabling the encoding of [JSON Expressions](https://docs.laminas.dev/laminas-json/advanced/#json-expressions). -Support for JSON expressions is being removed in version 3, so you will need to ensure that you are not using this feature prior to upgrading. +- The [Json View Helper](../helpers/json.md) diff --git a/src/Helper/Json.php b/src/Helper/Json.php index e54a66fe8..ca285d299 100644 --- a/src/Helper/Json.php +++ b/src/Helper/Json.php @@ -14,6 +14,8 @@ /** * Helper for simplifying JSON responses * + * @deprecated This view helper is obsolete and will be removed in version 3.0 + * * @psalm-suppress DeprecatedProperty * @final */ diff --git a/test/Helper/JsonTest.php b/test/Helper/JsonTest.php index b46754d3b..1672a91f8 100644 --- a/test/Helper/JsonTest.php +++ b/test/Helper/JsonTest.php @@ -19,6 +19,11 @@ use const JSON_PRETTY_PRINT; use const JSON_THROW_ON_ERROR; +/** + * @deprecated To be removed with the Json View Helper in v3.0 + * + * @psalm-suppress DeprecatedClass + */ class JsonTest extends TestCase { private Response $response; From 092db0dcdebc264ff6fcfe9ebc0b3a9185ed98d3 Mon Sep 17 00:00:00 2001 From: George Steel Date: Fri, 26 Jan 2024 10:02:06 +0000 Subject: [PATCH 3/4] Baseline deprecation issue Signed-off-by: George Steel --- psalm-baseline.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 12f91ef14..c1d5abb8d 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -104,6 +104,11 @@ vars + + + self + + null === static::$registeredDoctypes From 7577f7dc472ff46a5096662281ac529ea89cb1eb Mon Sep 17 00:00:00 2001 From: George Steel Date: Fri, 26 Jan 2024 11:14:17 +0000 Subject: [PATCH 4/4] Correct markdown callout formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Frank Brückner Signed-off-by: George Steel --- docs/book/v2/helpers/json.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/docs/book/v2/helpers/json.md b/docs/book/v2/helpers/json.md index a676cc9c1..455554cf3 100644 --- a/docs/book/v2/helpers/json.md +++ b/docs/book/v2/helpers/json.md @@ -1,10 +1,8 @@ # Json -> WARNING: **Deprecated** -> -> The JSON view helper has been deprecated and will be removed in version 3.0. -> There is no replacement; -> however, it is trivial to encode data by using PHP's built-in [`json_encode`](https://www.php.net/json_encode) function. +WARNING: **Deprecated** +The JSON view helper has been deprecated and will be removed in version 3.0. +There is no replacement; however, it is trivial to encode data by using PHP's built-in [`json_encode`](https://www.php.net/json_encode) function. When creating views that return JSON, it's important to also set the appropriate response header. The JSON view helper does exactly that. In addition, by