From ea619fc1b9d461755888afd6bff686b87adcb01b Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Wed, 17 Feb 2010 21:55:36 +0100 Subject: [PATCH] Convert to exceptions, use type hinting instead of type checking, remove references to PEAR. --- framework/Ldap/lib/Horde/Ldap.php | 6 +-- framework/Ldap/lib/Horde/Ldap/RootDSE.php | 19 +++------ framework/Ldap/lib/Horde/Ldap/Schema.php | 71 +++++++++---------------------- framework/Ldap/lib/Horde/Ldap/Util.php | 19 --------- 4 files changed, 28 insertions(+), 87 deletions(-) diff --git a/framework/Ldap/lib/Horde/Ldap.php b/framework/Ldap/lib/Horde/Ldap.php index 697fd284e..5b483df88 100644 --- a/framework/Ldap/lib/Horde/Ldap.php +++ b/framework/Ldap/lib/Horde/Ldap.php @@ -662,12 +662,8 @@ class Horde_Ldap * * @return true Horde_Ldap_Error object or true */ - public function add(&$entry) + public function add(Horde_Ldap_Entry &$entry) { - if (!$entry instanceof Horde_Ldap_Entry) { - throw new Horde_Ldap_Exception('Parameter to Horde_Ldap::add() must be a Horde_Ldap_Entry object.'); - } - // Continue attempting the add operation in a loop until we // get a success, a definitive failure, or the world ends. $foo = 0; diff --git a/framework/Ldap/lib/Horde/Ldap/RootDSE.php b/framework/Ldap/lib/Horde/Ldap/RootDSE.php index fe97752a6..950b39599 100644 --- a/framework/Ldap/lib/Horde/Ldap/RootDSE.php +++ b/framework/Ldap/lib/Horde/Ldap/RootDSE.php @@ -27,7 +27,7 @@ * @license http://www.gnu.org/copyleft/lesser.html LGPL * @link http://pear.php.net/package/Horde_Ldap2/ */ -class Horde_Ldap_RootDSE extends PEAR +class Horde_Ldap_RootDSE { /** * @access protected @@ -52,14 +52,12 @@ class Horde_Ldap_RootDSE extends PEAR * @param array $attrs Array of attributes to search for * * @access static - * @return Horde_Ldap_RootDSE|Horde_Ldap_Error + * @return Horde_Ldap_RootDSE + * + * @throws Horde_Ldap_Exception */ - public static function fetch($ldap, $attrs = null) + public static function fetch(Horde_Ldap $ldap, $attrs = null) { - if (!$ldap instanceof Horde_Ldap) { - return PEAR::raiseError("Unable to fetch Schema: Parameter \$ldap must be a Horde_Ldap object!"); - } - if (is_array($attrs) && count($attrs) > 0 ) { $attributes = $attrs; } else { @@ -73,13 +71,10 @@ class Horde_Ldap_RootDSE extends PEAR 'supportedLDAPVersion', 'subschemaSubentry' ); } - $result = $ldap->search('', '(objectClass=*)', array('attributes' => $attributes, 'scope' => 'base')); - if (self::isError($result)) { - return $result; - } + $ldap->search('', '(objectClass=*)', array('attributes' => $attributes, 'scope' => 'base')); $entry = $result->shiftEntry(); if (false === $entry) { - return PEAR::raiseError('Could not fetch RootDSE entry'); + throw new Horde_Ldap_Exception('Could not fetch RootDSE entry'); } $ret = new Horde_Ldap_RootDSE($entry); return $ret; diff --git a/framework/Ldap/lib/Horde/Ldap/Schema.php b/framework/Ldap/lib/Horde/Ldap/Schema.php index 7156e8bc4..26e95c26d 100644 --- a/framework/Ldap/lib/Horde/Ldap/Schema.php +++ b/framework/Ldap/lib/Horde/Ldap/Schema.php @@ -16,11 +16,6 @@ */ /** - * Includes - */ -#require_once 'PEAR.php'; - -/** * Syntax definitions * * Please don't forget to add binary attributes to isBinary() below @@ -50,7 +45,7 @@ define('HORDE_LDAP_SYNTAX_OCTET_STRING', '1.3.6.1.4.1.1466.115.121.1.40'); * @license http://www.gnu.org/copyleft/lesser.html LGPL * @link http://pear.php.net/package/Horde_Ldap2/ */ -class Horde_Ldap_Schema extends PEAR +class Horde_Ldap_Schema { /** * Map of entry types to ldap attributes of subschema entry @@ -102,17 +97,6 @@ class Horde_Ldap_Schema extends PEAR */ protected $_initialized = false; - - /** - * Constructor of the class - * - * @access protected - */ - protected function __construct() - { - $this->PEAR('Horde_Ldap_Error'); // default error class - } - /** * Fetch the Schema from an LDAP connection * @@ -120,25 +104,17 @@ class Horde_Ldap_Schema extends PEAR * @param string $dn (optional) Subschema entry dn * * @access public - * @return Horde_Ldap_Schema|NET_LDAP2_Error + * @return Horde_Ldap_Schema|Horde_Ldap_Error */ - public function fetch($ldap, $dn = null) + public function fetch(Horde_Ldap $ldap, $dn = null) { - if (!$ldap instanceof Horde_Ldap) { - return PEAR::raiseError("Unable to fetch Schema: Parameter \$ldap must be a Horde_Ldap object!"); - } - $schema_o = new Horde_Ldap_Schema(); if (is_null($dn)) { // get the subschema entry via root dse $dse = $ldap->rootDSE(array('subschemaSubentry')); - if (false == Horde_Ldap::isError($dse)) { - $base = $dse->getValue('subschemaSubentry', 'single'); - if (!Horde_Ldap::isError($base)) { - $dn = $base; - } - } + $base = $dse->getValue('subschemaSubentry', 'single'); + $dn = $base; } // Support for buggy LDAP servers (e.g. Siemens DirX 6.x) that incorrectly @@ -147,12 +123,8 @@ class Horde_Ldap_Schema extends PEAR if (is_null($dn)) { // get the subschema entry via root dse $dse = $ldap->rootDSE(array('subSchemaSubentry')); - if (false == Horde_Ldap::isError($dse)) { - $base = $dse->getValue('subSchemaSubentry', 'single'); - if (!Horde_Ldap::isError($base)) { - $dn = $base; - } - } + $base = $dse->getValue('subSchemaSubentry', 'single'); + $dn = $base; } // Final fallback case where there is no subschemaSubentry attribute @@ -166,13 +138,9 @@ class Horde_Ldap_Schema extends PEAR $result = $ldap->search($dn, '(objectClass=*)', array('attributes' => array_values($schema_o->types), 'scope' => 'base')); - if (Horde_Ldap::isError($result)) { - return $result; - } - $entry = $result->shiftEntry(); if (!$entry instanceof Horde_Ldap_Entry) { - return PEAR::raiseError('Could not fetch Subschema entry'); + throw new Horde_Ldap_Exception('Could not fetch Subschema entry'); } $schema_o->parse($entry); @@ -203,8 +171,10 @@ class Horde_Ldap_Schema extends PEAR 'syntaxes' => &$this->_ldapSyntaxes ); $key = strtolower($type); - $ret = ((key_exists($key, $map)) ? $map[$key] : PEAR::raiseError("Unknown type $type")); - return $ret; + if (!key_exists($key, $map)) { + throw new Horde_Ldap_Exception("Unknown type $type"); + } + return $map[$key]; } /** @@ -221,7 +191,7 @@ class Horde_Ldap_Schema extends PEAR if ($this->_initialized) { $type = strtolower($type); if (false == key_exists($type, $this->types)) { - return PEAR::raiseError("No such type $type"); + throw new Horde_Ldap_Exception("No such type $type"); } $name = strtolower($name); @@ -232,7 +202,7 @@ class Horde_Ldap_Schema extends PEAR } elseif (key_exists($name, $this->_oids) && $this->_oids[$name]['type'] == $type) { return $this->_oids[$name]; } else { - return PEAR::raiseError("Could not find $type $name"); + throw new Horde_Ldap_Exception("Could not find $type $name"); } } else { $return = null; @@ -286,7 +256,7 @@ class Horde_Ldap_Schema extends PEAR key_exists($attr, $this->_oids[$oc])) { return $this->_oids[$oc][$attr]; } else { - return PEAR::raiseError("Could not find $attr attributes for $oc "); + throw new Horde_Ldap_Exception("Could not find $attr attributes for $oc "); } } @@ -301,9 +271,6 @@ class Horde_Ldap_Schema extends PEAR public function superclass($oc) { $o = $this->get('objectclass', $oc); - if (Horde_Ldap::isError($o)) { - return $o; - } return (key_exists('sup', $o) ? $o['sup'] : array()); } @@ -484,11 +451,13 @@ class Horde_Ldap_Schema extends PEAR ); // Check Syntax - $attr_s = $this->get('attribute', $attribute); - if (Horde_Ldap::isError($attr_s)) { + try { + $attr_s = $this->get('attribute', $attribute); + } catch (Horde_Ldap_Exception $e) { // Attribute not found in schema $return = false; // consider attr not binary - } elseif (isset($attr_s['syntax']) && in_array($attr_s['syntax'], $syntax_binary)) { + } + if (isset($attr_s['syntax']) && in_array($attr_s['syntax'], $syntax_binary)) { // Syntax is defined as binary in schema $return = true; } else { diff --git a/framework/Ldap/lib/Horde/Ldap/Util.php b/framework/Ldap/lib/Horde/Ldap/Util.php index 40b2a23ae..bd7a9c5ee 100644 --- a/framework/Ldap/lib/Horde/Ldap/Util.php +++ b/framework/Ldap/lib/Horde/Ldap/Util.php @@ -14,11 +14,6 @@ */ /** - * Includes - */ -#require_once 'PEAR.php'; - -/** * Utility Class for Horde_Ldap * * This class servers some functionality to the other classes of Horde_Ldap but most of @@ -33,20 +28,6 @@ class Horde_Ldap_Util { /** - * Constructor - * - * @access public - */ - public function __construct() - { - // We do nothing here, since all methods can be called statically. - // In Net_LDAP <= 0.7, we needed a instance of Util, because - // it was possible to do utf8 encoding and decoding, but this - // has been moved to the LDAP class. The constructor remains only - // here to document the downward compatibility of creating an instance. - } - - /** * Explodes the given DN into its elements * * {@link http://www.ietf.org/rfc/rfc2253.txt RFC 2253} says, a Distinguished Name is a sequence -- 2.11.0