Uploaded image for project: 'PHP Driver: Library'
  1. PHP Driver: Library
  2. PHPLIB-1309

Add addSubscriber and removeSubscriber methods to the Client class to ease dependency injection configuration

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 1.18.0
    • Affects Version/s: None
    • Component/s: None

      In order to add a subscriber when creating a client, it is currently required to make call a method of the manager that is accessible through the client.

       

      $client = new MongoDB\Client();
      $client->getManager()->addSubscriber($subscriber);

      This is difficult to use with Symfony Dependency Injection as we have to extend the Client class or use a service Factory.

       

      Proposition: Add an option "subscriber" to the $driverOptions. It can be a Subscriber or a list<Subscriber>

       

      if (isset($driverOptions['subscriber'])) {
          if ($driverOptions['subscriber'] instanceof Subscriber) {
              $this->manager->addSubscriber($driverOptions['subscriber']);
          } elseif (! is_array($driverOptions['subscriber'])) {
              throw InvalidArgumentException::invalidType('"subscriber" driver option', $driverOptions['subscriber'], Subscriber::class);
          } else {
              foreach ($driverOptions['subscriber'] as $subscriber) {
                  if (! $subscriber instanceof Subscriber) {
                      throw InvalidArgumentException::invalidType('"subscriber" driver option', $subscriber, Subscriber::class);
                  }
      
                  $this->manager->addSubscriber($subscriber);
              }
          }
          unset($driverOptions['subscriber']);
      } 

       

      Alternatively, add proxy methods to the Client class:

       

      public function addSubscriber(Subscriber $subscriber): void
      {
          $this->manager->addSubscriber($subscriber);
      }
      
      public function removeSubscriber(Subscriber $subscriber): void
      {
          $this->manager->removeSubscriber($subscriber);
      }

      That could be used with a simple call when declaring the MongoDB\Client service in Symfony.

       

            Assignee:
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            Reporter:
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: