Magento 2 Proxies

In Today topic I am going to cover about Magento 2 Proxies, What is Proxy , Why to use proxy in Magento and it’s benefits.

Proxy is one of the important design pattern and feature of magento 2 which can be use to improve your performance and reduce resource consumption if you will use it at right place.

Proxy is used to solve redundant problem in project.

Proxy classes are automatically generated and contain a lazy loaded version of a class. The class contains only one dependency – object manager. This allows the class to be referenced anywhere and not all of the dependencies have to be loaded right away.

We will understand it with one example that how it works and how it helps to improve performance and reduce resources.

Create a Module and create a Cli command as below

<?php
namespace Mageacademy\ArgumentReplacement\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class FirstCommand extends Command
{
    const NAME = 'name';

    protected $a;

    protected $b;

    protected $helperClass;

    public function __construct(
        \Mageacademy\ArgumentReplacement\Helper\HelperClassA $helperClass,
        $a = 7,
        $b = 3,
        string $name = null)
    {
        $this->a = $a;
        $this->b = $b;
        $this->helperClass = $helperClass;
        parent::__construct($name);
    }

    /**
     * @inheritDoc
     */
    protected function configure()
    {
        $this->setName('mageacademy:argumentreplacement:showresult');
        $this->setDescription('Argument Replacement');

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        echo "\n";
        echo "Run";         
    }
}

and create HelperClassA as below

<?php

namespace Mageacademy\ArgumentReplacement\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class HelperClassA extends AbstractHelper
{
    public function __construct(Context $context)
    {
        echo "HelpClassA";
        for($i =1 ; $i<1000000000;$i++) {

        }
        echo "\n";
        echo "end";
        parent::__construct($context);
    }


    public function calculateResult($a,$b)
    {
        return $a + $b;
    }

}

Now enable your module and run compilation command and then run below command

php bin/magento mageacademy:argumentreplacement:showresult

You will see some delay in echo “RUN” to be execute. and you will see echo HelperClassA as well. because when you ran the command it is initiating all class which you have added in constructor of FirstCommand class as dependency injection.

Now add the proxy as below in di.xml

<type name="Mageacademy\ArgumentReplacement\Console\FirstCommand">
        <arguments>
            <argument name="helperClass" xsi:type="object">Mageacademy\ArgumentReplacement\Helper\HelperClassA\Proxy</argument>
        </arguments>
    </type>

Here you can see that using argument replacement feature we have added proxy class of HelperClassA

Actually this Proxy class is not exist in code base but it generates automatically while running compile command.

Now if you will run above command again after clearing cache and compilation then you will see that “RUN” Command executed immediately and there is no delay. and even “HelperClassA” is not displaying now as it is not initiated while running the command.

Now if you will add below code in FirstCommand execute function

        $d = $this->helperClass->calculateResult($this->a,$this->b);

        $output->writeln($this->a .' - '. $this->b.' = '. $d);

and Now if you will run above command then you will see that “RUN” is displaying instantly and after that HelperClassA has been initiated and called it’s calculateResult function.

So Hope you understand that using proxy class is initiated only when it is required and not all the time when FirstCommand class has been initiated.

So by this way, If you feel that class which is injected into another class is consuming lots of heavy logic which is consuming resources and affecting performance , you can add it into proxy class and save your resources.

Now you would be wondering if it is so effective why we are not using it everywhere. then answer is because when we generate proxy class it takes bit of resources and also initiating the object using proxy takes more time than direct initiation.

Hope you like this blog so please share your comment with us.

Keep visiting

Subscribe to our newsletter

Leave a Reply
Your email address will not be published. *