1

I'm trying to find an example in M2 repo where a proxy is used but not explicitly requested in a constructor. Can anyone point me in the right direction?

This is in response to phpcs Magento2 ruleset error

Proxies and interceptors MUST never be explicitly requested in constructors.
1

Proxies are injected via di.xml.

<type name="Your\Class">
    <arguments>
        <argument name="slowLoading" xsi:type="object">SlowLoading\Class\Proxy</argument>
    </arguments>
</type>

Mostly used them in console commands for now. Generally you can use them everywhere you want to delay the instantiation of a class with all its own dependencies until it is directly called upon.

Magento DevDocs

9
1

Here's what an actual working example looks like

https://github.com/justbetter/magento2-sentry/blob/26daa9575a8e9f673aa6102558288ae46a436a60/Plugin/MonologPlugin.php

Before

    /**
     * {@inheritdoc}
     */
    public function __construct(
        $name,
        Data\Proxy $data,
        SentryLog\Proxy $sentryLog,
        DeploymentConfig\Proxy $deploymentConfig,
        array $handlers = [],
        array $processors = []
    ) {
        $this->sentryHelper = $data;
        $this->sentryLog = $sentryLog;
        $this->deploymentConfig = $deploymentConfig;
        parent::__construct($name, $handlers, $processors);
    }

After

use JustBetter\Sentry\Model\SentryLog;
    /**
     * {@inheritdoc}
     */
    public function __construct(
        $name,
        Data $data,
        SentryLog $sentryLog,
        DeploymentConfig $deploymentConfig,
        array $handlers = [],
        array $processors = []
    ) {
        $this->sentryHelper = $data;
        $this->sentryLog = $sentryLog;
        $this->deploymentConfig = $deploymentConfig;
        parent::__construct($name, $handlers, $processors);
    }

etc/di.xml

https://github.com/justbetter/magento2-sentry/blob/8d7adcce685ae1fba38187901dee894002d05c78/etc/di.xml#L14-L23

 <type name="JustBetter\Sentry\Plugin\MonologPlugin">
        <arguments>
            <argument name="name" xsi:type="string"></argument>
            <argument name="data" xsi:type="object">JustBetter\Sentry\Helper\Data\Proxy</argument>
            <argument name="sentryLog" xsi:type="object">JustBetter\Sentry\Model\SentryLog\Proxy</argument>
            <argument name="deploymentConfig" xsi:type="object">Magento\Framework\App\DeploymentConfig\Proxy</argument>
            <argument name="handlers" xsi:type="array"></argument>
            <argument name="processors" xsi:type="array"></argument>
        </arguments>
    </type>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.