[Patch] factory methods for Mail_Mime to facilitate subclassing

From: Date: Sun, 19 Sep 2004 22:48:44 +0000
Subject: [Patch] factory methods for Mail_Mime to facilitate subclassing
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-33458@lists.php.net to get a copy of this message
Dear all, this patch adds two factory methods to Mail_Mime's mime.php and mimePart.php to allow for subclassing them more easily. I don't know if you consider it useful, so I'd like to hear some opinions. I'm still a bit unhappy about this solution from a designers point of view: 1. Mail_mimePart (mimePart.php) needs to create a new instance of its own class when adding subparts. Thus, _subPartFactory() should always "return new {nameOfTheCurrentClassGoesHere}($body, $params)". I find it strange having to override this method when subclassing Mail_mimePart, just to type the name of the new subclass again. Does anybody see a way to do this "automagically"? I thought about something like cloning (PHP5?), but also from the conceptual point of view this is not really what we need here - we want a new instance of the same class, not a clone of $this. Also if __clone could get us around explicitly naming the class, you'd have to implement __clone to mimic __construct behavior and you'd lose the ability to make a "real" clone if you need it. This anomaly is not introduced by the patch - it has been in addSubPart() before; the patch isolates it in _subPartFactory. 2. Mail_mime (mime.php) is a kind of "facade" for Mail_mimePart. By overriding Mail_mime's _mimePartFactory method, you can choose on which subclass of Mail_mimePart to work on. But I don't like having to override two factories (in mimePart.php and mime.php) to make subclasses work. I've got the feeling that this anomaly is caused by the "facade" existing independently of the mimeParts. Any ideas? Best regards, Matthias

Index: mime.php =================================================================== RCS file: /repository/pear/Mail_Mime/mime.php,v retrieving revision 1.24 diff -w -u -r1.24 mime.php --- mime.php 4 Jan 2003 11:55:33 -0000 1.24 +++ mime.php 19 Sep 2004 22:11:52 -0000 @@ -36,7 +36,6 @@ // $Id: mime.php,v 1.24 2003/01/04 11:55:33 mj Exp $ require_once 'PEAR.php'; -require_once 'Mail/mimePart.php'; /** * Mime mail composer class. Can handle: text and html bodies, embedded html @@ -122,6 +121,24 @@ } /* + * Factory method for creating a new mime part. + * + * This method facilitates the use of custom Mail_mimePart subclasses; + * override this method to create specialized subclasses of Mail_mimePart. + * + * @access private + * @param $body - The body of the mime part if any, @see Mail_mimePart::Mail_mimePart(). + * @param $params - An associative array of parameters, , @see Mail_mimePart::Mail_mimePart(). + * @return object A newly created instance of the multipart/mixed mimePart object. + * @access private + */ + function &_mimePartFactory($body = '', $params = array()) + { + require_once 'Mail/mimePart.php'; + return new Mail_mimePart($body, $params); + } + + /* * Accessor function to set the body text. Body text is used if * it's not an html mail being sent or else is used to fill the * text/plain part that emails clients who don't support @@ -282,7 +299,7 @@ if (is_object($obj)) { return $obj->addSubpart($text, $params); } else { - return new Mail_mimePart($text, $params); + return $this->_mimePartFactory($text, $params); } } @@ -303,7 +320,7 @@ if (is_object($obj)) { return $obj->addSubpart($this->_htmlbody, $params); } else { - return new Mail_mimePart($this->_htmlbody, $params); + return $this->_mimePartFactory($this->_htmlbody, $params); } } @@ -318,7 +335,7 @@ function &_addMixedPart(){ $params['content_type'] = 'multipart/mixed'; - return new Mail_mimePart('', $params); + return $this->_mimePartFactory('', $params); } /* @@ -337,7 +354,7 @@ if (is_object($obj)) { return $obj->addSubpart('', $params); } else { - return new Mail_mimePart('', $params); + return $this->_mimePartFactory('', $params); } } @@ -357,7 +374,7 @@ if (is_object($obj)) { return $obj->addSubpart('', $params); } else { - return new Mail_mimePart('', $params); + return $this->_mimePartFactory('', $params); } } @@ -650,3 +667,4 @@ } // End of class ?> + Index: mimePart.php =================================================================== RCS file: /repository/pear/Mail_Mime/mimePart.php,v retrieving revision 1.11 diff -w -u -r1.11 mimePart.php --- mimePart.php 4 Jan 2003 11:55:34 -0000 1.11 +++ mimePart.php 19 Sep 2004 22:11:52 -0000 @@ -246,6 +246,25 @@ return $encoded; } + /* + * Factory method for creating a new subpart. + * + * This method facilitates the use of custom Mail_mimePart subclasses; + * also @see Mail_mime::_mimePartFactory(). + * + * @access private + * @param $body - The body of the mime part if any, @see Mail_mimePart(), addSubPart() + * @param $params - An associative array of parameters, @see Mail_mimePart(), addSubPart() + * @return object A newly created instance of this class itself + * @access private + */ + function &_subPartFactory($body, $params) + { + /* Don't forget to override this when subclassing Mail_mimePart. + You'll want to "return new _NameOfThisClass_($body, $params). */ + return new Mail_mimePart($body, $params); + } + /** * &addSubPart() * @@ -263,7 +282,7 @@ */ function &addSubPart($body, $params) { - $this->_subparts[] = new Mail_mimePart($body, $params); + $this->_subparts[] =& $this->_subPartFactory($body, $params); return $this->_subparts[count($this->_subparts) - 1]; } @@ -347,3 +366,4 @@ } } // End of class ?> +
« previous php.pear.dev (#33458) next »