-
Notifications
You must be signed in to change notification settings - Fork 181
Feature/swiftmailer 6 compat #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4b56952
aeb69bb
6c4e967
750935f
30dd4d5
c5c4818
2ed880f
66dd314
b557d34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the symfony package. | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
// Defining a base class for sfMailer to handle both, Swiftmailer 5 and Swiftmailer 6. | ||
if(class_exists('Swift') && version_compare(Swift::VERSION, '6.0.0') >= 0) { | ||
class sfMailerBase extends Swift_Mailer | ||
{ | ||
/** | ||
* Sends the given message. | ||
* | ||
* @param Swift_Mime_SimpleMessage $message A transport instance | ||
* @param string[] &$failedRecipients An array of failures by-reference | ||
* | ||
* @return int|false The number of sent emails | ||
*/ | ||
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) | ||
{ | ||
if ($this->force) | ||
{ | ||
$this->force = false; | ||
|
||
if (!$this->realtimeTransport->isStarted()) | ||
{ | ||
$this->realtimeTransport->start(); | ||
} | ||
|
||
return $this->realtimeTransport->send($message, $failedRecipients); | ||
} | ||
|
||
return parent::send($message, $failedRecipients); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function compose($from = null, $to = null, $subject = null, $body = null) | ||
{ | ||
$msg = new Swift_Message($subject); | ||
|
||
return $msg | ||
->setFrom($from) | ||
->setTo($to) | ||
->setBody($body) | ||
; | ||
} | ||
} | ||
} else { | ||
class sfMailerBase extends Swift_Mailer | ||
{ | ||
/** | ||
* Sends the given message. | ||
* | ||
* @param Swift_Mime_Message $message A transport instance | ||
* @param string[] &$failedRecipients An array of failures by-reference | ||
* | ||
* @return int|false The number of sent emails | ||
*/ | ||
public function send(Swift_Mime_Message $message, &$failedRecipients = null) | ||
{ | ||
if ($this->force) | ||
{ | ||
$this->force = false; | ||
|
||
if (!$this->realtimeTransport->isStarted()) | ||
{ | ||
$this->realtimeTransport->start(); | ||
} | ||
|
||
return $this->realtimeTransport->send($message, $failedRecipients); | ||
} | ||
|
||
return parent::send($message, $failedRecipients); | ||
} | ||
|
||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function compose($from = null, $to = null, $subject = null, $body = null) | ||
{ | ||
$msg = Swift_Message::newInstance($subject); | ||
|
||
return $msg | ||
->setFrom($from) | ||
->setTo($to) | ||
->setBody($body) | ||
; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* sfMailer is the main entry point for the mailer system. | ||
|
@@ -18,7 +98,7 @@ | |
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* @version SVN: $Id$ | ||
*/ | ||
class sfMailer extends Swift_Mailer | ||
class sfMailer extends sfMailerBase | ||
{ | ||
const | ||
REALTIME = 'realtime', | ||
|
@@ -63,7 +143,7 @@ public function __construct(sfEventDispatcher $dispatcher, $options) | |
'transport' => array( | ||
'class' => 'Swift_MailTransport', | ||
'param' => array(), | ||
), | ||
), | ||
), $options); | ||
|
||
$constantName = 'sfMailer::'.strtoupper($options['delivery_strategy']); | ||
|
@@ -227,26 +307,6 @@ public function setDeliveryAddress($address) | |
} | ||
} | ||
|
||
/** | ||
* Creates a new message. | ||
* | ||
* @param string|array $from The from address | ||
* @param string|array $to The recipient(s) | ||
* @param string $subject The subject | ||
* @param string $body The body | ||
* | ||
* @return Swift_Message A Swift_Message instance | ||
*/ | ||
public function compose($from = null, $to = null, $subject = null, $body = null) | ||
{ | ||
return Swift_Message::newInstance() | ||
thirsch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->setFrom($from) | ||
->setTo($to) | ||
->setSubject($subject) | ||
->setBody($body) | ||
; | ||
} | ||
|
||
/** | ||
* Sends a message. | ||
* | ||
|
@@ -274,31 +334,6 @@ public function sendNextImmediately() | |
return $this; | ||
} | ||
|
||
/** | ||
* Sends the given message. | ||
* | ||
* @param Swift_Transport $transport A transport instance | ||
* @param string[] &$failedRecipients An array of failures by-reference | ||
* | ||
* @return int|false The number of sent emails | ||
*/ | ||
public function send(Swift_Mime_Message $message, &$failedRecipients = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the type hint. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, that was way to simple... ;-) I wasn't aware, that you can remove the typehint and be less specific while extending from another class. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You rise me a doubt. But for sure the test have always right, and the result may depend on the PHP version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it's working. On a new codebase I would not do prefer it that way, but on the legacy stuff it's sufficient to get the compat for swiftmailer 6 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, however, even on new codebase as the type constraint is respected with the call on the parent and the transport. There is a goal to avoid using language version specific code as much as possible to avoid useless complexity. But that's another story depending of the codebase. I give you me point of view. |
||
{ | ||
if ($this->force) | ||
{ | ||
$this->force = false; | ||
|
||
if (!$this->realtimeTransport->isStarted()) | ||
{ | ||
$this->realtimeTransport->start(); | ||
} | ||
|
||
return $this->realtimeTransport->send($message, $failedRecipients); | ||
} | ||
|
||
return parent::send($message, $failedRecipients); | ||
} | ||
|
||
/** | ||
* Sends the current messages in the spool. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dependency constraint should be more strict like that =>
^6.0
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, could you please explain the difference to me? From what I understood on https://getcomposer.org/doc/articles/versions.md#next-significant-release-operators, it will be the same and only differ, if I would introduce the third digit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
~
include7.0@dev
but not with^
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... Really?
I think they explicitly mention that it will not install versions having major number different from
6
@alquercihttps://getcomposer.org/doc/articles/versions.md#tilde-version-range-
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, indeed I wasn't understand in details the difference.
Now yes.
https://getcomposer.org/doc/articles/versions.md#caret-version-range-
#learningAllDays