What affects Magento's performance when a class consumes a lot of resources?
Here is the solution to the question that is popping up in your mind. It’s all about “Proxy”. Yes! which helps to overcome the problem in Magento.
Hello, all of this is MO Toady we will explain to you what is Proxy. Without killing time let's dive into the topic.
If you need any other class object to be used in your class, instead of instantiating it inside the class, you should inject that object into the constructor and then use it in the class.
Magento’s constructor injection pattern enables you to flexibly manage your class dependencies. However, constructor injection is like a chain reaction of object instantiating and often the result, when you create an object.
The original object has dependencies and so on. If the class’s constructor is particularly resource-consuming, this leads to an impact on the performance of the dependent class.
To stop this chain reaction Proxy had been used.
How to use a proxy in Magento 2?
If you're planning to inject the resource-consuming class into your class, first generate the proxy for them>>(means to inject the class to your class via dependency injection).
Now the question arises what is dependency injection or di?
Dependency injection is used in Magento 2 to replace the Mage class functionality that is used in Magento1.
It’s a pattern in which object 1 can declare its dependencies on object 2, and then object 1 doesn’t have to worry about procuring its dependency requirements. Object 2 will do that based on desired behavior or configurations.
Since the dependency injection is done in the di.xml file you will be having the control that you can change the object lifestyle (constructor params). here is an example.
<type name="Magento\Customer\Model\Session"> <arguments> <argument name="configShare" xsi:type="object"> Magento\Customer\Model\Config\Share\Proxy </arguments> </arguments> </type>
The above entry is from customer model di.xml, as we see here the type of declaration for class “Magento\Customer\Model\Session”, replaces constructor arguments “configShare” with Magento\Customer\Model\Config\Share\Proxy as these classes have the name “Proxy”.
Check in your Magento after installing it. You won’t be able to find it in the codebase and these classes are generated automatically by Magento once you run the Magento compilation code.
i.e., bin/magento setup:di:compile
When you execute the above command, it will check the di.xml files of all the modules installed in magento2 and if it encounters a class names proxy, and if it doesn’t exist then it will be automatically generated with the same namespace inside the generated folder of the magento2 root directory.
What does the Proxy class look like?
Here is an example of how the proxy class looks like:
<?php namespace Magento\Customer\Model\Config\Share; /** * Proxy class for @see \Magento\Customer\Model\Config\Share */ class Proxy extends \Magento\Customer\Model\Config\Share implements \Magento\ Framework\ObjectManager\NoninterceptableInterface { /** * Object Manager instance * * @var \Magento\Framework\ObjectManagerInterface */ protected $_objectManager = null; /** * Proxied instance name * * @var string */ protected $_instanceName = null; /** * Proxied instance * * @var \Magento\Customer\Model\Config\Share */ protected $_subject = null; /** * Instance shareability flag * * @var bool */ protected $_isShared = null; /** * Proxy constructor * * @param \Magento\Framework\ObjectManagerInterface $objectManager * @param string $instanceName * @param bool $shared */ public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\Magento\\Customer\\Model\\Config\\Share', $shared = true) { $this->_objectManager = $objectManager; $this->_instanceName = $instanceName; $this->_isShared = $shared; } /** * @return array */ public function __sleep() { return ['_subject', '_isShared', '_instanceName']; } /** * Retrieve ObjectManager from the global scope */ public function __wakeup() { $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); } /** * Clone proxied instance */ public function __clone() { $this->_subject = clone $this->_getSubject(); } /** * Get a proxied instance * * @return \Magento\Customer\Model\Config\Share */ protected function _getSubject() { if (!$this->_subject) { $this->_subject = true === $this->_isShared ? $this->_objectManager->get($this->_instanceName) : $this->_objectManager->create($this->_instanceName); } return $this->_subject; } /** * {@inheritdoc} */ public function getVisibleStatusIds() { return $this->_getSubject()->getVisibleStatusIds(); } /** * {@inheritdoc} */ public function getSaleableStatusIds() { return $this->_getSubject()->getSaleableStatusIds(); } /** * {@inheritdoc} */ public function getAllOptions() { return $this->_getSubject()->getAllOptions(); } /** * {@inheritdoc} */ public function getOptionText($optionId) { return $this->_getSubject()->getOptionText($optionId); } /** * {@inheritdoc} */ public function addValueSortToCollection($collection, $dir = 'asc') { return $this->_getSubject()->addValueSortToCollection($collection, $dir); } /** * {@inheritdoc} */ public function setAttribute($attribute) { return $this->_getSubject()->setAttribute($attribute); } /** * {@inheritdoc} */ public function getAttribute() { return $this->_getSubject()->getAttribute(); } /** * {@inheritdoc} */ public function getOptionId($value) { return $this->_getSubject()->getOptionId($value); } /** * {@inheritdoc} */ public function getFlatColumns() { return $this->_getSubject()->getFlatColumns(); } /** * {@inheritdoc} */ public function getFlatIndexes() { return $this->_getSubject()->getFlatIndexes(); } /** * {@inheritdoc} */ public function getFlatUpdateSelect($store) { return $this->_getSubject()->getFlatUpdateSelect($store); } /** * {@inheritdoc} */ public function getIndexOptionText($value) { return $this->_getSubject()->getIndexOptionText($value); } /** * {@inheritdoc} */ public function toOptionArray() { return $this->_getSubject()->toOptionArray(); } }
The Proxy class will have the same name as the original class so that it can be easily replaced with the original object and it also extends the original class so that it can use its public methods.
Proxy classes implement “\Magento\Framework\ObjectManager\NoninterceptableInterface”, this is a marker interface that means it is blank, it has no methods and no member variables, and it is used only for signaling that this class cannot be intercepted.
The constructor is having only one object dependency which is the “Object Manager class”.
So this makes sense as there is only one dependency in the class it will load very quickly and will save the resources.
__sleep, __wakeup, and __sleep are the PHP magic functions that execute when a class is invoked or destroyed to perform some actions.
_getSubject This method will return the original class object whenever it is asked for, so it will simply work like a lazy loading process and an object will be only provided when it is requested.
All the other functions are parent(original) class public functions that are overridden in the proxy class so that they can be called on a “proxy class object”, inside these functions it is calling the _getSubject to get the parent object and then call the same function.
Hope I have cleared how to eliminate the resource-consuming class that impacts the performance of Magento using Proxy.
This is an easy way to use and understand the structure. Drop your doubts in the below comment section!
Thank you for Reading. Are you fascinated to know more about Magento, What are you waiting for? Check out the other blogs in the Magento section and follow us through Youtube and Instagram to know more about us.
0 Comments