Skip to content

Commit

Permalink
Merge pull request #30 from kilburn/options
Browse files Browse the repository at this point in the history
Add configuration settings
  • Loading branch information
ceesvanegmond committed Apr 19, 2016
2 parents e152fca + 32f8dce commit 8c3cd95
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 28 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Into this:
## Installation
Begin by installing this package through Composer. Edit your project's `composer.json` file to require `fedeisas/laravel-mail-css-inliner`.

This package needs Laravel 4.x or Laravel 5.x
This package needs Laravel 5.x
```json
{
"require": {
Expand All @@ -60,11 +60,17 @@ Next, update Composer from the Terminal:
$ composer update
```

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.
Once this operation completes, you must add the service provider. Open `app/config/app.php`, and add a new item to the providers array.
```php
'Fedeisas\LaravelMailCssInliner\LaravelMailCssInlinerServiceProvider',
```

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:
```bash
$ php artisan vendor:publish --provider='Fedeisas\\LaravelMailCssInliner\\LaravelMailCssInlinerServiceProvider'
```
and changing the settings on the generated `config/css-inliner.php` file.

## Contributing
```bash
$ composer install
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
}
],
"require": {
"php": ">=5.3.2",
"illuminate/support": "~4.0|~5.0",
"php": ">=5.4.0",
"illuminate/support": "~5.0",
"tijsverkoyen/css-to-inline-styles": "~1.2"
},
"require-dev" : {
Expand Down
32 changes: 32 additions & 0 deletions config/css-inliner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Strip styles
|--------------------------------------------------------------------------
|
| Settings this to false prevents the inliner from removing the style
| definitions that have been inlined.
|
| Notice that media query styles are not inlined, and hence never
| stripped.
|
*/

'strip-styles' => true,

/*
|--------------------------------------------------------------------------
| Remove classes
|--------------------------------------------------------------------------
|
| Settings this to false disables the removal of class attributes from
| your html elements (do not enable this if you use media queries)
|
*/

'strip-classes' => true,

];
38 changes: 34 additions & 4 deletions src/CssInlinerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@

class CssInlinerPlugin implements \Swift_Events_SendListener
{
/**
* @var array
*/
protected $options;

/**
* @param array $options options defined in the configuration file.
*/
public function __construct(array $options)
{
$this->options = $options;
}

/**
* @param Swift_Events_SendEvent $evt
*/
Expand All @@ -14,10 +27,7 @@ public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
$message = $evt->getMessage();

$converter = new CssToInlineStyles();
$converter->setUseInlineStylesBlock();
$converter->setStripOriginalStyleTags();

$converter->setCleanup();
$this->applySettings($converter);

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

/**
* Applies the configuration settings.
*
* @param CssToInlineStyles $converter
*/
private function applySettings(CssToInlineStyles $converter)
{
// Always enabled because there is no way to specify an external style sheet
// when using this plugin
$converter->setUseInlineStylesBlock();

if ($this->options['strip-styles']) {
$converter->setStripOriginalStyleTags();
}

if ($this->options['strip-classes']) {
$converter->setCleanup();
}
}

/**
* Do nothing
*
Expand Down
26 changes: 15 additions & 11 deletions src/LaravelMailCssInlinerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Fedeisas\LaravelMailCssInliner;

use Illuminate\Support\ServiceProvider;
use Swift_Mailer;

class LaravelMailCssInlinerServiceProvider extends ServiceProvider
{
Expand All @@ -19,7 +20,9 @@ class LaravelMailCssInlinerServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->app['mailer']->getSwiftMailer()->registerPlugin(new CssInlinerPlugin());
$this->publishes([
__DIR__.'/../config/css-inliner.php' => config_path('css-inliner.php'),
], 'config');
}

/**
Expand All @@ -29,16 +32,17 @@ public function boot()
*/
public function register()
{
// Do nothing
}
$this->mergeConfigFrom(__DIR__.'/../config/css-inliner.php', 'css-inliner');

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
$this->app->singleton('Fedeisas\LaravelMailCssInliner\CssInlinerPlugin', function ($app) {
$options = $app['config']->get('css-inliner');
return new CssInlinerPlugin($options);
});

$this->app->extend('swift.mailer', function (Swift_Mailer $swiftMailer, $app) {
$inlinerPlugin = $app->make('Fedeisas\LaravelMailCssInliner\CssInlinerPlugin');
$swiftMailer->registerPlugin($inlinerPlugin);
return $swiftMailer;
});
}
}
66 changes: 58 additions & 8 deletions tests/CssInlinerPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@

class CssInlinerPluginTest extends PHPUnit_Framework_TestCase
{

protected $stubs;

protected $options;

protected static $stubDefinitions = array(
'plain-text', 'original-html', 'converted-html', 'converted-html-with-classes',
'converted-html-with-styles'
);

public function setUp()
{
$this->stubs['plain-text'] = file_get_contents(__DIR__.'/stubs/plain-text.stub');
$this->stubs['original-html'] = file_get_contents(__DIR__.'/stubs/original-html.stub');
$this->stubs['converted-html'] = file_get_contents(__DIR__.'/stubs/converted-html.stub');
foreach (self::$stubDefinitions as $stub) {
$this->stubs[$stub] = file_get_contents(__DIR__.'/stubs/'.$stub.'.stub');
}

$this->options = require(__DIR__.'/../config/css-inliner.php');
}

/** @test **/
public function itShouldConvertHtmlBody()
{
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());

$mailer->registerPlugin(new CssInlinerPlugin());
$mailer->registerPlugin(new CssInlinerPlugin($this->options));

$message = Swift_Message::newInstance();

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

/** @test **/
public function itShouldConvertHtmlBodyKeepingClasses()
{
$this->options['strip-classes'] = false;

$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());

$mailer->registerPlugin(new CssInlinerPlugin($this->options));

$message = Swift_Message::newInstance();

$message->setFrom('[email protected]');
$message->setTo('[email protected]');
$message->setSubject('Test');
$message->setBody($this->stubs['original-html'], 'text/html');

$mailer->send($message);

$this->assertEquals($this->stubs['converted-html-with-classes'], $message->getBody());
}

/** @test **/
public function itShouldConvertHtmlBodyKeepingStyles()
{
$this->options['strip-styles'] = false;

$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());

$mailer->registerPlugin(new CssInlinerPlugin($this->options));

$message = Swift_Message::newInstance();

$message->setFrom('[email protected]');
$message->setTo('[email protected]');
$message->setSubject('Test');
$message->setBody($this->stubs['original-html'], 'text/html');

$mailer->send($message);

$this->assertEquals($this->stubs['converted-html-with-styles'], $message->getBody());
}

/** @test **/
public function itShouldConvertHtmlBodyAndTextParts()
{
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());

$mailer->registerPlugin(new CssInlinerPlugin());
$mailer->registerPlugin(new CssInlinerPlugin($this->options));

$message = Swift_Message::newInstance();

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

$mailer->registerPlugin(new CssInlinerPlugin());
$mailer->registerPlugin(new CssInlinerPlugin($this->options));

$message = Swift_Message::newInstance();

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

$mailer->registerPlugin(new CssInlinerPlugin());
$mailer->registerPlugin(new CssInlinerPlugin($this->options));

$message = Swift_Message::newInstance();

Expand Down
13 changes: 13 additions & 0 deletions tests/stubs/converted-html-with-classes.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><style></style></head><body>
<div class="block" style="height: 20px; width: 100px;">
text

<ul><li>
Big list
</li>
<li class="small" style="margin: 10px;">
Small list
</li>
</ul></div>
</body></html>
21 changes: 21 additions & 0 deletions tests/stubs/converted-html-with-styles.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><style>
.block {
width: 100px;
height: 20px;
}
div.block ul li.small {
margin: 10px;
}
</style></head><body>
<div style="height: 20px; width: 100px;">
text

<ul><li>
Big list
</li>
<li style="margin: 10px;">
Small list
</li>
</ul></div>
</body></html>

0 comments on commit 8c3cd95

Please sign in to comment.