Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate the JSON View Helper for Removal in v3.0 #237

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion docs/book/v2/migration/preparing-for-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ try {
}
```

## Deprecations
## Deprecations

### Undocumented Behaviour

Expand All @@ -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 `<?= $variable ?>` to `<?php echo $variable ?>` so that templates would continue to be processed in environments where short_open_tag was turned off. Since PHP 5.4.0, `<?=` is always available, therefore the wrapper became mostly unnecessary.

The impact of this future removal will affect templates that use a regular short open tag for general PHP code, i.e. `<? $i = 1; echo $i ?>` 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 `<?php` open tag. Use of the short echo tag `<?=` is unaffected.

## View Helper Changes

### [Json View Helper](../helpers/json.md)

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.
froschdesign marked this conversation as resolved.
Show resolved Hide resolved

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.
13 changes: 13 additions & 0 deletions test/Helper/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
use Laminas\View\Helper\Json as JsonHelper;
use PHPUnit\Framework\TestCase;

use function json_encode;
use function restore_error_handler;
use function set_error_handler;

use const E_USER_DEPRECATED;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;

class JsonTest extends TestCase
{
Expand Down Expand Up @@ -74,4 +77,14 @@ public function testThatADeprecationErrorIsNotTriggeredWhenExpressionFinderOptio
$this->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]));
}
}