0

I need to create plugin for this below file :

vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php

There are __call function in this file :

public function __call($name, $arguments) {
        if (isset($this->registeredNamespaces[$name])) {
            return $this->registeredNamespaces[$name];
        }
        throw new BadMethodCallException("Namespace [$name] not found");
    }

I need to create plugin of this function.

What I tried :

In frontend/di.xml I added this below line :

<type name="Elasticsearch\Client">
      <plugin name="Vendor_Module::elasticsearcherrorsolver"
                type="Vendor\Module\Plugins\Elasticsearch\Client"/>
</type>

Vendor\Module\Plugins\Elasticsearch\Client.php

<?php

namespace Vendor\Module\Plugins\Elasticsearch;

class Client {

    public function around__call(\Elasticsearch\Client $subject, callable $proceed, $name, $arguments) {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/Rohan.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Your text message');
        $result = $proceed($name, $arguments);
        // if (!isset($this->registeredNamespaces[$name])) {
        //  return true;
        // }
        return $result;
    }
}

But, It's not working. What should I need to change to call this plugin?

Any help would be appreciated !!

2

Your plugin is correct but it is not picked up by the magento interception system because the Elasticsearch Client class is not instantiated via ObjectManager. it is instantiated using the good old new Client(...) approach.

See it in Elasticsearch\ClientBuilder::instantiate().

Plugins work only for classes that go through the object manager to get instantiated.
for the same reason you cannot pluginize the Phrase class, the one that translates texts when using __('some thext here').

| improve this answer | |
  • okay. Is there any other way? I want to bypass throw new BadMethodCallException("Namespace [$name] not found"); this exception and want to pass as return true. – Rohan Hapani Feb 20 at 9:11
  • Btw, +1 from mine. Let me try do that. Some other way. I don't know. But, I can't override file too. Is it not possible? – Rohan Hapani Feb 21 at 6:21
  • not that I know of. – Marius Feb 21 at 6:51

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.