Use injector to load IMP_Compose
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Apr 2010 10:45:08 +0000 (04:45 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Apr 2010 20:03:54 +0000 (14:03 -0600)
13 files changed:
imp/compose-dimp.php
imp/compose-mimp.php
imp/compose.php
imp/lib/Ajax/Application.php
imp/lib/Application.php
imp/lib/Auth.php
imp/lib/Compose.php
imp/lib/Injector/Binder/Compose.php [new file with mode: 0644]
imp/lib/Injector/Factory/Compose.php [new file with mode: 0644]
imp/lib/Message.php
imp/lib/Spam.php
imp/lib/Views/Compose.php
imp/view.php

index 109e61e..a241531 100644 (file)
@@ -57,7 +57,7 @@ if (!$prefs->isLocked('default_identity') && isset($vars->identity)) {
 }
 
 /* Initialize the IMP_Compose:: object. */
-$imp_compose = IMP_Compose::singleton();
+$imp_compose = $injector->getInstance('IMP_Compose')->getOb();
 
 /* Init IMP_Ui_Compose:: object. */
 $imp_ui = new IMP_Ui_Compose();
index 29b0b41..4227787 100644 (file)
@@ -70,7 +70,7 @@ $save_sent_mail = $imp_imap->isReadOnly($sent_mail_folder)
 $compose_disable = !IMP::canCompose();
 
 /* Initialize objects. */
-$imp_compose = IMP_Compose::singleton($vars->composeCache);
+$imp_compose = $injector->getInstance('IMP_Compose')->getOb($vars->composeCache);
 $imp_ui = new IMP_Ui_Compose();
 
 foreach (array_keys($display_hdrs) as $val) {
index 69044c7..eb37290 100644 (file)
@@ -108,7 +108,7 @@ if ($readonly_sentmail) {
 }
 
 /* Initialize the IMP_Compose:: object. */
-$imp_compose = IMP_Compose::singleton($vars->composeCache);
+$imp_compose = $injector->getInstance('IMP_Compose')->getOb($vars->composeCache);
 $imp_compose->pgpAttachPubkey((bool) $vars->pgp_attach_pubkey);
 $imp_compose->userLinkAttachments((bool) $vars->link_attachments);
 
index 8c51a81..b66280b 100644 (file)
@@ -1067,7 +1067,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
      */
     public function cancelCompose()
     {
-        $imp_compose = IMP_Compose::singleton($this->_vars->imp_compose);
+        $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb($this->_vars->imp_compose);
         $imp_compose->destroy('cancel');
 
         return true;
@@ -1085,7 +1085,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
      */
     public function deleteDraft()
     {
-        IMP_Compose::singleton($this->_vars->imp_compose)->destroy('cancel');
+        $GLOBALS['injector']->getInstance('IMP_Compose')->getOb($this->_vars->imp_compose)->destroy('cancel');
         return true;
     }
 
@@ -1103,7 +1103,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
     public function deleteAttach()
     {
         if (isset($this->_vars->atc_indices)) {
-            $imp_compose = IMP_Compose::singleton($this->_vars->imp_compose);
+            $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb($this->_vars->imp_compose);
             foreach ($imp_compose->deleteAttachment($this->_vars->atc_indices) as $val) {
                 $GLOBALS['notification']->push(sprintf(_("Deleted attachment \"%s\"."), Horde_Mime::decode($val)), 'horde.success');
             }
@@ -1287,7 +1287,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
      */
     public function addAttachment()
     {
-        $imp_compose = IMP_Compose::singleton($this->_vars->composeCache);
+        $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb($this->_vars->composeCache);
 
         $result = new stdClass;
         $result->action = 'addAttachment';
@@ -1445,7 +1445,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
         $result->success = 1;
 
         try {
-            $imp_compose = IMP_Compose::singleton($this->_vars->redirect_composeCache);
+            $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb($this->_vars->redirect_composeCache);
             $imp_compose->sendRedirectMessage($this->_vars->redirect_to);
 
             $result->mbox = $imp_compose->getMetadata('mailbox');
@@ -1523,7 +1523,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
         }
         $headers['subject'] = $this->_vars->subject;
 
-        $imp_compose = IMP_Compose::singleton($this->_vars->composeCache);
+        $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb($this->_vars->composeCache);
 
         return array($result, $imp_compose, $headers, $identity);
     }
@@ -1533,7 +1533,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
      */
     protected function _initCompose()
     {
-        $imp_compose = IMP_Compose::singleton($this->_vars->imp_compose);
+        $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb($this->_vars->imp_compose);
         if (!($imp_contents = $imp_compose->getContentsOb())) {
             $indices = $GLOBALS['imp_imap']->ob()->utils->fromSequenceString($this->_vars->uid);
             $i = each($indices);
index 0288e49..ef42dc0 100644 (file)
@@ -76,8 +76,7 @@ class IMP_Application extends Horde_Registry_Application
     {
         if (($e->getCode() == Horde_Registry::AUTH_FAILURE) &&
             Horde_Util::getFormData('composeCache')) {
-            $imp_compose = IMP_Compose::singleton();
-            $imp_compose->sessionExpireDraft();
+            $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb()->sessionExpireDraft();
         }
     }
 
@@ -93,6 +92,7 @@ class IMP_Application extends Horde_Registry_Application
     {
         /* Add IMP-specific binders. */
         $binders = array(
+            'IMP_Compose' => new IMP_Injector_Binder_Compose(),
             'IMP_Contents' => new IMP_Injector_Binder_Contents(),
             'IMP_Crypt_Pgp' => new IMP_Injector_Binder_Pgp(),
             'IMP_Crypt_Smime' => new IMP_Injector_Binder_Smime(),
index fd02805..6b65757 100644 (file)
@@ -498,8 +498,7 @@ class IMP_Auth
         }
 
         /* Check for drafts due to session timeouts. */
-        $imp_compose = IMP_Compose::singleton();
-        $imp_compose->recoverSessionExpireDraft();
+        $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb()->recoverSessionExpireDraft();
 
         self::_logMessage('login', 'NOTICE');
     }
index a589804..edf85f9 100644 (file)
@@ -23,13 +23,6 @@ class IMP_Compose
     const VFS_DRAFTS_PATH = '.horde/imp/drafts';
 
     /**
-     * Singleton instances.
-     *
-     * @var array
-     */
-    static protected $_instances = array();
-
-    /**
      * The cached attachment data.
      *
      * @var array
@@ -87,36 +80,11 @@ class IMP_Compose
     protected $_modified = false;
 
     /**
-     * Attempts to return a reference to a concrete IMP_Compose instance.
-     *
-     * If an IMP_Cacheid object exists with the given cacheid, recreate that
-     * that object.  Else, create a new instance.
-     *
-     * @param string $cacheid  The cache ID string.
-     *
-     * @return IMP_Compose  The IMP_Compose object.
-     */
-    static public function singleton($cacheid = null)
-    {
-        if (!empty($cacheid) && !isset(self::$_instances[$cacheid])) {
-            $obs = $GLOBALS['injector']->getInstance('Horde_SessionObjects');
-            self::$_instances[$cacheid] = $obs->query($cacheid);
-        }
-
-        if (empty($cacheid) || empty(self::$_instances[$cacheid])) {
-            $cacheid = empty($cacheid) ? uniqid(mt_rand()) : $cacheid;
-            self::$_instances[$cacheid] = new self($cacheid);
-        }
-
-        return self::$_instances[$cacheid];
-    }
-
-    /**
      * Constructor.
      *
      * @param string $cacheid  The cache ID string.
      */
-    protected function __construct($cacheid)
+    public function __construct($cacheid)
     {
         $this->_cacheid = $cacheid;
         $this->__wakeup();
diff --git a/imp/lib/Injector/Binder/Compose.php b/imp/lib/Injector/Binder/Compose.php
new file mode 100644 (file)
index 0000000..34188cb
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Binder for IMP_Compose::.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Michael Slusarz <slusarz@horde.org>
+ * @package IMP
+ */
+class IMP_Injector_Binder_Compose implements Horde_Injector_Binder
+{
+    /**
+     */
+    public function create(Horde_Injector $injector)
+    {
+        return new IMP_Injector_Factory_Compose($injector);
+    }
+
+    /**
+     */
+    public function equals(Horde_Injector_Binder $binder)
+    {
+        return false;
+    }
+
+}
diff --git a/imp/lib/Injector/Factory/Compose.php b/imp/lib/Injector/Factory/Compose.php
new file mode 100644 (file)
index 0000000..1563cb2
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+/**
+ * A Horde_Injector:: based IMP_Compose:: factory.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  IMP
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @license  http://www.fsf.org/copyleft/gpl.html GPL
+ * @link     http://pear.horde.org/index.php?package=IMP
+ */
+
+/**
+ * A Horde_Injector:: based IMP_Compose:: factory.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @category Horde
+ * @package  IMP
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @license  http://www.fsf.org/copyleft/gpl.html GPL
+ * @link     http://pear.horde.org/index.php?package=IMP
+ */
+class IMP_Injector_Factory_Compose
+{
+    /**
+     * Instances.
+     *
+     * @var array
+     */
+    private $_instances = array();
+
+    /**
+     * 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_Compose:: instance.
+     *
+     * @param string $cacheid  The cache ID string.
+     *
+     * @return IMP_Compose  The singleton compose instance.
+     * @throws IMP_Exception
+     */
+    public function getOb($cacheid = null)
+    {
+        if (empty($cacheid)) {
+            $cacheid = uniqid(mt_rand());
+        } elseif (!isset($this->_instances[$cacheid])) {
+            $obs = $GLOBALS['injector']->getInstance('Horde_SessionObjects');
+            $this->_instances[$cacheid] = $obs->query($cacheid);
+        }
+
+        if (empty($this->_instances[$cacheid])) {
+            $this->_instances[$cacheid] = new IMP_Compose($cacheid);
+        }
+
+        return $this->_instances[$cacheid];
+    }
+
+}
index a04b1a9..777ea39 100644 (file)
@@ -319,7 +319,6 @@ class IMP_Message
                 $subject = $imp_headers->getValue('subject');
 
                 /* Extract the message body. */
-                $imp_compose = IMP_Compose::singleton();
                 $mime_message = $imp_contents->getMIMEMessage();
                 $body_id = $imp_contents->findBody();
                 $body_part = $mime_message->getPart($body_id);
index df21274..654c56b 100644 (file)
@@ -109,7 +109,7 @@ class IMP_Spam
                     }
 
                     if (!isset($imp_compose)) {
-                        $imp_compose = IMP_Compose::singleton();
+                        $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb();
                         try {
                             $from_line = $GLOBALS['injector']->getInstance('IMP_Identity')->getFromLine();
                         } catch (Horde_Exception $e) {
index 1ae6c13..a97b4e5 100644 (file)
@@ -41,7 +41,7 @@ class IMP_Views_Compose
         if (empty($args['composeCache'])) {
             $composeCache = null;
         } else {
-            $imp_compose = IMP_Compose::singleton($args['composeCache']);
+            $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb($args['composeCache']);
             $composeCache = $args['composeCache'];
         }
 
index 2d548c1..a590648 100644 (file)
@@ -55,7 +55,7 @@ Horde_Registry::appInit('imp', array(
  * message data. Rather, we must use the IMP_Compose object to get the
  * necessary data for Horde_Mime_Part. */
 if ($vars->actionID == 'compose_attach_preview') {
-    $imp_compose = IMP_Compose::singleton($vars->composeCache);
+    $imp_compose = $injector->getInstance('IMP_Compose')->getOb($vars->composeCache);
     $mime = $imp_compose->buildAttachment($vars->id);
     $mime->setMimeId($vars->id);