-
Notifications
You must be signed in to change notification settings - Fork 4
Adds beforeSerialize and afterSerialize events #156
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
base: master
Are you sure you want to change the base?
Changes from all commits
f1fc490
49751d8
9f35307
360beb6
a7296ae
01d450e
957e78e
5d0043f
81c817f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,16 +2,16 @@ | |||||||||
|
|
||||||||||
| namespace MixerApi\CollectionView\Test\TestCase; | ||||||||||
|
|
||||||||||
| use Cake\Controller\ComponentRegistry; | ||||||||||
| use Cake\Datasource\FactoryLocator; | ||||||||||
| use Cake\Datasource\Paging\PaginatedResultSet; | ||||||||||
| use Cake\Event\EventList; | ||||||||||
| use Cake\Event\EventManager; | ||||||||||
| use Cake\Http\Response; | ||||||||||
| use Cake\Http\ServerRequest; | ||||||||||
| use Cake\Routing\RouteBuilder; | ||||||||||
| use Cake\Routing\Router; | ||||||||||
| use Cake\TestSuite\TestCase; | ||||||||||
| use Cake\View\Helper\PaginatorHelper; | ||||||||||
| use Cake\View\View; | ||||||||||
| use MixerApi\CollectionView\Configuration; | ||||||||||
| use MixerApi\CollectionView\View\JsonCollectionView; | ||||||||||
| use MixerApi\CollectionView\Serializer; | ||||||||||
|
|
@@ -20,7 +20,7 @@ | |||||||||
| class SerializerTest extends TestCase | ||||||||||
| { | ||||||||||
| /** | ||||||||||
| * @var string[] | ||||||||||
| * @inheritdoc | ||||||||||
| */ | ||||||||||
| public array $fixtures = [ | ||||||||||
| 'plugin.MixerApi/CollectionView.Actors', | ||||||||||
|
|
@@ -34,6 +34,8 @@ public function setUp(): void | |||||||||
|
|
||||||||||
| public function test_as_json(): void | ||||||||||
| { | ||||||||||
| EventManager::instance()->trackEvents(true)->setEventList(new EventList()); | ||||||||||
|
||||||||||
| EventManager::instance()->trackEvents(true)->setEventList(new EventList()); | |
| $eventManager = EventManager::instance(); | |
| $eventManager->trackEvents(true); | |
| $eventManager->setEventList(new EventList()); |
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EventManager::trackEvents() returns a boolean flag and isn't chainable; ->setEventList(...) will fail at runtime here as well. Use separate calls to enable tracking and then set the EventList.
| EventManager::instance()->trackEvents(true)->setEventList(new EventList()); | |
| $eventManager = EventManager::instance(); | |
| $eventManager->trackEvents(true); | |
| $eventManager->setEventList(new EventList()); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -245,3 +245,36 @@ use Cake\Http\ServerRequest; | |||||
| use Cake\View\Helper\PaginatorHelper; | ||||||
| $json = (new JsonSerializer($data, new ServerRequest(), new PaginatorHelper()))->asJson(); | ||||||
| ``` | ||||||
|
|
||||||
| ## Events | ||||||
|
|
||||||
| HalView dispatches two events. Read the CakePHP docs for how to | ||||||
| [register listeners](https://book.cakephp.org/5/en/core-libraries/events.html#registering-listeners). | ||||||
|
|
||||||
| ### MixerApi.HalView.beforeSerialize | ||||||
|
|
||||||
| The event contains the Serializer as the subject and is dispatched just before serialization. | ||||||
|
|
||||||
| ```php | ||||||
| use \Cake\Event\Event; | ||||||
| use \Cake\Event\EventManager; | ||||||
| use \MixerApi\HalView\JsonSerializer; | ||||||
|
|
||||||
| EventManager::instance()->on(JsonSerializer::BEFORE_SERIALIZE_EVENT, function (Event $event) { | ||||||
| /** @var Serializer $serializer */ | ||||||
|
||||||
| /** @var Serializer $serializer */ | |
| /** @var JsonSerializer $serializer */ |
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example listener registration is missing a terminating semicolon after the EventManager::instance()->on(...) call ();), so it’s not valid PHP as written.
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This afterSerialize listener example is missing a terminating semicolon after the EventManager::instance()->on(...) call, so the snippet is invalid PHP.
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |||||||||
|
|
||||||||||
| use Cake\Datasource\FactoryLocator; | ||||||||||
| use Cake\Datasource\Paging\PaginatedResultSet; | ||||||||||
| use Cake\Event\EventList; | ||||||||||
| use Cake\Event\EventManager; | ||||||||||
| use Cake\Http\Response; | ||||||||||
| use Cake\Http\ServerRequest; | ||||||||||
| use Cake\ORM\Table; | ||||||||||
|
|
@@ -71,6 +73,8 @@ public function setUp(): void | |||||||||
|
|
||||||||||
| public function test_collection(): void | ||||||||||
| { | ||||||||||
| EventManager::instance()->trackEvents(true)->setEventList(new EventList()); | ||||||||||
|
||||||||||
| EventManager::instance()->trackEvents(true)->setEventList(new EventList()); | |
| $eventManager = EventManager::instance(); | |
| $eventManager->trackEvents(true); | |
| $eventManager->setEventList(new EventList()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example listener registration is missing a terminating semicolon after the
EventManager::instance()->on(...)call ();), so the snippet is invalid PHP as written.