Skip to content

Commit

Permalink
Note JSON view helper changes in v3 preparation guide
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Jan 25, 2024
1 parent 53136a5 commit a067c44
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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.

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

0 comments on commit a067c44

Please sign in to comment.