From: Gunnar Wrobel Date: Thu, 9 Dec 2010 08:11:40 +0000 (+0100) Subject: Extract the factory and convert Horde_Kolab_Format to an interface. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=f939aa3715d99ac2ab7caf1ccc728c0033a58948;p=horde.git Extract the factory and convert Horde_Kolab_Format to an interface. --- diff --git a/framework/Kolab_Format/lib/Horde/Kolab/Format.php b/framework/Kolab_Format/lib/Horde/Kolab/Format.php index b53a055ed..7cfb7237d 100644 --- a/framework/Kolab_Format/lib/Horde/Kolab/Format.php +++ b/framework/Kolab_Format/lib/Horde/Kolab/Format.php @@ -12,10 +12,11 @@ */ /** - * The Horde_Kolab_Format:: class provides the means to read/write the - * Kolab format. + * The Horde_Kolab_Format:: interface defines the basic properties of a Kolab + * format handler. * * Copyright 2007-2010 Klarälvdalens Datakonsult AB + * Copyright 2010 The Horde Project (http://www.horde.org/) * * See the enclosed file COPYING for license information (LGPL). If you did not * receive this file, see @@ -27,62 +28,28 @@ * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @link http://pear.horde.org/index.php?package=Kolab_Format */ -abstract class Horde_Kolab_Format +interface Horde_Kolab_Format { /** - * Attempts to return a concrete Horde_Kolab_Format instance based on - * $format_type. - * - * @param string $format_type The format type that should be handled. - * @param string $object_type The object type that should be handled. - * @param array $params An array of additional parameters. - * - * Supported parameters: - * - * 'version' - The format version. - * - * @return mixed The newly created concrete Horde_Kolab_Format_XML instance - * - * @throws Horde_Kolab_Format_Exception If the specified driver could not be loaded. - */ - static public function factory($format_type = '', $object_type = '', - $params = null) - { - $class = 'Horde_Kolab_Format_' . ucfirst(strtolower($format_type)); - if (class_exists($class)) { - $driver = call_user_func( - array($class, 'factory'), $object_type, $params - ); - } else { - throw new Horde_Kolab_Format_Exception( - sprintf('Failed to load Kolab Format driver %s', - $format_type) - ); - } - - return $driver; - } - - /** * Return the name of the resulting document. * * @return string The name that may be used as filename. */ - abstract public function getName(); + public function getName(); /** * Return the mime type of the resulting document. * * @return string The mime type of the result. */ - abstract public function getMimeType(); + public function getMimeType(); /** * Return the disposition of the resulting document. * * @return string The disportion of this document. */ - abstract public function getDisposition(); + public function getDisposition(); /** * Load an object based on the given XML string. @@ -93,7 +60,7 @@ abstract class Horde_Kolab_Format * * @throws Horde_Kolab_Format_Exception */ - abstract public function load(&$xmltext); + public function load(&$xmltext); /** * Convert the data to a XML string. @@ -104,5 +71,5 @@ abstract class Horde_Kolab_Format * * @throws Horde_Kolab_Format_Exception */ - abstract public function save($object); + public function save($object); } diff --git a/framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php b/framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php new file mode 100644 index 000000000..e13ba465f --- /dev/null +++ b/framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php @@ -0,0 +1,72 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Format + */ + +/** + * A factory for generating Kolab format handlers. + * + * Copyright 2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you did not + * receive this file, see + * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. + * + * @category Kolab + * @package Kolab_Format + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Format + */ +class Horde_Kolab_Format_Factory +{ + /** + * Instances. + * + * @var array + */ + private $_instances = array(); + + /** + * Generates a handler for a specific Kolab object type. + * + * @param string $format The format that the handler should work with. + * @param string $object The object type that should be handled. + * @param array $params Additional parameters. + *
+     * 'version' - The format version.
+     * 
+ * + * @return Horde_Kolab_Format The handler. + * + * @throws Horde_Kolab_Format_Exception If the specified handler does not + * exist. + */ + public function create($format_type = '', $object_type = '', $params = null) + { + $class = 'Horde_Kolab_Format_' . ucfirst(strtolower($format_type)) . '_' + . ucfirst(strtolower(str_replace('-', '', $object_type))); + + if (!isset($this->_instances[$class])) { + if (class_exists($class)) { + $this->_instances[$class] = new $class($params); + } else { + throw new Horde_Kolab_Format_Exception( + sprintf( + 'Failed to load the specified Kolab Format handler (Class %s does not exist)!', + $class + ) + ); + } + } + return $this->_instances[$class]; + } +} diff --git a/framework/Kolab_Format/lib/Horde/Kolab/Format/Xml.php b/framework/Kolab_Format/lib/Horde/Kolab/Format/Xml.php index 9fc843f50..fd147ba10 100644 --- a/framework/Kolab_Format/lib/Horde/Kolab/Format/Xml.php +++ b/framework/Kolab_Format/lib/Horde/Kolab/Format/Xml.php @@ -31,7 +31,7 @@ * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @link http://pear.horde.org/index.php?package=Kolab_Server */ -class Horde_Kolab_Format_Xml +class Horde_Kolab_Format_Xml implements Horde_Kolab_Format { /** diff --git a/framework/Kolab_Format/package.xml b/framework/Kolab_Format/package.xml index 9564922f6..970fdd546 100644 --- a/framework/Kolab_Format/package.xml +++ b/framework/Kolab_Format/package.xml @@ -29,8 +29,8 @@ jan@horde.org yes - 2010-10-22 - + 2010-12-09 + 1.1.0 1.1.0 @@ -81,6 +81,7 @@ + @@ -378,9 +379,16 @@ + + + + + + + @@ -432,9 +440,12 @@ + + + @@ -534,6 +545,7 @@ + @@ -551,6 +563,10 @@ + + + + @@ -686,7 +702,7 @@ stable stable - 2010-10-22 + 2010-12-09 LGPL * Removed dependency on the Kolab package within the task handler. diff --git a/framework/Kolab_Format/test/Horde/Kolab/Format/Autoload.php b/framework/Kolab_Format/test/Horde/Kolab/Format/Autoload.php index c376132b6..6b972225d 100644 --- a/framework/Kolab_Format/test/Horde/Kolab/Format/Autoload.php +++ b/framework/Kolab_Format/test/Horde/Kolab/Format/Autoload.php @@ -23,4 +23,4 @@ require_once 'Horde/Test/Autoload.php'; error_reporting(E_ALL | E_STRICT); /** Load the basic test definition */ -//require_once dirname(__FILE__) . '/TestCase.php'; +require_once dirname(__FILE__) . '/TestCase.php'; diff --git a/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/EventTest.php b/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/EventTest.php index 8241ff424..21238bae6 100644 --- a/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/EventTest.php +++ b/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/EventTest.php @@ -33,7 +33,7 @@ require_once dirname(__FILE__) . '/../Autoload.php'; * @link http://pear.horde.org/index.php?package=Kolab_Format */ class Horde_Kolab_Format_Integration_EventTest -extends PHPUnit_Framework_TestCase +extends Horde_Kolab_Format_TestCase { /** * Test for https://www.intevation.de/roundup/kolab/issue3525 @@ -42,7 +42,7 @@ extends PHPUnit_Framework_TestCase */ public function testIssue3525() { - $xml = Horde_Kolab_Format::factory('XML', 'event'); + $xml = $this->factory('XML', 'event'); // Load XML $event = file_get_contents(dirname(__FILE__) diff --git a/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/MimeAttrTest.php b/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/MimeAttrTest.php index 773805f2c..883562cbc 100644 --- a/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/MimeAttrTest.php +++ b/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/MimeAttrTest.php @@ -33,7 +33,7 @@ require_once dirname(__FILE__) . '/../Autoload.php'; * @link http://pear.horde.org/index.php?package=Kolab_Format */ class Horde_Kolab_Format_Integration_MimeAttrTest -extends PHPUnit_Framework_TestCase +extends Horde_Kolab_Format_TestCase { /** * Test retrieving the document name. @@ -42,7 +42,7 @@ extends PHPUnit_Framework_TestCase */ public function testGetName() { - $format = Horde_Kolab_Format::factory('XML', 'contact'); + $format = $this->factory('XML', 'contact'); $this->assertEquals('kolab.xml', $format->getName()); } @@ -53,7 +53,7 @@ extends PHPUnit_Framework_TestCase */ public function testMimeType() { - $format = Horde_Kolab_Format::factory('XML', 'contact'); + $format = $this->factory('XML', 'contact'); $this->assertEquals('application/x-vnd.kolab.contact', $format->getMimeType()); } @@ -65,7 +65,7 @@ extends PHPUnit_Framework_TestCase */ public function testGetDisposition() { - $format = Horde_Kolab_Format::factory('XML', 'contact'); + $format = $this->factory('XML', 'contact'); $this->assertEquals('attachment', $format->getDisposition()); } } diff --git a/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/RecurrenceTest.php b/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/RecurrenceTest.php index 06fed7612..0cb5dd6d6 100644 --- a/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/RecurrenceTest.php +++ b/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/RecurrenceTest.php @@ -33,7 +33,7 @@ require_once dirname(__FILE__) . '/../Autoload.php'; * @link http://pear.horde.org/index.php?package=Kolab_Format */ class Horde_Kolab_Format_Integration_RecurrenceTest -extends PHPUnit_Framework_TestCase +extends Horde_Kolab_Format_TestCase { /** @@ -43,8 +43,6 @@ extends PHPUnit_Framework_TestCase */ protected function setUp() { - @include_once 'Horde/Date/Recurrence.php'; - if (!class_exists('Horde_Date_Recurrence')) { $this->markTestSkipped('The Horde_Date_Recurrence class is missing.'); } @@ -65,13 +63,13 @@ extends PHPUnit_Framework_TestCase */ public function testBug6388() { - $xml = Horde_Kolab_Format::factory('XML', 'event'); + $xml = $this->factory('XML', 'event'); // Load XML $recur = file_get_contents(dirname(__FILE__) . '/fixtures/recur.xml'); // Load XML - $xml = &Horde_Kolab_Format::factory('XML', 'event'); + $xml = $this->factory('XML', 'event'); $recur = file_get_contents(dirname(__FILE__) . '/fixtures/recur_fail.xml'); // Check that the xml fails because of a missing interval value @@ -91,7 +89,7 @@ extends PHPUnit_Framework_TestCase */ public function testExceptions() { - $xml = Horde_Kolab_Format::factory('XML', 'event'); + $xml = $this->factory('XML', 'event'); // Load XML $recur = file_get_contents(dirname(__FILE__) . '/fixtures/recur.xml'); @@ -124,7 +122,7 @@ extends PHPUnit_Framework_TestCase */ public function testCompletions() { - $xml = Horde_Kolab_Format::factory('XML', 'event'); + $xml = $this->factory('XML', 'event'); $r = new Horde_Date_Recurrence(0); $r->setRecurType(Horde_Date_Recurrence::RECUR_DAILY); diff --git a/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/TaskTest.php b/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/TaskTest.php index 9da55068d..0231a8a78 100644 --- a/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/TaskTest.php +++ b/framework/Kolab_Format/test/Horde/Kolab/Format/Integration/TaskTest.php @@ -33,7 +33,7 @@ require_once dirname(__FILE__) . '/../Autoload.php'; * @link http://pear.horde.org/index.php?package=Kolab_Format */ class Horde_Kolab_Format_Integration_TaskTest -extends PHPUnit_Framework_TestCase +extends Horde_Kolab_Format_TestCase { /** @@ -41,7 +41,7 @@ extends PHPUnit_Framework_TestCase */ public function testBasicTask() { - $xml = Horde_Kolab_Format::factory('XML', 'task'); + $xml = $this->factory('XML', 'task'); // Load XML $task = file_get_contents(dirname(__FILE__) . '/../fixtures/task.xml'); diff --git a/framework/Kolab_Format/test/Horde/Kolab/Format/TestCase.php b/framework/Kolab_Format/test/Horde/Kolab/Format/TestCase.php new file mode 100644 index 000000000..818848ca0 --- /dev/null +++ b/framework/Kolab_Format/test/Horde/Kolab/Format/TestCase.php @@ -0,0 +1,39 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Format + */ + +/** + * Basic test case. + * + * Copyright 2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Kolab + * @package Kolab_Format + * @subpackage UnitTests + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Format + */ +class Horde_Kolab_Format_TestCase +extends PHPUnit_Framework_TestCase +{ + public function factory( + $format_type = '', $object_type = '', $params = null + ) { + $factory = new Horde_Kolab_Format_Factory(); + return $factory->create($format_type, $object_type, $params); + } +} diff --git a/framework/Kolab_Format/test/Horde/Kolab/Format/Unit/FactoryTest.php b/framework/Kolab_Format/test/Horde/Kolab/Format/Unit/FactoryTest.php new file mode 100644 index 000000000..79346dd20 --- /dev/null +++ b/framework/Kolab_Format/test/Horde/Kolab/Format/Unit/FactoryTest.php @@ -0,0 +1,64 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Format + */ + +/** + * Prepare the test setup. + */ +require_once dirname(__FILE__) . '/../Autoload.php'; + +/** + * Test the factory. + * + * Copyright 2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Kolab + * @package Kolab_Format + * @subpackage UnitTests + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Format + */ +class Horde_Kolab_Format_Unit_FactoryTest +extends PHPUnit_Framework_TestCase +{ + public function testFactory() + { + $factory = new Horde_Kolab_Format_Factory(); + $this->assertInstanceOf( + 'Horde_Kolab_Format_Xml_Contact', + $factory->create('XML', 'contact') + ); + } + + /** + * @expectedException Horde_Kolab_Format_Exception + */ + public function testFactoryException() + { + $factory = new Horde_Kolab_Format_Factory(); + $factory->create('UNKNOWN', 'contact'); + } + + public function testCaching() + { + $factory = new Horde_Kolab_Format_Factory(); + $this->assertSame( + $factory->create('XML', 'contact'), + $factory->create('XML', 'contact') + ); + } +} diff --git a/framework/Kolab_Format/test/Horde/Kolab/Format/Unit/FormatTest.php b/framework/Kolab_Format/test/Horde/Kolab/Format/Unit/FormatTest.php deleted file mode 100644 index f86c9c023..000000000 --- a/framework/Kolab_Format/test/Horde/Kolab/Format/Unit/FormatTest.php +++ /dev/null @@ -1,55 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Kolab_Format - */ - -/** - * Prepare the test setup. - */ -require_once dirname(__FILE__) . '/../Autoload.php'; - -/** - * Test the format entry point. - * - * Copyright 2010 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (LGPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. - * - * @category Kolab - * @package Kolab_Format - * @subpackage UnitTests - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Kolab_Format - */ -class Horde_Kolab_Format_Unit_FormatTest -extends PHPUnit_Framework_TestCase -{ - public function testFactory() - { - $this->assertInstanceOf( - 'Horde_Kolab_Format_Xml_Contact', - Horde_Kolab_Format::factory('XML', 'contact') - ); - } - - /** - * @expectedException Horde_Kolab_Format_Exception - */ - public function testFactoryException() - { - Horde_Kolab_Format::factory('UNKNOWN', 'contact'); - } - - -}