From 229fe913cb7e3797c21ac2a95c6346018ddb5d0f Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Tue, 23 Mar 2010 06:14:07 +0100 Subject: [PATCH] Refactor. --- .../lib/Horde/Kolab/Storage/Namespace.php | 150 +++++++-------------- .../lib/Horde/Kolab/Storage/Namespace/Config.php | 61 +++------ .../lib/Horde/Kolab/Storage/Namespace/Element.php | 122 +++++++++++++++++ .../Kolab/Storage/Namespace/Element/Other.php | 47 +++++++ .../Kolab/Storage/Namespace/Element/Private.php | 27 ++++ .../Kolab/Storage/Namespace/Element/Shared.php | 27 ++++ .../Storage/Namespace/Element/SharedWithPrefix.php | 41 ++++++ .../lib/Horde/Kolab/Storage/Namespace/Fixed.php | 35 ++--- .../lib/Horde/Kolab/Storage/Namespace/Imap.php | 38 +----- framework/Kolab_Storage/package.xml | 12 ++ .../test/Horde/Kolab/Storage/NamespaceTest.php | 36 +++-- 11 files changed, 384 insertions(+), 212 deletions(-) create mode 100644 framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element.php create mode 100644 framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/Other.php create mode 100644 framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/Private.php create mode 100644 framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/Shared.php create mode 100644 framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/SharedWithPrefix.php diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace.php index 563ce3efe..4908a8905 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace.php @@ -39,17 +39,7 @@ abstract class Horde_Kolab_Storage_Namespace * * @var array */ - protected $_namespaces = array( - self::PRIV => array( - 'INBOX' => '/', - ), - self::OTHER => array( - 'user' => '/', - ), - self::SHARED => array( - '' => '/', - ), - ); + protected $_namespaces = array(); /** * The characterset this module uses to communicate with the outside world. @@ -66,36 +56,64 @@ abstract class Horde_Kolab_Storage_Namespace protected $_sharedPrefix; /** + * The namespace that matches any folder name not matching to another + * namespace. + * + * @var Horde_Kolab_Storage_Namespace_Element + */ + protected $_any; + + /** * Indicates the personal namespace that the class will use to create new * folders. * - * @var string + * @var Horde_Kolab_Storage_Namespace_Element */ protected $_primaryPersonalNamespace; /** + * Constructor. + */ + public function __construct() + { + $this->_charset = Horde_Nls::getCharset(); + if (empty($this->_primaryPersonalNamespace)) { + $priv = null; + foreach ($this->_namespaces as $namespace) { + if ($namespace->getName() == 'INBOX') { + $this->_primaryPersonalNamespace = $namespace; + break; + } + if (empty($priv) && $namespace->getType() == self::PRIV) { + $priv = $namespace; + } + } + if (empty($this->_primaryPersonalNamespace)) { + $this->_primaryPersonalNamespace = $priv; + } + } + } + + /** * Match a folder name with the corresponding namespace. * * @param string $name The name of the folder. * - * @return array The corresponding namespace. + * @return Horde_Kolab_Storage_Namespace_Element The corresponding namespace. * * @throws Horde_Kolab_Storage_Exception If the namespace of the folder * cannot be determined. */ protected function matchNamespace($name) { - foreach (array(self::PRIV, self::OTHER, self::SHARED) as $type) { - foreach ($this->_namespaces[$type] as $namespace => $delimiter) { - if ($namespace === '' || strpos($name, $namespace) === 0) { - return array( - 'namespace' => $namespace, - 'delimiter' => $delimiter, - 'type' => $type, - ); - } + foreach ($this->_namespaces as $namespace) { + if ($namespace->matches($name)) { + return $namespace; } } + if (!empty($this->_any)) { + return $this->_any; + } throw new Horde_Kolab_Storage_Exception( sprintf('Namespace of folder %s cannot be determined.', $name) ); @@ -121,7 +139,8 @@ abstract class Horde_Kolab_Storage_Namespace */ public function getTitle($name) { - return join($this->_subpath($name, $this->matchNamespace($name)), ':'); + $name = Horde_String::convertCharset($name, 'UTF7-IMAP', $this->_charset); + return $this->matchNamespace($name)->getTitle($name); } /** @@ -133,23 +152,8 @@ abstract class Horde_Kolab_Storage_Namespace */ public function getOwner($name) { - $namespace = $this->matchNamespace($name); $name = Horde_String::convertCharset($name, 'UTF7-IMAP', $this->_charset); - $path = explode($namespace['delimiter'], $name); - if ($namespace['type'] == self::PRIV) { - return self::PRIV; - } - if ($namespace['type'] == self::OTHER) { - $user = $path[1]; - $domain = strstr(array_pop($path), '@'); - if (!empty($domain)) { - $user .= $domain; - } - return self::OTHER . ':' . $user; - } - if ($namespace['type'] == self::SHARED) { - return self::SHARED; - } + return $this->matchNamespace($name)->getOwner($name); } /** @@ -161,9 +165,8 @@ abstract class Horde_Kolab_Storage_Namespace */ public function getSubpath($name) { - $namespace = $this->matchNamespace($name); - $path = $this->_subpath($name, $namespace); - return join($path, $namespace['delimiter']); + $name = Horde_String::convertCharset($name, 'UTF7-IMAP', $this->_charset); + return $this->matchNamespace($name)->getSubpath($name); } /** @@ -179,65 +182,10 @@ abstract class Horde_Kolab_Storage_Namespace $path = explode(':', $name); if (empty($this->_sharedPrefix) || (strpos($path[0], $this->_sharedPrefix) === false - && !in_array($path[0], array_keys($this->_namespaces[self::OTHER])))) { - $namespace = $this->_getPrimaryPersonalNamespace(); - array_unshift($path, $namespace['namespace']); - } else { - $namespace = $this->matchNamespace($name); - } - return Horde_String::convertCharset( - join($path, $namespace['delimiter']), $this->_charset, 'UTF7-IMAP' - ); - } - - /** - * Returns the primary personal namespace. - * - * @return array The primary personal namespace. - */ - protected function _getPrimaryPersonalNamespace() - { - foreach ($this->_namespaces[self::PRIV] as $namespace => $delimiter) { - if ($namespace == $this->_primaryPersonalNamespace) { - return array( - 'namespace' => $namespace, - 'delimiter' => $delimiter, - 'type' => self::PRIV, - ); - } - } - return array( - 'namespace' => array_shift(array_keys($this->_namespaces[self::PRIV])), - 'delimiter' => reset($this->_namespaces[self::PRIV]), - 'type' => self::PRIV, - ); - } - - /** - * Return an array describing the path elements of the folder. - * - * @param string $name The name of the folder. - * @param array $namespace The namespace of the folder. - * - * @return array The path elements. - */ - protected function _subpath($name, array $namespace) - { - $name = Horde_String::convertCharset($name, 'UTF7-IMAP', $this->_charset); - $path = explode($namespace['delimiter'], $name); - if ($path[0] == $namespace['namespace']) { - array_shift($path); - } - if ($namespace['type'] == self::OTHER) { - array_shift($path); - } - if (!empty($this->_sharedPrefix) - && $namespace['type'] == self::SHARED) { - if (strpos($path[0], $this->_sharedPrefix) === 0) { - $path[0] = substr($path[0], strlen($this->_sharedPrefix)); - } + && $namespace->getType() != self::OTHER)) { + array_unshift($path, $this->_primaryPersonalNamespace->getName()); + $namespace = $this->_primaryPersonalNamespace; } - //@todo: What about the potential trailing domain? - return $path; + return Horde_String::convertCharset($namespace->generateName($path), $this->_charset, 'UTF7-IMAP'); } } \ No newline at end of file diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Config.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Config.php index b1f8f0575..e4e6ffdee 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Config.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Config.php @@ -31,51 +31,30 @@ class Horde_Kolab_Storage_Namespace_Config extends Horde_Kolab_Storage_Namespace { /** - * The namespaces. - * - * @var array - */ - protected $_namespaces = array( - self::PRIV => array( - 'INBOX' => '/', - ), - self::OTHER => array( - 'user' => '/', - ), - self::SHARED => array( - '' => '/', - ), - ); - - /** - * A prefix in the shared namespaces that will be ignored/removed. - * - * @var string - */ - protected $_sharedPrefix = ''; - - /** - * Indicates the personal namespace that the class will use to create new - * folders. - * - * @var string - */ - protected $_primaryPersonalNamespace = 'INBOX'; - - /** * Constructor. */ public function __construct(array $configuration) { - if (isset($configuration['elements'])) { - $this->_namespaces = $configuration['elements']; - } - if (isset($configuration['shared_prefix'])) { - $this->_sharedPrefix = $configuration['shared_prefix']; - } - if (isset($configuration['add_namespace'])) { - $this->_primaryPersonalNamespace = $configuration['add_namespace']; + parent::__construct(); + foreach ($configuration as $element) { + if ($element['type'] == Horde_Kolab_Storage_Namespace::SHARED + && isset($element['prefix'])) { + $namespace_element = new Horde_Kolab_Storage_Namespace_Element_SharedWithPrefix( + $element['name'], $element['delimiter'], $element['prefix'] + ); + $this->_sharedPrefix = $element['prefix']; + } else { + $class = 'Horde_Kolab_Storage_Namespace_Element_' . ucfirst($element['type']); + $namespace_element = new $class($element['name'], $element['delimiter']); + } + if (empty($element['name'])) { + $this->_any = $namespace_element; + } else { + $this->_namespaces[] = $namespace_element; + } + if (isset($element['add'])) { + $this->_primaryPersonalNamespace = $namespace_element; + } } - $this->_charset = Horde_Nls::getCharset(); } } \ No newline at end of file diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element.php new file mode 100644 index 000000000..6f717bc7b --- /dev/null +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element.php @@ -0,0 +1,122 @@ +_name = $name; + $this->_delimiter = $delimiter; + } + + /** + * Return the type of this namespace (private, other, or shared). + * + * @return string The type. + */ + abstract public function getType(); + + /** + * Return the name of this namespace. + * + * @return string The name/prefix. + */ + public function getName() + { + return $this->_name; + } + + /** + * Does the folder name lie in this namespace? + * + * @param string $name The name of the folder. + * + * @return boolean True if the folder is element of this namespace. + */ + public function matches($name) + { + return (strpos($name, $this->_name) === 0); + } + + /** + * Return the owner of a folder. + * + * @param string $name The name of the folder. + * + * @return string The owner of the folder. + */ + abstract public function getOwner($name); + + /** + * Return the title of a folder. + * + * @param string $name The name of the folder. + * + * @return string The title of the folder. + */ + public function getTitle($name) + { + return join($this->_subpath($name), ':'); + } + + /** + * Get the sub path for the given folder name. + * + * @param string $name The folder name. + * + * @return string The sub path. + */ + public function getSubpath($name) + { + return join($this->_subpath($name), $this->_delimiter); + } + + /** + * Return an array describing the path elements of the folder. + * + * @param string $name The name of the folder. + * + * @return array The path elements. + */ + protected function _subpath($name) + { + $path = explode($this->_delimiter, $name); + if ($path[0] == $this->_name) { + array_shift($path); + } + //@todo: What about the potential trailing domain? + return $path; + } + + /** + * Generate a folder path for the given path in this namespace. + * + * @param array $path The path of the folder. + * + * @return string The name of the folder. + */ + public function generateName($path) + { + return join($path, $this->_delimiter); + } + +} \ No newline at end of file diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/Other.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/Other.php new file mode 100644 index 000000000..583e53473 --- /dev/null +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/Other.php @@ -0,0 +1,47 @@ +_delimiter, $name); + $user = $path[1]; + $domain = strstr(array_pop($path), '@'); + if (!empty($domain)) { + $user .= $domain; + } + return Horde_Kolab_Storage_Namespace::OTHER . ':' . $user; + } + + /** + * Return an array describing the path elements of the folder. + * + * @param string $name The name of the folder. + * + * @return array The path elements. + */ + protected function _subpath($name) + { + $path = parent::_subpath($name); + array_shift($path); + return $path; + } +} \ No newline at end of file diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/Private.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/Private.php new file mode 100644 index 000000000..cae23d68b --- /dev/null +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Element/Private.php @@ -0,0 +1,27 @@ +_prefix = $prefix; + } + + /** + * Return an array describing the path elements of the folder. + * + * @param string $name The name of the folder. + * + * @return array The path elements. + */ + protected function _subpath($name) + { + $path = parent::_subpath($name); + if (strpos($path[0], $this->_prefix) === 0) { + $path[0] = substr($path[0], strlen($this->_prefix)); + } + return $path; + } +} \ No newline at end of file diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Fixed.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Fixed.php index 01eb8ae4a..ce9558bf5 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Fixed.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Fixed.php @@ -31,30 +31,6 @@ class Horde_Kolab_Storage_Namespace_Fixed extends Horde_Kolab_Storage_Namespace { /** - * The namespaces. - * - * @var array - */ - protected $_namespaces = array( - self::PRIV => array( - 'INBOX' => '/', - ), - self::OTHER => array( - 'user' => '/', - ), - self::SHARED => array( - '' => '/', - ), - ); - - /** - * A prefix in the shared namespaces that will be ignored/removed. - * - * @var string - */ - protected $_sharedPrefix = 'shared.'; - - /** * Indicates the personal namespace that the class will use to create new * folders. * @@ -67,6 +43,15 @@ extends Horde_Kolab_Storage_Namespace */ public function __construct() { - $this->_charset = Horde_Nls::getCharset(); + parent::__construct(); + + $priv = new Horde_Kolab_Storage_Namespace_Element_Private('INBOX', '/'); + $other = new Horde_Kolab_Storage_Namespace_Element_Other('user', '/'); + $shared = new Horde_Kolab_Storage_Namespace_Element_SharedWithPrefix('', '/', 'shared.'); + + $this->_namespaces = array($priv, $other); + $this->_any = $shared; + $this->_primaryPersonalNamespace = $priv; + $this->_sharedPrefix = 'shared.'; } } \ No newline at end of file diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Imap.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Imap.php index 89a9ae5d4..1748227bb 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Imap.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace/Imap.php @@ -30,44 +30,20 @@ * @link http://pear.horde.org/index.php?package=Kolab_Storage */ class Horde_Kolab_Storage_Namespace_Imap -extends Horde_Kolab_Storage_Namespace +extends Horde_Kolab_Storage_Namespace_Config { /** - * The namespaces. - * - * @var array - */ - protected $_namespaces = array(); - - /** - * A prefix in the shared namespaces that will be ignored/removed. - * - * @var string - */ - protected $_sharedPrefix = ''; - - /** - * Indicates the personal namespace that the class will use to create new - * folders. - * - * @var string - */ - protected $_primaryPersonalNamespace = 'INBOX'; - - /** * Constructor. */ public function __construct(array $namespaces, array $configuration) { + $c = array(); foreach ($namespaces as $namespace) { - $this->_namespaces[$namespace['type']][$namespace['name']] = $namespace['delimiter']; - } - if (isset($configuration['shared_prefix'])) { - $this->_sharedPrefix = $configuration['shared_prefix']; - } - if (isset($configuration['add_namespace'])) { - $this->_primaryPersonalNamespace = $configuration['add_namespace']; + if (in_array($namespace['name'], array_keys($configuration))) { + $namespace = array_merge($namespace, $configuration[$namespace['name']]); + } + $c[] = $namespace; } - $this->_charset = Horde_Nls::getCharset(); + parent::__construct($c); } } \ No newline at end of file diff --git a/framework/Kolab_Storage/package.xml b/framework/Kolab_Storage/package.xml index 30b4073a2..3d71c9a99 100644 --- a/framework/Kolab_Storage/package.xml +++ b/framework/Kolab_Storage/package.xml @@ -82,6 +82,13 @@ + + + + + + + @@ -195,6 +202,11 @@ + + + + + diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/NamespaceTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/NamespaceTest.php index bc949de70..921321efa 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/NamespaceTest.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/NamespaceTest.php @@ -270,19 +270,23 @@ class Horde_Kolab_Storage_NamespaceTest extends PHPUnit_Framework_TestCase new Horde_Kolab_Storage_Namespace_Fixed(), new Horde_Kolab_Storage_Namespace_Config( array( - 'elements' => array( - Horde_Kolab_Storage_Namespace::PRIV => array( - 'INBOX' => '/', - ), - Horde_Kolab_Storage_Namespace::OTHER => array( - 'user' => '/', - ), - Horde_Kolab_Storage_Namespace::SHARED => array( - '' => '/', - ), + array( + 'type' => Horde_Kolab_Storage_Namespace::PRIV, + 'name' => 'INBOX', + 'delimiter' => '/', + 'add' => true, + ), + array( + 'type' => Horde_Kolab_Storage_Namespace::OTHER, + 'name' => 'user', + 'delimiter' => '/', + ), + array( + 'type' => Horde_Kolab_Storage_Namespace::SHARED, + 'name' => '', + 'delimiter' => '/', + 'prefix' => 'shared.' ), - 'shared_prefix' => 'shared.', - 'add_namespace' => 'INBOX', ) ), new Horde_Kolab_Storage_Namespace_Imap( @@ -304,8 +308,12 @@ class Horde_Kolab_Storage_NamespaceTest extends PHPUnit_Framework_TestCase ), ), array( - 'shared_prefix' => 'shared.', - 'add_namespace' => 'INBOX', + 'INBOX' => array( + 'add' => true, + ), + '' => array( + 'prefix' => 'shared.', + ), ) ), -- 2.11.0