Skip to content

Commit 8c3cd95

Browse files
committed
Merge pull request #30 from kilburn/options
Add configuration settings
2 parents e152fca + 32f8dce commit 8c3cd95

9 files changed

+183
-28
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.3
54
- 5.4
65
- 5.5
76
- 5.6

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Into this:
4646
## Installation
4747
Begin by installing this package through Composer. Edit your project's `composer.json` file to require `fedeisas/laravel-mail-css-inliner`.
4848

49-
This package needs Laravel 4.x or Laravel 5.x
49+
This package needs Laravel 5.x
5050
```json
5151
{
5252
"require": {
@@ -60,11 +60,17 @@ Next, update Composer from the Terminal:
6060
$ composer update
6161
```
6262

63-
Once this operation completes, the final step is to add the service provider. Open `app/config/app.php`, and add a new item to the providers array.
63+
Once this operation completes, you must add the service provider. Open `app/config/app.php`, and add a new item to the providers array.
6464
```php
6565
'Fedeisas\LaravelMailCssInliner\LaravelMailCssInlinerServiceProvider',
6666
```
6767

68+
At this point the inliner should be already working with the default options. If you want to fine-tune these options, you can do so by publishing the configuration file:
69+
```bash
70+
$ php artisan vendor:publish --provider='Fedeisas\\LaravelMailCssInliner\\LaravelMailCssInlinerServiceProvider'
71+
```
72+
and changing the settings on the generated `config/css-inliner.php` file.
73+
6874
## Contributing
6975
```bash
7076
$ composer install

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=5.3.2",
14-
"illuminate/support": "~4.0|~5.0",
13+
"php": ">=5.4.0",
14+
"illuminate/support": "~5.0",
1515
"tijsverkoyen/css-to-inline-styles": "~1.2"
1616
},
1717
"require-dev" : {

config/css-inliner.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Strip styles
8+
|--------------------------------------------------------------------------
9+
|
10+
| Settings this to false prevents the inliner from removing the style
11+
| definitions that have been inlined.
12+
|
13+
| Notice that media query styles are not inlined, and hence never
14+
| stripped.
15+
|
16+
*/
17+
18+
'strip-styles' => true,
19+
20+
/*
21+
|--------------------------------------------------------------------------
22+
| Remove classes
23+
|--------------------------------------------------------------------------
24+
|
25+
| Settings this to false disables the removal of class attributes from
26+
| your html elements (do not enable this if you use media queries)
27+
|
28+
*/
29+
30+
'strip-classes' => true,
31+
32+
];

src/CssInlinerPlugin.php

+34-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66

77
class CssInlinerPlugin implements \Swift_Events_SendListener
88
{
9+
/**
10+
* @var array
11+
*/
12+
protected $options;
13+
14+
/**
15+
* @param array $options options defined in the configuration file.
16+
*/
17+
public function __construct(array $options)
18+
{
19+
$this->options = $options;
20+
}
21+
922
/**
1023
* @param Swift_Events_SendEvent $evt
1124
*/
@@ -14,10 +27,7 @@ public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
1427
$message = $evt->getMessage();
1528

1629
$converter = new CssToInlineStyles();
17-
$converter->setUseInlineStylesBlock();
18-
$converter->setStripOriginalStyleTags();
19-
20-
$converter->setCleanup();
30+
$this->applySettings($converter);
2131

2232
if ($message->getContentType() === 'text/html' ||
2333
($message->getContentType() === 'multipart/alternative' && $message->getBody()) ||
@@ -35,6 +45,26 @@ public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
3545
}
3646
}
3747

48+
/**
49+
* Applies the configuration settings.
50+
*
51+
* @param CssToInlineStyles $converter
52+
*/
53+
private function applySettings(CssToInlineStyles $converter)
54+
{
55+
// Always enabled because there is no way to specify an external style sheet
56+
// when using this plugin
57+
$converter->setUseInlineStylesBlock();
58+
59+
if ($this->options['strip-styles']) {
60+
$converter->setStripOriginalStyleTags();
61+
}
62+
63+
if ($this->options['strip-classes']) {
64+
$converter->setCleanup();
65+
}
66+
}
67+
3868
/**
3969
* Do nothing
4070
*

src/LaravelMailCssInlinerServiceProvider.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Fedeisas\LaravelMailCssInliner;
22

33
use Illuminate\Support\ServiceProvider;
4+
use Swift_Mailer;
45

56
class LaravelMailCssInlinerServiceProvider extends ServiceProvider
67
{
@@ -19,7 +20,9 @@ class LaravelMailCssInlinerServiceProvider extends ServiceProvider
1920
*/
2021
public function boot()
2122
{
22-
$this->app['mailer']->getSwiftMailer()->registerPlugin(new CssInlinerPlugin());
23+
$this->publishes([
24+
__DIR__.'/../config/css-inliner.php' => config_path('css-inliner.php'),
25+
], 'config');
2326
}
2427

2528
/**
@@ -29,16 +32,17 @@ public function boot()
2932
*/
3033
public function register()
3134
{
32-
// Do nothing
33-
}
35+
$this->mergeConfigFrom(__DIR__.'/../config/css-inliner.php', 'css-inliner');
3436

35-
/**
36-
* Get the services provided by the provider.
37-
*
38-
* @return array
39-
*/
40-
public function provides()
41-
{
42-
return array();
37+
$this->app->singleton('Fedeisas\LaravelMailCssInliner\CssInlinerPlugin', function ($app) {
38+
$options = $app['config']->get('css-inliner');
39+
return new CssInlinerPlugin($options);
40+
});
41+
42+
$this->app->extend('swift.mailer', function (Swift_Mailer $swiftMailer, $app) {
43+
$inlinerPlugin = $app->make('Fedeisas\LaravelMailCssInliner\CssInlinerPlugin');
44+
$swiftMailer->registerPlugin($inlinerPlugin);
45+
return $swiftMailer;
46+
});
4347
}
4448
}

tests/CssInlinerPluginTest.php

+58-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@
44

55
class CssInlinerPluginTest extends PHPUnit_Framework_TestCase
66
{
7-
87
protected $stubs;
98

9+
protected $options;
10+
11+
protected static $stubDefinitions = array(
12+
'plain-text', 'original-html', 'converted-html', 'converted-html-with-classes',
13+
'converted-html-with-styles'
14+
);
15+
1016
public function setUp()
1117
{
12-
$this->stubs['plain-text'] = file_get_contents(__DIR__.'/stubs/plain-text.stub');
13-
$this->stubs['original-html'] = file_get_contents(__DIR__.'/stubs/original-html.stub');
14-
$this->stubs['converted-html'] = file_get_contents(__DIR__.'/stubs/converted-html.stub');
18+
foreach (self::$stubDefinitions as $stub) {
19+
$this->stubs[$stub] = file_get_contents(__DIR__.'/stubs/'.$stub.'.stub');
20+
}
21+
22+
$this->options = require(__DIR__.'/../config/css-inliner.php');
1523
}
1624

1725
/** @test **/
1826
public function itShouldConvertHtmlBody()
1927
{
2028
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
2129

22-
$mailer->registerPlugin(new CssInlinerPlugin());
30+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
2331

2432
$message = Swift_Message::newInstance();
2533

@@ -33,12 +41,54 @@ public function itShouldConvertHtmlBody()
3341
$this->assertEquals($this->stubs['converted-html'], $message->getBody());
3442
}
3543

44+
/** @test **/
45+
public function itShouldConvertHtmlBodyKeepingClasses()
46+
{
47+
$this->options['strip-classes'] = false;
48+
49+
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
50+
51+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
52+
53+
$message = Swift_Message::newInstance();
54+
55+
$message->setFrom('[email protected]');
56+
$message->setTo('[email protected]');
57+
$message->setSubject('Test');
58+
$message->setBody($this->stubs['original-html'], 'text/html');
59+
60+
$mailer->send($message);
61+
62+
$this->assertEquals($this->stubs['converted-html-with-classes'], $message->getBody());
63+
}
64+
65+
/** @test **/
66+
public function itShouldConvertHtmlBodyKeepingStyles()
67+
{
68+
$this->options['strip-styles'] = false;
69+
70+
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
71+
72+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
73+
74+
$message = Swift_Message::newInstance();
75+
76+
$message->setFrom('[email protected]');
77+
$message->setTo('[email protected]');
78+
$message->setSubject('Test');
79+
$message->setBody($this->stubs['original-html'], 'text/html');
80+
81+
$mailer->send($message);
82+
83+
$this->assertEquals($this->stubs['converted-html-with-styles'], $message->getBody());
84+
}
85+
3686
/** @test **/
3787
public function itShouldConvertHtmlBodyAndTextParts()
3888
{
3989
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
4090

41-
$mailer->registerPlugin(new CssInlinerPlugin());
91+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
4292

4393
$message = Swift_Message::newInstance();
4494

@@ -61,7 +111,7 @@ public function itShouldLeavePlainTextUnmodified()
61111
{
62112
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
63113

64-
$mailer->registerPlugin(new CssInlinerPlugin());
114+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
65115

66116
$message = Swift_Message::newInstance();
67117

@@ -82,7 +132,7 @@ public function itShouldConvertHtmlBodyAsAPart()
82132
{
83133
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
84134

85-
$mailer->registerPlugin(new CssInlinerPlugin());
135+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
86136

87137
$message = Swift_Message::newInstance();
88138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html><head><style></style></head><body>
3+
<div class="block" style="height: 20px; width: 100px;">
4+
text
5+
6+
<ul><li>
7+
Big list
8+
</li>
9+
<li class="small" style="margin: 10px;">
10+
Small list
11+
</li>
12+
</ul></div>
13+
</body></html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html><head><style>
3+
.block {
4+
width: 100px;
5+
height: 20px;
6+
}
7+
div.block ul li.small {
8+
margin: 10px;
9+
}
10+
</style></head><body>
11+
<div style="height: 20px; width: 100px;">
12+
text
13+
14+
<ul><li>
15+
Big list
16+
</li>
17+
<li style="margin: 10px;">
18+
Small list
19+
</li>
20+
</ul></div>
21+
</body></html>

0 commit comments

Comments
 (0)