Extract the factory and convert Horde_Kolab_Format to an interface.
authorGunnar Wrobel <p@rdus.de>
Thu, 9 Dec 2010 08:11:40 +0000 (09:11 +0100)
committerGunnar Wrobel <p@rdus.de>
Mon, 13 Dec 2010 10:17:06 +0000 (11:17 +0100)
12 files changed:
framework/Kolab_Format/lib/Horde/Kolab/Format.php
framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php [new file with mode: 0644]
framework/Kolab_Format/lib/Horde/Kolab/Format/Xml.php
framework/Kolab_Format/package.xml
framework/Kolab_Format/test/Horde/Kolab/Format/Autoload.php
framework/Kolab_Format/test/Horde/Kolab/Format/Integration/EventTest.php
framework/Kolab_Format/test/Horde/Kolab/Format/Integration/MimeAttrTest.php
framework/Kolab_Format/test/Horde/Kolab/Format/Integration/RecurrenceTest.php
framework/Kolab_Format/test/Horde/Kolab/Format/Integration/TaskTest.php
framework/Kolab_Format/test/Horde/Kolab/Format/TestCase.php [new file with mode: 0644]
framework/Kolab_Format/test/Horde/Kolab/Format/Unit/FactoryTest.php [new file with mode: 0644]
framework/Kolab_Format/test/Horde/Kolab/Format/Unit/FormatTest.php [deleted file]

index b53a055..7cfb723 100644 (file)
  */
 
 /**
- * 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
  * @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 (file)
index 0000000..e13ba46
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+/**
+ * A factory for generating Kolab format handlers.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Format
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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.
+     * <pre>
+     * 'version' - The format version.
+     * </pre>
+     *
+     * @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];
+    }
+}
index 9fc843f..fd147ba 100644 (file)
@@ -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
 {
 
     /**
index 9564922..970fdd5 100644 (file)
@@ -29,8 +29,8 @@
   <email>jan@horde.org</email>
   <active>yes</active>
  </lead>
- <date>2010-10-22</date>
- <time>19:03:01</time>
+ <date>2010-12-09</date>
+ <time>08:47:27</time>
  <version>
   <release>1.1.0</release>
   <api>1.1.0</api>
@@ -81,6 +81,7 @@
        </dir> <!-- /lib/Horde/Kolab/Format/Xml -->
        <file name="Date.php" role="php" />
        <file name="Exception.php" role="php" />
+       <file name="Factory.php" role="php" />
        <file name="Translation.php" role="php">
         <tasks:replace from="@data_dir@" to="data_dir" type="pear-config" />
        </file>
         <file name="TaskTest.php" role="test" />
         <file name="XmlTest.php" role="test" />
        </dir> <!-- /test/Horde/Kolab/Format/Integration -->
+       <dir name="Unit">
+        <dir name="Decorator">
+         <file name="TimedTest.php" role="test" />
+        </dir> <!-- /test/Horde/Kolab/Format/Unit/Decorator -->
+        <file name="FactoryTest.php" role="test" />
+       </dir> <!-- /test/Horde/Kolab/Format/Unit -->
        <file name="AllTests.php" role="test" />
        <file name="Autoload.php" role="test" />
        <file name="phpunit.xml" role="test" />
+       <file name="TestCase.php" role="test" />
       </dir> <!-- /test/Horde/Kolab/Format -->
      </dir> <!-- /test/Horde/Kolab -->
     </dir> <!-- /test/Horde -->
  <phprelease>
   <filelist>
    <install as="Horde/Kolab/Format/usage.txt" name="doc/Horde/Kolab/Format/usage.txt" />
+   <install as="Horde/Kolab/Format/event.php" name="examples/Horde/Kolab/Format/event.php" />
+   <install as="Horde/Kolab/Format/new_type.php" name="examples/Horde/Kolab/Format/new_type.php" />
    <install as="Horde/Kolab/Format.php" name="lib/Horde/Kolab/Format.php" />
    <install as="Horde/Kolab/Format/Date.php" name="lib/Horde/Kolab/Format/Date.php" />
    <install as="Horde/Kolab/Format/Exception.php" name="lib/Horde/Kolab/Format/Exception.php" />
+   <install as="Horde/Kolab/Format/Factory.php" name="lib/Horde/Kolab/Format/Factory.php" />
    <install as="Horde/Kolab/Format/Translation.php" name="lib/Horde/Kolab/Format/Translation.php" />
    <install as="Horde/Kolab/Format/Xml.php" name="lib/Horde/Kolab/Format/Xml.php" />
    <install as="Horde/Kolab/Format/Xml/Annotation.php" name="lib/Horde/Kolab/Format/Xml/Annotation.php" />
    <install as="Horde/Kolab/Format/AllTests.php" name="test/Horde/Kolab/Format/AllTests.php" />
    <install as="Horde/Kolab/Format/Autoload.php" name="test/Horde/Kolab/Format/Autoload.php" />
    <install as="Horde/Kolab/Format/phpunit.xml" name="test/Horde/Kolab/Format/phpunit.xml" />
+   <install as="Horde/Kolab/Format/TestCase.php" name="test/Horde/Kolab/Format/TestCase.php" />
    <install as="Horde/Kolab/Format/fixtures/task.xml" name="test/Horde/Kolab/Format/fixtures/task.xml" />
    <install as="Horde/Kolab/Format/Integration/ContactTest.php" name="test/Horde/Kolab/Format/Integration/ContactTest.php" />
    <install as="Horde/Kolab/Format/Integration/EventTest.php" name="test/Horde/Kolab/Format/Integration/EventTest.php" />
    <install as="Horde/Kolab/Format/Integration/fixtures/preferences_write_old.xml" name="test/Horde/Kolab/Format/Integration/fixtures/preferences_write_old.xml" />
    <install as="Horde/Kolab/Format/Integration/fixtures/recur.xml" name="test/Horde/Kolab/Format/Integration/fixtures/recur.xml" />
    <install as="Horde/Kolab/Format/Integration/fixtures/recur_fail.xml" name="test/Horde/Kolab/Format/Integration/fixtures/recur_fail.xml" />
+   <install as="Horde/Kolab/Format/Unit/FactoryTest.php" name="test/Horde/Kolab/Format/Unit/FactoryTest.php" />
+   <install as="Horde/Kolab/Format/Unit/Decorator/TimedTest.php" name="test/Horde/Kolab/Format/Unit/Decorator/TimedTest.php" />
+   <install as="COPYING" name="COPYING" />
+   <install as="TODO" name="TODO" />
   </filelist>
  </phprelease>
  <changelog>
     <release>stable</release>
     <api>stable</api>
    </stability>
-   <date>2010-10-22</date>
+   <date>2010-12-09</date>
    <license uri="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">LGPL</license>
    <notes>
 * Removed dependency on the Kolab package within the task handler.
index c376132..6b97222 100644 (file)
@@ -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';
index 8241ff4..21238ba 100644 (file)
@@ -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__)
index 773805f..883562c 100644 (file)
@@ -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());
     }
 }
index 06fed76..0cb5dd6 100644 (file)
@@ -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);
index 9da5506..0231a8a 100644 (file)
@@ -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 (file)
index 0000000..818848c
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+/**
+ * Basic test case.
+ *
+ * PHP version 5
+ *
+ * @category   Kolab
+ * @package    Kolab_Format
+ * @subpackage UnitTests
+ * @author     Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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 (file)
index 0000000..79346dd
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+/**
+ * Test the factory.
+ *
+ * PHP version 5
+ *
+ * @category   Kolab
+ * @package    Kolab_Format
+ * @subpackage UnitTests
+ * @author     Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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 (file)
index f86c9c0..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-<?php
-/**
- * Test the format entry point.
- *
- * PHP version 5
- *
- * @category   Kolab
- * @package    Kolab_Format
- * @subpackage UnitTests
- * @author     Gunnar Wrobel <wrobel@pardus.de>
- * @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 <wrobel@pardus.de>
- * @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');
-    }
-
-
-}