Remove Core dependency in Horde_Editor
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 25 Mar 2010 18:42:23 +0000 (12:42 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 25 Mar 2010 19:51:29 +0000 (13:51 -0600)
17 files changed:
ansel/img/ecard.php
framework/Core/lib/Horde/Core/Binder/Editor.php [new file with mode: 0644]
framework/Core/lib/Horde/Core/Factory/Editor.php [new file with mode: 0644]
framework/Core/lib/Horde/Registry.php
framework/Core/package.xml
framework/Editor/lib/Horde/Editor.php
framework/Editor/lib/Horde/Editor/Ckeditor.php
framework/Editor/lib/Horde/Editor/Exception.php [new file with mode: 0644]
framework/Editor/lib/Horde/Editor/Fckeditor.php
framework/Editor/package.xml
framework/Model/lib/Horde/Form/VarRenderer/Xhtml.php
framework/Ui/lib/Horde/Ui/VarRenderer/Html.php
gollem/edit.php
imp/lib/Auth.php
imp/lib/Ui/Compose.php
imp/stationery.php
news/add.php

index f5f9efa..a4247a9 100644 (file)
@@ -101,8 +101,8 @@ $vars->set('image_desc', strlen($image->caption) ? $image->caption : $image->fil
 $form = new Ansel_Form_Ecard($vars, $title);
 $renderer = new Horde_Form_Renderer();
 
-if ($browser->hasFeature('rte')) {
-    $editor = Horde_Editor::singleton('ckeditor', array('id' => 'ecard_comments'));
+$editor = $injector->getInstance('Horde_Editor')->getEditor('ckeditor', array('id' => 'ecard_comments'));
+if ($editor->supportedByBrowser()) {
     $vars->set('rtemode', 1);
     $form->addHidden('', 'rtemode', 'text', false);
 }
diff --git a/framework/Core/lib/Horde/Core/Binder/Editor.php b/framework/Core/lib/Horde/Core/Binder/Editor.php
new file mode 100644 (file)
index 0000000..c20c847
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+class Horde_Core_Binder_Editor implements Horde_Injector_Binder
+{
+    public function create(Horde_Injector $injector)
+    {
+        return new Horde_Core_Factory_Editor($injector);
+    }
+
+    public function equals(Horde_Injector_Binder $binder)
+    {
+        return false;
+    }
+}
diff --git a/framework/Core/lib/Horde/Core/Factory/Editor.php b/framework/Core/lib/Horde/Core/Factory/Editor.php
new file mode 100644 (file)
index 0000000..f080dae
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+/**
+ * A Horde_Injector:: based Horde_Editor:: factory.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Core
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Core
+ */
+
+/**
+ * A Horde_Injector:: based Horde_Editor:: 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 Horde
+ * @package  Core
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Core
+ */
+class Horde_Core_Factory_Editor
+{
+    /**
+     * The injector.
+     *
+     * @var Horde_Injector
+     */
+    private $_injector;
+
+    /**
+     * Constructor.
+     *
+     * @param Horde_Injector $injector  The injector to use.
+     */
+    public function __construct(Horde_Injector $injector)
+    {
+        $this->_injector = $injector;
+    }
+
+    /**
+     * Return the Horde_Editor:: instance.
+     *
+     * @param string $driver  The editor driver.
+     * @param array $params   Additional parameters to pass to the driver
+     *                        (will override Horde defaults).
+     *
+     * @return Horde_Editor  The singleton editor instance.
+     * @throws Horde_Editor_Exception
+     */
+    public function getEditor($driver, $params = array())
+    {
+        $browser = $this->_injector->getInstance('Horde_Browser');
+        if (!$browser->hasFeature('rte')) {
+            return Horde_Editor::factory();
+        }
+
+        $params = array_merge(
+            Horde::getDriverConfig('editor', $driver),
+            $params,
+            array(
+                'browser' => $browser
+            )
+        );
+
+        return Horde_Editor::factory($driver, $params);
+    }
+
+}
index 4a5fd8c..5a5cd25 100644 (file)
@@ -236,6 +236,7 @@ class Horde_Registry
             // 'Horde_Browser' - initialized below
             'Horde_Cache' => new Horde_Core_Binder_Cache(),
             'Horde_Db_Adapter_Base' => new Horde_Core_Binder_Db('reader'),
+            'Horde_Editor' => new Horde_Core_Binder_Editor(),
             'Horde_Lock' => new Horde_Core_Binder_Lock(),
             'Horde_Log_Logger' => new Horde_Core_Binder_Logger(),
             'Horde_Memcache' => new Horde_Core_Binder_Memcache(),
index b8f9be4..4dae572 100644 (file)
@@ -68,6 +68,7 @@ Application Framework.
        <file name="Cache.php" role="php" />
        <file name="Db.php" role="php" />
        <file name="Dns.php" role="php" />
+       <file name="Editor.php" role="php" />
        <file name="Lock.php" role="php" />
        <file name="Logger.php" role="php" />
        <file name="Mail.php" role="php" />
@@ -80,6 +81,7 @@ Application Framework.
        <file name="Vfs.php" role="php" />
       </dir> <!-- /lib/Horde/Core/Binder -->
       <dir name="Factory">
+       <file name="Editor.php" role="php" />
        <file name="KolabServer.php" role="php" />
        <file name="KolabSession.php" role="php" />
        <file name="KolabStorage.php" role="php" />
@@ -179,6 +181,10 @@ Application Framework.
     <channel>pear.horde.org</channel>
    </package>
    <package>
+    <name>Editor</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Kolab_Server</name>
     <channel>pear.horde.org</channel>
    </package>
@@ -202,6 +208,7 @@ Application Framework.
    <install name="lib/Horde/Core/Binder/Cache.php" as="Horde/Core/Binder/Cache.php" />
    <install name="lib/Horde/Core/Binder/Db.php" as="Horde/Core/Binder/Db.php" />
    <install name="lib/Horde/Core/Binder/Dns.php" as="Horde/Core/Binder/Dns.php" />
+   <install name="lib/Horde/Core/Binder/Editor.php" as="Horde/Core/Binder/Editor.php" />
    <install name="lib/Horde/Core/Binder/Lock.php" as="Horde/Core/Binder/Lock.php" />
    <install name="lib/Horde/Core/Binder/Logger.php" as="Horde/Core/Binder/Logger.php" />
    <install name="lib/Horde/Core/Binder/Mail.php" as="Horde/Core/Binder/Mail.php" />
@@ -212,6 +219,7 @@ Application Framework.
    <install name="lib/Horde/Core/Binder/Template.php" as="Horde/Core/Binder/Template.php" />
    <install name="lib/Horde/Core/Binder/Token.php" as="Horde/Core/Binder/Token.php" />
    <install name="lib/Horde/Core/Binder/Vfs.php" as="Horde/Core/Binder/Vfs.php" />
+   <install name="lib/Horde/Core/Factory/Editor.php" as="Horde/Core/Factory/Editor.php" />
    <install name="lib/Horde/Core/Factory/KolabServer.php" as="Horde/Core/Factory/KolabServer.php" />
    <install name="lib/Horde/Core/Factory/KolabSession.php" as="Horde/Core/Factory/KolabSession.php" />
    <install name="lib/Horde/Core/Factory/KolabStorage.php" as="Horde/Core/Factory/KolabStorage.php" />
index e6bf2ae..7d19e6e 100644 (file)
@@ -8,13 +8,21 @@
  * See the enclosed file COPYING for license information (LGPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  *
- * @author  Nuno Loureiro <nuno@co.sapo.pt>
- * @author  Michael Slusarz <slusarz@curecanti.org>
- * @package Horde_Editor
+ * @author   Nuno Loureiro <nuno@co.sapo.pt>
+ * @author   Michael Slusarz <slusarz@curecanti.org>
+ * @category Horde
+ * @package  Editor
  */
 class Horde_Editor
 {
     /**
+     * A browser detection object.
+     *
+     * @var Horde_Browser
+     */
+    protected $_browser;
+
+    /**
      * Javascript code to init the editor.
      *
      * @var string
@@ -22,15 +30,14 @@ class Horde_Editor
     protected $_js = '';
 
     /**
-     * Attempts to return a concrete Horde_Editor instance based on $driver.
+     * Attempts to return a concrete instance based on $driver.
      *
-     * @param string $driver  The type of concrete Horde_Editor subclass to
-     *                       return.
-     * @param array $params  A hash containing any additional configuration or
-     *                       connection parameters a subclass might need.
+     * @param string $driver  The type of concrete subclass to return.
+     * @param array $params   A hash containing any additional configuration
+     *                        or connection parameters a subclass might need.
      *
-     * @return Horde_Editor  The newly created concrete Horde_Editor instance,
-     *                       or false on error.
+     * @return Horde_Editor  The newly created concrete instance.
+     * @throws Horde_Editor_Exception.
      */
     static public function factory($driver, $params = null)
     {
@@ -40,52 +47,26 @@ class Horde_Editor
         }
 
         $class = __CLASS__ . '_' . $driver;
-        if (!class_exists($class)) {
-            throw new Exception('Driver ' . $driver . ' not found');
+        if (class_exists($class)) {
+            return new $class($params);
         }
 
-        if (is_null($params) && class_exists('Horde')) {
-            $params = Horde::getDriverConfig('editor', $driver);
-        }
-        return new $class($params);
+        throw new Horde_Editor_Exception('Driver ' . $driver . ' not found');
     }
 
     /**
-     * Attempts to return a reference to a concrete Horde_Editor
-     * instance based on $driver. It will only create a new instance
-     * if no Horde_Editor instance with the same parameters currently
-     * exists.
-     *
-     * This should be used if multiple cache backends (and, thus,
-     * multiple Horde_Editor instances) are required.
+     * Constructor.
      *
-     * This method must be invoked as:
-     *   $var = Horde_Editor::singleton()
-     *
-     * @param mixed $driver  The type of concrete Horde_Editor subclass to
-     *                       return. If $driver is an array, then we will look
-     *                       in $driver[0]/lib/Editor/ for the subclass
-     *                       implementation named $driver[1].php.
-     * @param array $params  A hash containing any additional configuration or
-     *                       connection parameters a subclass might need.
-     *
-     * @return Horde_Editor  The concrete Horde_Editor reference, or false on
-     *                       error.
+     * @param array $params  The following configuration parameters:
+     * <pre>
+     * 'browser' - (Horde_Browser) A browser object.
+     * </pre>
      */
-    public static function singleton($driver, $params = null)
+    public function __construct($params = array())
     {
-        static $instances = array();
-
-        if (is_null($params) && class_exists('Horde')) {
-            $params = Horde::getDriverConfig('editor', $driver);
+        if (isset($params['browser'])) {
+            $this->_browser = $params['browser'];
         }
-
-        $signature = serialize(array($driver, $params));
-        if (!array_key_exists($signature, $instances)) {
-            $instances[$signature] = self::factory($driver, $params);
-        }
-
-        return $instances[$signature];
     }
 
     /**
@@ -105,7 +86,7 @@ class Horde_Editor
      */
     public function supportedByBrowser()
     {
-        return true;
+        return false;
     }
 
     /**
index d39e654..534fd8f 100644 (file)
@@ -8,8 +8,9 @@
  * See the enclosed file COPYING for license information (LGPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  *
- * @author  Michael Slusarz <slusarz@horde.org>
- * @package Horde_Editor
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @package  Editor
  */
 class Horde_Editor_Ckeditor extends Horde_Editor
 {
@@ -30,6 +31,12 @@ class Horde_Editor_Ckeditor extends Horde_Editor
      */
     public function __construct($params = array())
     {
+        parent::__construct($params);
+
+        if (!$this->supportedByBrowser()) {
+            return;
+        }
+
         $ck_file = empty($params['basic'])
             ? 'ckeditor.js'
             : 'ckeditor_basic.js';
@@ -59,9 +66,11 @@ class Horde_Editor_Ckeditor extends Horde_Editor
      */
     public function supportedByBrowser()
     {
-        global $browser;
+        if (!$this->_browser) {
+            return true;
+        }
 
-        switch ($browser->getBrowser()) {
+        switch ($this->_browser->getBrowser()) {
         case 'webkit':
         case 'msie':
         case 'mozilla':
@@ -70,7 +79,7 @@ class Horde_Editor_Ckeditor extends Horde_Editor
             // Firefox: 1.5+
             // Opera: 9.5+
             // Safari: 3.0+
-            return $browser->hasFeature('rte');
+            return $this->_browser->hasFeature('rte');
 
         default:
             return false;
diff --git a/framework/Editor/lib/Horde/Editor/Exception.php b/framework/Editor/lib/Horde/Editor/Exception.php
new file mode 100644 (file)
index 0000000..3f852e0
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Exception handler for the Horde_Editor package.
+ *
+ * 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.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @package  Editor
+ */
+class Horde_Editor_Exception extends Horde_Exception_Prior
+{
+}
index 71693f2..ad6e7bf 100644 (file)
@@ -8,10 +8,11 @@
  * See the enclosed file COPYING for license information (LGPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  *
- * @author  Nuno Loureiro <nuno@co.sapo.pt>
- * @author  Jan Schneider <jan@horde.org>
- * @author  Michael Slusarz <slusarz@horde.org>
- * @package Horde_Editor
+ * @author   Nuno Loureiro <nuno@co.sapo.pt>
+ * @author   Jan Schneider <jan@horde.org>
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @package  Editor
  */
 class Horde_Editor_Fckeditor extends Horde_Editor
 {
@@ -20,13 +21,19 @@ class Horde_Editor_Fckeditor extends Horde_Editor
      *
      * @param array $params  The following configuration parameters:
      * <pre>
-     * 'id' - The ID of the text area to turn into an editor.
-     * 'no_notify' - Don't output JS code automatically. Code will be stored
-     *               for access via getJS().
+     * 'id' - (string) The ID of the text area to turn into an editor.
+     * 'no_notify' - (boolean) Don't output JS code automatically. Code will
+     *               be stored for access via getJS().
      * </pre>
      */
     public function __construct($params = array())
     {
+        parent::__construct($params);
+
+        if (!$this->supportedByBrowser()) {
+            return;
+        }
+
         $fck_path = $GLOBALS['registry']->get('webroot', 'horde') . '/services/editor/fckeditor/';
         $js = array(
             'var oFCKeditor = new FCKeditor("' . $params['id'] . '")',
@@ -50,9 +57,11 @@ class Horde_Editor_Fckeditor extends Horde_Editor
      */
     public function supportedByBrowser()
     {
-        global $browser;
+        if (!$this->_browser) {
+            return true;
+        }
 
-        switch ($browser->getBrowser()) {
+        switch ($this->_browser->getBrowser()) {
         case 'webkit':
         case 'msie':
         case 'mozilla':
@@ -61,7 +70,7 @@ class Horde_Editor_Fckeditor extends Horde_Editor
             // Firefox: 1.5+
             // Opera: 9.5+
             // Safari: 3.0+
-            return $browser->hasFeature('rte');
+            return $this->_browser->hasFeature('rte');
 
         default:
             return false;
index 3d5c8f8..6827a55 100644 (file)
@@ -30,7 +30,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Removed Xinha driver.
+ <notes>* Removed dependency on Horde_Core.
+ * Removed Xinha driver.
  * Added CKEditor driver.
  * Initial Horde 4 package.
  </notes>
@@ -40,6 +41,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <dir name="Horde">
      <dir name="Editor">
       <file name="Ckeditor.php" role="php" />
+      <file name="Exception.php" role="php" />
       <file name="Fckeditor.php" role="php" />
      </dir> <!-- /lib/Horde/Editor -->
      <file name="Editor.php" role="php" />
@@ -60,6 +62,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <channel>pear.horde.org</channel>
    </package>
    <package>
+    <name>Exception</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Util</name>
     <channel>pear.horde.org</channel>
    </package>
@@ -68,6 +74,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
  <phprelease>
   <filelist>
    <install name="lib/Horde/Editor/Ckeditor.php" as="Horde/Editor/Ckeditor.php" />
+   <install name="lib/Horde/Editor/Exception.php" as="Horde/Editor/Exception.php" />
    <install name="lib/Horde/Editor/Fckeditor.php" as="Horde/Editor/Fckeditor.php" />
    <install name="lib/Horde/Editor.php" as="Horde/Editor.php" />
   </filelist>
index 9453618..e164d3b 100644 (file)
@@ -155,8 +155,8 @@ class Horde_Form_VarRenderer_Xhtml extends Horde_Form_VarRenderer
                         $var->isDisabled() ? ' disabled="disabled"' : '',
                         htmlspecialchars($var->getValue($vars)));
 
-        if ($var->type->hasHelper('rte') && $browser->hasFeature('rte')) {
-            $editor = Horde_Editor::singleton('ckeditor', array('id' => $var->getVarName()));
+        if ($var->type->hasHelper('rte')) {
+            $GLOBALS['injector']->getInstance('Horde_Editor')->getEditor('ckeditor', array('id' => $var->getVarName()));
         }
 
         if ($var->type->hasHelper() && $browser->hasFeature('javascript')) {
index 8d8dba4..f029d7e 100644 (file)
@@ -289,8 +289,8 @@ class Horde_Ui_VarRenderer_Html extends Horde_Ui_VarRenderer
                         $var->isDisabled() ? ' disabled="disabled"' : '',
                         @htmlspecialchars($var->getValue($vars), ENT_QUOTES, $this->_charset));
 
-        if ($var->type->hasHelper('rte') && $browser->hasFeature('rte')) {
-            $editor = Horde_Editor::singleton('fckeditor', array('id' => $varname, 'relativelinks' => $var->type->hasHelper('relativelinks')));
+        if ($var->type->hasHelper('rte')) {
+            $GLOBALS['injector']->getInstance('Horde_Editor')->getEditor('fckeditor', array('id' => $varname, 'relativelinks' => $var->type->hasHelper('relativelinks')));
         }
 
         if ($var->type->hasHelper() && $browser->hasFeature('javascript')) {
index 85846f1..0c4bdfd 100644 (file)
@@ -55,7 +55,7 @@ case 'edit_file':
         echo Horde::wrapInlineScript(array('window.close();'));
     }
     if ($mime_type == 'text/html') {
-        $editor = Horde_Editor::singleton('ckeditor', array('id' => 'content'));
+        $injector->getInstance('Horde_Editor')->getEditor('ckeditor', array('id' => 'content'));
     }
     require GOLLEM_TEMPLATES . '/common-header.inc';
     Gollem::status();
index 32b6fc0..71aa90c 100644 (file)
@@ -452,7 +452,7 @@ class IMP_Auth
 
         /* Is the HTML editor available? */
         $imp_ui = new IMP_Ui_Compose();
-        $editor = Horde_Editor::singleton('Ckeditor', array('no_notify' => true));
+        $editor = $GLOBALS['injector']->getInstance('Horde_Editor')->getEditor('Ckeditor', array('no_notify' => true));
         $sess['rteavail'] = $editor->supportedByBrowser();
 
         /* Set view in session/cookie. */
index bdddb09..f7c6da3 100644 (file)
@@ -145,7 +145,7 @@ class IMP_Ui_Compose
      */
     public function initRTE($basic = false)
     {
-        $editor = Horde_Editor::singleton('Ckeditor', array('basic' => $basic));
+        $GLOBALS['injector']->getInstance('Horde_Editor')->getEditor('Ckeditor', array('basic' => $basic));
 
         $config = array(
             /* To more closely match "normal" textarea behavior, send <BR> on
index 2a818c3..27bf694 100644 (file)
@@ -97,7 +97,7 @@ if ($updated) {
 }
 
 if ($stationery['t'] == 'html') {
-    $editor = Horde_Editor::singleton('Fckeditor', array('id' => 'content'));
+    $injector->getInstance('Horde_Editor')->getEditor('Ckeditor', array('id' => 'content'));
 }
 
 /* Show the header. */
index 65c007e..11fd755 100644 (file)
@@ -600,7 +600,7 @@ if ($form->validate()) {
 
 // Add editor now to avoud JS error notifications no redirect
 foreach ($conf['attributes']['languages'] as $key) {
-    Horde_Editor::singleton('Ckeditor', array('id' => "content_$key"));
+    $injector->getInstance('Horde_Editor')->getEditor('Ckeditor', array('id' => 'content_' . $key));
 }
 
 require_once NEWS_TEMPLATES . '/common-header.inc';