Move Swiftmailer to vendor-prefixed

[MAILPOET-2363]
This commit is contained in:
Jan Jakeš
2019-09-24 16:16:51 +02:00
committed by Jack Kitterhing
parent eb3ea8d540
commit 86675dfd4f
11 changed files with 107 additions and 71 deletions

View File

@ -12,7 +12,6 @@
"mtdowling/cron-expression": "^1.1",
"nesbot/carbon": "^1.21",
"soundasleep/html2text": "dev-master",
"swiftmailer/swiftmailer": "5.4.12",
"tburry/pquery": "^1.1.1"
},
"require-dev": {

56
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c641885578d3d2bb8a3853d01d0827fe",
"content-hash": "f65ddd571e509c0289261f02e514f883",
"packages": [
{
"name": "cerdic/css-tidy",
@ -325,60 +325,6 @@
},
"time": "2019-04-24T12:03:33+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v5.4.12",
"source": {
"type": "git",
"url": "https://github.com/swiftmailer/swiftmailer.git",
"reference": "181b89f18a90f8925ef805f950d47a7190e9b950"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/181b89f18a90f8925ef805f950d47a7190e9b950",
"reference": "181b89f18a90f8925ef805f950d47a7190e9b950",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"mockery/mockery": "~0.9.1",
"symfony/phpunit-bridge": "~3.2"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "5.4-dev"
}
},
"autoload": {
"files": [
"lib/swift_required.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Chris Corbyn"
},
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
}
],
"description": "Swiftmailer, free feature-rich PHP mailer",
"homepage": "https://swiftmailer.symfony.com",
"keywords": [
"email",
"mail",
"mailer"
],
"time": "2018-07-31T09:26:32+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.12.0",

View File

@ -17,9 +17,6 @@ class RequirementsChecker {
public $vendor_classes = [
'\ORM',
'\Model',
'\Swift_Mailer',
'\Swift_SmtpTransport',
'\Swift_Message',
'\Carbon\Carbon',
'\Sudzy\ValidModel',
'\Sudzy\ValidationException',

View File

@ -6,6 +6,7 @@ use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
use MailPoet\Mailer\Methods\ErrorMappers\AmazonSESMapper;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Swift_Message;
class AmazonSES {
public $aws_access_key;
@ -108,7 +109,7 @@ class AmazonSES {
}
function createMessage($newsletter, $subscriber, $extra_params = []) {
$message = \Swift_Message::newInstance()
$message = Swift_Message::newInstance()
->setTo($this->processSubscriber($subscriber))
->setFrom([
$this->sender['from_email'] => $this->sender['from_name'],
@ -132,7 +133,7 @@ class AmazonSES {
return $message;
}
function encodeMessage(\Swift_Message $message) {
function encodeMessage(Swift_Message $message) {
return base64_encode($message->toString());
}

View File

@ -6,6 +6,7 @@ use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\SubscriberError;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Swift_RfcComplianceException;
class AmazonSESMapper {
use BlacklistErrorMapperTrait;
@ -15,7 +16,7 @@ class AmazonSESMapper {
function getErrorFromException(\Exception $e, $subscriber) {
$level = MailerError::LEVEL_HARD;
if ($e instanceof \Swift_RfcComplianceException) {
if ($e instanceof Swift_RfcComplianceException) {
$level = MailerError::LEVEL_SOFT;
}
$subscriber_errors = [new SubscriberError($subscriber, null)];

View File

@ -6,6 +6,7 @@ use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\SubscriberError;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Swift_RfcComplianceException;
class SMTPMapper {
use BlacklistErrorMapperTrait;
@ -22,7 +23,7 @@ class SMTPMapper {
$message = explode(PHP_EOL, $e->getMessage());
$level = MailerError::LEVEL_HARD;
if ($e instanceof \Swift_RfcComplianceException) {
if ($e instanceof Swift_RfcComplianceException) {
$level = MailerError::LEVEL_SOFT;
}
$subscriber_errors = [new SubscriberError($subscriber, null)];

View File

@ -6,6 +6,11 @@ use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Swift_Mailer;
use MailPoetVendor\Swift_Message;
use MailPoetVendor\Swift_Plugins_LoggerPlugin;
use MailPoetVendor\Swift_Plugins_Loggers_ArrayLogger;
use MailPoetVendor\Swift_SmtpTransport;
class SMTP {
public $host;
@ -45,8 +50,8 @@ class SMTP {
$return_path :
$this->sender['from_email'];
$this->mailer = $this->buildMailer();
$this->mailer_logger = new \Swift_Plugins_Loggers_ArrayLogger();
$this->mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($this->mailer_logger));
$this->mailer_logger = new Swift_Plugins_Loggers_ArrayLogger();
$this->mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($this->mailer_logger));
$this->error_mapper = $error_mapper;
$this->blacklist = new BlacklistCheck();
}
@ -73,7 +78,7 @@ class SMTP {
}
function buildMailer() {
$transport = \Swift_SmtpTransport::newInstance(
$transport = Swift_SmtpTransport::newInstance(
$this->host, $this->port, $this->encryption);
$connection_timeout = $this->wp->applyFilters('mailpoet_mailer_smtp_connection_timeout', self::SMTP_CONNECTION_TIMEOUT);
$transport->setTimeout($connection_timeout);
@ -83,11 +88,11 @@ class SMTP {
->setPassword($this->password);
}
$transport = $this->wp->applyFilters('mailpoet_mailer_smtp_transport_agent', $transport);
return \Swift_Mailer::newInstance($transport);
return Swift_Mailer::newInstance($transport);
}
function createMessage($newsletter, $subscriber, $extra_params = []) {
$message = \Swift_Message::newInstance()
$message = Swift_Message::newInstance()
->setTo($this->processSubscriber($subscriber))
->setFrom(
[

View File

@ -7,6 +7,7 @@
"gregwar/captcha": "^1.1",
"monolog/monolog": "^1.23",
"sabberworm/php-css-parser": "^8.1",
"swiftmailer/swiftmailer": "5.4.12",
"symfony/dependency-injection": "3.4.26",
"symfony/polyfill-mbstring": "^1.11",
"symfony/polyfill-php72": "^1.11",
@ -21,7 +22,8 @@
"../tools/vendor/php-scoper.phar add-prefix",
"mv ./build/* ../vendor-prefixed",
"php fix-twig.php",
"php fix-doctrine.php"
"php fix-doctrine.php",
"php fix-swiftmailer.php"
]
},
"config": {

56
prefixer/composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "74021a12e7237e4a9994dff6fc888b6e",
"content-hash": "3c34a814f0565a78f75d50814f8a165d",
"packages": [
{
"name": "doctrine/annotations",
@ -883,6 +883,60 @@
],
"time": "2018-07-13T13:23:56+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v5.4.12",
"source": {
"type": "git",
"url": "https://github.com/swiftmailer/swiftmailer.git",
"reference": "181b89f18a90f8925ef805f950d47a7190e9b950"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/181b89f18a90f8925ef805f950d47a7190e9b950",
"reference": "181b89f18a90f8925ef805f950d47a7190e9b950",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"mockery/mockery": "~0.9.1",
"symfony/phpunit-bridge": "~3.2"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "5.4-dev"
}
},
"autoload": {
"files": [
"lib/swift_required.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Chris Corbyn"
},
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
}
],
"description": "Swiftmailer, free feature-rich PHP mailer",
"homepage": "https://swiftmailer.symfony.com",
"keywords": [
"email",
"mail",
"mailer"
],
"time": "2018-07-31T09:26:32+00:00"
},
{
"name": "symfony/console",
"version": "v3.4.29",

29
prefixer/fix-swiftmailer.php Executable file
View File

@ -0,0 +1,29 @@
<?php
// throw exception if anything fails
set_error_handler(function ($severity, $message, $file, $line) {
throw new ErrorException($message, 0, $severity, $file, $line);
});
// fix Swiftmailer namespaces in string not being correctly prefixed
$iterator = new RecursiveDirectoryIterator(__DIR__ . '/../vendor-prefixed/swiftmailer', RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
if (substr($file, -3) === 'php') {
$data = file_get_contents($file);
$data = preg_replace("/'(Swift_[^']*?::)/", "'MailPoetVendor\\\\\\\\$1", $data);
$data = preg_replace("/InstanceOf\('(Swift_[^']*?')/", "InstanceOf('MailPoetVendor\\\\\\\\$1", $data);
$data = preg_replace("/registerAutoload\('(_swift[^']*?')/", "registerAutoload('MailPoetVendor\\\\\\\\$1", $data);
$data = str_replace('SWIFT_INIT_LOADED', 'MAILPOET_SWIFT_INIT_LOADED', $data);
// require-once 'swift_init.php' in classes since prefixed version won't use Swift's autoloader
if (strpos($file, 'classes') !== false) {
$path = substr($file, strpos($file, 'classes'));
$nesting_level = substr_count(str_replace('\\', '/', $path), '/');
$search = 'namespace MailPoetVendor;';
$require_path = str_repeat('/..', $nesting_level) . '/swift_init.php';
$data = str_replace($search, "$search\n\nrequire_once __DIR__ . '$require_path';", $data);
}
file_put_contents($file, $data);
}
}

View File

@ -7,6 +7,7 @@ use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Swift_RfcComplianceException;
class SMTPMapperTest extends \MailPoetUnitTest {
@ -37,7 +38,7 @@ class SMTPMapperTest extends \MailPoetUnitTest {
function testItCreatesSoftErrorForInvalidEmail() {
$message = 'Invalid email';
$error = $this->mapper->getErrorFromException(new \Swift_RfcComplianceException($message), 'john@rambo.com');
$error = $this->mapper->getErrorFromException(new Swift_RfcComplianceException($message), 'john@rambo.com');
expect($error->getLevel())->equals(MailerError::LEVEL_SOFT);
}