Skip to content

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php" : ">=5.3.0",
"swiftmailer/swiftmailer": "~5.2"
"swiftmailer/swiftmailer": "~5.2|~6.0"
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ~ include 7.0@dev but not with ^.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ~ include 7.0@dev but not with ^.

Hm... Really?

I think they explicitly mention that it will not install versions having major number different from 6 @alquerci

Note: Although 2.0-beta.1 is strictly before 2.0, a version constraint like ~1.2 would not install it. As said above ~1.2 only means the .2 can change but the 1. part is fixed.

https://getcomposer.org/doc/articles/versions.md#tilde-version-range-

Copy link
Contributor

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.

The ^ operator behaves very similarly, but it sticks closer to semantic versioning, and will always allow non-breaking updates.

https://getcomposer.org/doc/articles/versions.md#caret-version-range-

#learningAllDays

},
"require-dev": {
"psr/log": "*"
Expand Down
143 changes: 89 additions & 54 deletions lib/mailer/sfMailer.class.php
100755 → 100644
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.
Expand All @@ -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',
Expand Down Expand Up @@ -63,7 +143,7 @@ public function __construct(sfEventDispatcher $dispatcher, $options)
'transport' => array(
'class' => 'Swift_MailTransport',
'param' => array(),
),
),
), $options);

$constantName = 'sfMailer::'.strtoupper($options['delivery_strategy']);
Expand Down Expand Up @@ -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()
->setFrom($from)
->setTo($to)
->setSubject($subject)
->setBody($body)
;
}

/**
* Sends a message.
*
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the type hint.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 :)

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
*
Expand Down
1 change: 0 additions & 1 deletion lib/vendor/swiftmailer
Submodule swiftmailer deleted from 4cc928