-
Notifications
You must be signed in to change notification settings - Fork 21
Add DTO (Data Transfer Object) support to Queue plugin #192
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
Open
skie
wants to merge
8
commits into
cakephp:3.x
Choose a base branch
from
skie:feature/dto-support
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2cdb396
Add DTO (Data Transfer Object) support to Queue plugin
skie 8b8b17b
apply rector, fix ci lowest, bump ramsey/uuid
skie 9522b57
fix job template to match rector rules
skie dc4fbd8
bump ramsey/uuid
skie ba6c91d
remove final for DtoManager
skie b01bee3
Require expected class in Message::getDto()
skie 0a72800
Add typed docblocks and asserts.
skie 111ae42
Update src/Dto/DtoManager.php
skie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | ||
| * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/) | ||
| * | ||
| * Licensed under The MIT License | ||
| * For full copyright and license information, please see the LICENSE.txt | ||
| * Redistributions of files must retain the above copyright notice. | ||
| * | ||
| * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/) | ||
| * @link https://cakephp.org CakePHP(tm) Project | ||
| * @since 3.0.0 | ||
| * @license https://opensource.org/licenses/MIT MIT License | ||
| */ | ||
| namespace Cake\Queue\Dto; | ||
|
|
||
| use Cake\ORM\ResultSetFactory; | ||
| use InvalidArgumentException; | ||
| use JsonSerializable; | ||
|
|
||
| /** | ||
| * Serializes DTO objects for queue transport and hydrates them back on the | ||
| * receiving side. | ||
| * | ||
| * Hydration reuses the CakePHP 5.4 DTO support via `ResultSetFactory::getDtoHydrator()`, | ||
| * which handles both a static `createFromArray($data, $nested)` factory method | ||
| * (cakephp-dto style) and plain DTOs mapped through `Cake\ORM\DtoMapper` (constructor | ||
| * parameters, nested DTO type-hints and the `#[CollectionOf]` attribute). | ||
| */ | ||
| class DtoManager | ||
| { | ||
| /** | ||
| * Serialize a DTO (or array) into an array suitable for queue transport. | ||
| * | ||
| * Nested objects are converted to arrays so the resulting data only contains | ||
| * JSON-encodable scalars. | ||
| * | ||
| * @param array<string, mixed>|object $data Data or DTO object to serialize. | ||
| * @return array<string, mixed> Serialized data. | ||
| */ | ||
| public static function serialize(array|object $data): array | ||
| { | ||
| if ($data instanceof JsonSerializable) { | ||
| $data = $data->jsonSerialize(); | ||
| } | ||
|
|
||
| if (is_object($data)) { | ||
| $data = get_object_vars($data); | ||
| } | ||
|
|
||
| if (!is_array($data)) { | ||
| throw new InvalidArgumentException( | ||
| 'DTO data could not be serialized into an array. `jsonSerialize()` must return an array or object.', | ||
| ); | ||
| } | ||
|
|
||
| return self::toScalarArray($data); | ||
| } | ||
|
|
||
| /** | ||
| * Hydrate queue data back into a DTO instance. | ||
| * | ||
|
skie marked this conversation as resolved.
|
||
| * This method is not safe to use with user-defined `dtoClass` values. | ||
| * @template T of object | ||
| * @param array<string, mixed> $data Serialized data. | ||
| * @param class-string<T> $dtoClass DTO class name. | ||
| * @return T Hydrated DTO instance. | ||
| * @throws \InvalidArgumentException When the DTO class does not exist. | ||
| */ | ||
| public static function deserialize(array $data, string $dtoClass): object | ||
| { | ||
| if (!class_exists($dtoClass)) { | ||
| throw new InvalidArgumentException(sprintf('DTO class `%s` does not exist.', $dtoClass)); | ||
| } | ||
|
|
||
| $dto = (new ResultSetFactory())->hydrateDto($data, $dtoClass); | ||
| assert($dto instanceof $dtoClass); | ||
|
|
||
| return $dto; | ||
| } | ||
|
|
||
| /** | ||
| * Recursively convert any nested objects into arrays. | ||
| * | ||
| * @param array<string, mixed> $data The data to convert. | ||
| * @return array<string, mixed> The converted data. | ||
| */ | ||
| protected static function toScalarArray(array $data): array | ||
| { | ||
| foreach ($data as $key => $value) { | ||
| if (is_object($value)) { | ||
| $value = $value instanceof JsonSerializable | ||
| ? $value->jsonSerialize() | ||
| : get_object_vars($value); | ||
| } | ||
|
|
||
| if (is_array($value)) { | ||
| $data[$key] = self::toScalarArray($value); | ||
| } | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.