From 2d7129df7a29c36da0914207c989c04f080007f4 Mon Sep 17 00:00:00 2001 From: Pablo Godel Date: Thu, 18 Dec 2014 09:14:23 -0500 Subject: [PATCH 1/2] Reuse binded connection to improve performance --- Manager/LdapConnection.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Manager/LdapConnection.php b/Manager/LdapConnection.php index d02f84f..c5ba59e 100644 --- a/Manager/LdapConnection.php +++ b/Manager/LdapConnection.php @@ -13,6 +13,12 @@ class LdapConnection implements LdapConnectionInterface protected $ress; + /** + * Holds a list of resources per username (username=>resource) to allow for reusing binded connections. + * @var array + */ + protected $resources = []; + public function __construct(array $params, Logger $logger) { $this->params = $params; @@ -64,7 +70,7 @@ public function search(array $params) /** * @return true - * @throws \IMAG\LdapBundle\Exceptions\ConnectionException | Connection error + * @throws \IMAG\LdapBundle\Exception\ConnectionException | Connection error */ public function bind($user_dn, $password = '', $ress = null) { @@ -128,6 +134,16 @@ private function connect() ? $this->params['client']['port'] : '389'; + // check if there is a binded connection for this username, if there is, set $this->ress and return without + // doing it again. + $username = $this->params['client']['username']; + if (isset($this->params['client']['username'])) { + if (isset($this->resources[$username])) { + $this->ress = $this->resources[$username]; + return $this; + } + } + $ress = @ldap_connect($this->params['client']['host'], $port); if (isset($this->params['client']['version'])) { @@ -151,6 +167,8 @@ private function connect() $this->checkLdapError($ress); } + // store resource per username to be reused later. + $this->resources[$username] = $ress; $this->ress = $ress; return $this; From 39999ecac5d5aa280d6b1ed8e90bbba629edaa79 Mon Sep 17 00:00:00 2001 From: Pablo Godel Date: Thu, 18 Dec 2014 11:00:00 -0500 Subject: [PATCH 2/2] Use array() for BC with older versions of PHP --- Manager/LdapConnection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Manager/LdapConnection.php b/Manager/LdapConnection.php index c5ba59e..02f1b4c 100644 --- a/Manager/LdapConnection.php +++ b/Manager/LdapConnection.php @@ -17,7 +17,7 @@ class LdapConnection implements LdapConnectionInterface * Holds a list of resources per username (username=>resource) to allow for reusing binded connections. * @var array */ - protected $resources = []; + protected $resources = array(); public function __construct(array $params, Logger $logger) {