magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.39k stars 9.29k forks source link

Add support for plain objects with promoted properties in message queue communication #36660

Open speller opened 1 year ago

speller commented 1 year ago

Description

Add support for plain objects with promoted properties in message queue communication.

PHP 8 allows to use the following immutable data objects with promoted properties:

class CacheFlushMessage
{
    public function __construct(
        public readonly CacheFlushMessageType $messageType,
        public readonly string $cacheType,
        public readonly AppMode $source,
    ) {
    }
}

This allows to keep the user code clean by using modern language features with less code doing the same as old-styled code with regular constructor properties and getter methods.

But I can not add this object to the communication.xml file as-is:

    <topic name="test" request="CacheFlushMessage"/>

The resulting emitted message body will be empty.

The workaround is to use a custom encoder and markup such classes somehow. For example, use a marker interface:

interface PlainJsonDataInterface
{
}

class CacheFlushMessage implements PlainJsonDataInterface
{
    public function __construct(
        public readonly CacheFlushMessageType $messageType,
        public readonly string $cacheType,
        public readonly AppMode $source,
    ) {
    }
}

class PlainJsonDataProcessor
{
    public function execute(
        PlainJsonDataInterface $dataObject,
        array $result
    ): array {
        return array_merge($result, json_decode(json_encode($dataObject), 'true'));
    }
}
    <type name="Magento\Framework\Reflection\DataObjectProcessor">
        <arguments>
            <argument name="processors" xsi:type="array">
                <item name="PlainJsonDataInterface" xsi:type="object">PlainJsonDataProcessor</item>
            </argument>
        </arguments>
    </type>

Expected behavior

I'm able to use a data class with promoted properties without workarounds.

Benefits

Better user code quality

Additional information

No response

Release note

No response

m2-assistant[bot] commented 1 year ago

Hi @speller. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel