Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Drop dependency on zendframework/zend-loader #186

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ transport. Of course, you can also implement your own transport by implementing
the `Mail\Transport\TransportInterface`.

- File issues at https://github.com/zendframework/zend-mail/issues
- Documentation is at https://zendframework.github.io/zend-mail/
- Documentation is at https://docs.zendframework.com/zend-mail/
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"require": {
"php": "^7.0 || ^5.6",
"ext-iconv": "*",
"zendframework/zend-loader": "^2.5",
"zendframework/zend-mime": "^2.5",
"zendframework/zend-stdlib": "^2.7 || ^3.0",
"zendframework/zend-validator": "^2.6"
Expand Down
46 changes: 1 addition & 45 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 0 additions & 45 deletions src/Header/HeaderLoader.php

This file was deleted.

68 changes: 36 additions & 32 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Countable;
use Iterator;
use Traversable;
use Zend\Loader\PluginClassLocator;

/**
* Basic mail headers collection functionality
Expand All @@ -28,10 +27,29 @@ class Headers implements Countable, Iterator
/** @var string Start of Line when folding */
const FOLDING = "\r\n ";

/**
* @var \Zend\Loader\PluginClassLoader
*/
protected $pluginClassLoader = null;
const HEADERS_CLASS_MAP = [
Copy link
Member

Choose a reason for hiding this comment

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

This is not configurable.
Please move this into a variable in separate class with methods get($name), has($name), add($name, $class), remove($name)

Copy link
Author

Choose a reason for hiding this comment

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

I didn't realize it had to be configurable. I'll fix it right away.

Copy link
Author

Choose a reason for hiding this comment

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

If we move the logic to a class, should it be handled as the HeaderLoader used to?

Do we want to keep exposing it via public getter?

'bcc' => Header\Bcc::class,
'cc' => Header\Cc::class,
'contenttype' => Header\ContentType::class,
'content_type' => Header\ContentType::class,
'content-type' => Header\ContentType::class,
'contenttransferencoding' => Header\ContentTransferEncoding::class,
'content_transfer_encoding' => Header\ContentTransferEncoding::class,
'content-transfer-encoding' => Header\ContentTransferEncoding::class,
'date' => Header\Date::class,
'from' => Header\From::class,
'message-id' => Header\MessageId::class,
'mimeversion' => Header\MimeVersion::class,
'mime_version' => Header\MimeVersion::class,
'mime-version' => Header\MimeVersion::class,
'received' => Header\Received::class,
'replyto' => Header\ReplyTo::class,
'reply_to' => Header\ReplyTo::class,
'reply-to' => Header\ReplyTo::class,
'sender' => Header\Sender::class,
'subject' => Header\Subject::class,
'to' => Header\To::class,
];

/**
* @var array key names for $headers array
Expand Down Expand Up @@ -117,31 +135,6 @@ public static function fromString($string, $EOL = self::EOL)
return $headers;
}

/**
* Set an alternate implementation for the PluginClassLoader
*
* @param PluginClassLocator $pluginClassLoader
* @return Headers
*/
public function setPluginClassLoader(PluginClassLocator $pluginClassLoader)
{
$this->pluginClassLoader = $pluginClassLoader;
return $this;
}

/**
* Return an instance of a PluginClassLocator, lazyload and inject map if necessary
*
* @return PluginClassLocator
*/
public function getPluginClassLoader()
{
if ($this->pluginClassLoader === null) {
$this->pluginClassLoader = new Header\HeaderLoader();
}
return $this->pluginClassLoader;
}

/**
* Set the header encoding
*
Expand Down Expand Up @@ -478,7 +471,7 @@ public function forceLoading()
public function loadHeader($headerLine)
{
list($name, ) = Header\GenericHeader::splitHeaderLine($headerLine);
$class = $this->getPluginClassLoader()->load($name) ?: Header\GenericHeader::class;
$class = $this->resolveHeaderClass($name);
return $class::fromString($headerLine);
}

Expand All @@ -491,7 +484,7 @@ protected function lazyLoadHeader($index)
$current = $this->headers[$index];

$key = $this->headersKeys[$index];
$class = ($this->getPluginClassLoader()->load($key)) ?: 'Zend\Mail\Header\GenericHeader';
$class = $this->resolveHeaderClass($key);

$encoding = $current->getEncoding();
$headers = $class::fromString($current->toString());
Expand Down Expand Up @@ -523,4 +516,15 @@ protected function normalizeFieldName($fieldName)
{
return str_replace(['-', '_', ' ', '.'], '', strtolower($fieldName));
}

/**
* @param string $key
* @return string
*/
private function resolveHeaderClass($key)
{
$key = strtolower($key);
$headerExists = array_key_exists($key, self::HEADERS_CLASS_MAP);
return $headerExists ? self::HEADERS_CLASS_MAP[$key] : Header\GenericHeader::class;
}
}
12 changes: 3 additions & 9 deletions test/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ public function testHeadersFromStringFactoryCreatesMultipleObjects()
public function testHeadersFromStringMultiHeaderWillAggregateLazyLoadedHeaders()
{
$headers = new Mail\Headers();
/* @var $pcl \Zend\Loader\PluginClassLoader */
$pcl = $headers->getPluginClassLoader();
$pcl->registerPlugin('foo', 'Zend\Mail\Header\GenericMultiHeader');
$headers->addHeaderLine('foo: bar1,bar2,bar3');
$headers->addHeaderLine('foo', ['bar1@domain.com', 'bar2@domain.com', 'bar3@domain.com']);
$headers->forceLoading();
$this->assertEquals(3, $headers->count());
}
Expand Down Expand Up @@ -382,12 +379,9 @@ public static function expectedHeaders()
/**
* @dataProvider expectedHeaders
*/
public function testDefaultPluginLoaderIsSeededWithHeaders($plugin, $class)
public function testHeadersMapResolvesProperHeader($plugin, $class)
{
$headers = new Mail\Headers();
$loader = $headers->getPluginClassLoader();
$test = $loader->load($plugin);
$this->assertEquals($class, $test);
$this->assertEquals(Mail\Headers::HEADERS_CLASS_MAP[$plugin], $class);
}

public function testClone()
Expand Down