Skip to content
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ composer require laravel-css-inliner/css-inliner

## Usage

For the purpose of this demsonstration we'll use the facade `LaravelCssInliner\Facades\CssInline`, however if you prefer directly using the instance (like myself) you can swap `LaravelCssInliner\Facades\CssInline::` out for `LaravelCssInliner\CssInliner::singleton()->` in any of the examples below.
For the purpose of this demonstration we'll use the facade `LaravelCssInliner\Facades\CssInline`, however if you prefer directly using the instance (like myself) you can swap `LaravelCssInliner\Facades\CssInline::` out for `LaravelCssInliner\CssInliner::singleton()->` in any of the examples below.

#### Adding CSS via PHP

Expand Down
37 changes: 12 additions & 25 deletions src/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,10 @@ class CssInliner
protected array $interceptCssFiles = [];

/** Is debug mode enabled? */
protected static bool $debug = false;
protected bool $debug = false;

/** @var array<string> Debug logs */
protected static array $log = [];

/**
* Create a new CssInliner instance
*
* For Laravel: it's recommended you use the facade
* or at least `app(CssInliner::class)` to ensure that
* you're also referencing a singleton not a stray
* instance
*/
public static function create(): self
{
return new self();
}
protected array $log = [];

public function __construct()
{
Expand All @@ -68,51 +55,51 @@ public function __construct()
/**
* Enable debug mode
*/
public static function enableDebug(): void
public function enableDebug(): void
{
static::$debug = true;
$this->debug = true;
}

/**
* Disable debug mode
*/
public static function disableDebug(): void
public function disableDebug(): void
{
static::$debug = false;
$this->debug = false;
}

/**
* Reset the log
*/
public static function flushDebugLog(): void
public function flushDebugLog(): void
{
static::$log = [];
$this->log = [];
}

/**
* Get the debug log
*
* @return array<string>
*/
public static function getDebugLog(): array
public function getDebugLog(): array
{
return static::$log;
return $this->log;
}

/**
* Log some debug information for when debug mode is enabled
*/
public function debug(string $message): self
{
if (static::$debug) {
if ($this->debug) {
$log = sprintf(
'[%s]: %s | %s',
Carbon::now()->toDateTimeString(),
$this->instance,
$message,
);

static::$log[] = $log;
$this->log[] = $log;
}

return $this;
Expand Down
9 changes: 7 additions & 2 deletions src/Facades/CssInline.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
/**
* @mixin CssInliner
*
* @method static void enableDebug()
* @method static void disableDebug()
* @method static void flushDebugLog()
* @method static array getDebugLog()
* @method static CssInliner debug(string $message)
* @method static CssInliner addCss(string|SplFileInfo $file)
* @method static CssInliner addCss(string|SplFileInfo $css)
* @method static CssInliner addCssPath(string|SplFileInfo $file)
* @method static CssInliner addCssRaw(string $css)
* @method static CssInliner clearCss()
Expand All @@ -31,6 +35,7 @@
* @method static CssInliner clearInterceptors()
* @method static Email convertEmail(Email $email)
* @method static string convert(string $html)
* @method static void stripCssFromHtml(string &$html)
* @method static string parseCssFromHtml(string &$html)
* @method static CssInliner beforeConvertingEmail(callable $callback)
* @method static CssInliner afterConvertingEmail(callable $callback)
Expand All @@ -39,7 +44,7 @@
* @method static CssInliner instance()
* @method static array cssFiles()
* @method static array cssRaw()
* @method static void halt()
* @method static false halt()
*/
class CssInline extends Facade
{
Expand Down
18 changes: 9 additions & 9 deletions tests/Feature/ConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

$expect = 'This is a <span class="font-bold" style="font-weight: bold;">test</span>';

$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->addCssRaw($css)
->convert($html);

Expand All @@ -24,7 +24,7 @@
$html = 'This is a <span class="font-bold">test</span>';
$expect = 'This is a <span class="font-bold" style="font-weight: bold;">test</span>';

$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->addCssPath($file)
->convert($html);

Expand All @@ -37,7 +37,7 @@

$expect = 'This is a <span class="example" style="text-decoration: underline;">test</span>';

$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->enableCssExtractionFromHtmlContent()
->disableCssRemovalFromHtmlContent()
->convert($html);
Expand All @@ -52,7 +52,7 @@

$expect = 'This is a <span class="example" style="font-style: italic;">test</span>';

$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->enableCssExtractionFromHtmlContent()
->disableCssRemovalFromHtmlContent()
->convert($html);
Expand All @@ -71,7 +71,7 @@

$expect = 'This is a <span class="example" style="font-weight: bold; font-size: 12px; color: red; text-decoration: underline;">test</span>';

$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->addCssPath($file1)
->addCssPath($file2)
->addCssRaw($css1)
Expand Down Expand Up @@ -114,7 +114,7 @@
/**
* Enabled
*/
$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->enableCssExtractionFromHtmlContent()
->enableCssRemovalFromHtmlContent()
->convert($html);
Expand All @@ -130,7 +130,7 @@
/**
* Disabled
*/
$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->enableCssExtractionFromHtmlContent()
->disableCssRemovalFromHtmlContent()
->convert($html);
Expand Down Expand Up @@ -177,7 +177,7 @@
/**
* Enabled
*/
$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->enableCssExtractionFromHtmlContent()
->disableCssRemovalFromHtmlContent()
->convert($html);
Expand All @@ -188,7 +188,7 @@
/**
* Disabled
*/
$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->disableCssExtractionFromHtmlContent()
->disableCssRemovalFromHtmlContent()
->convert($html);
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
PostEmailCssInlineEvent::class => 0,
];

CssInliner::create()
$this->app->make(CssInliner::class)
->addCssRaw($css)
->beforeConvertingHtml(function (string $eventHtml, CssInliner $eventInliner, PreCssInlineEvent $event) use ($html, $css, &$callbacks) {
$callbacks[PreCssInlineEvent::class]++;
Expand Down Expand Up @@ -67,7 +67,7 @@
PostEmailCssInlineEvent::class => 0,
];

CssInliner::create()
$this->app->make(CssInliner::class)
->addCssRaw($css = '.font-bold { font-weight: bold; }')
->beforeConvertingEmail(function (Email $eventEmail, CssInliner $eventInliner, PreEmailCssInlineEvent $event) use ($html, $email, $css, &$callbacks) {
$callbacks[PreEmailCssInlineEvent::class]++;
Expand Down Expand Up @@ -136,7 +136,7 @@
PostEmailCssInlineEvent::class => 0,
];

CssInliner::create()
$this->app->make(CssInliner::class)
->beforeConvertingEmail(function (Email $eventEmail, CssInliner $eventInliner, PreEmailCssInlineEvent $event) use (&$callbacks) {
$callbacks[PreEmailCssInlineEvent::class]++;
})
Expand Down Expand Up @@ -175,7 +175,7 @@
$html = 'Test<span class="font-bold">Test</span>Test';

$expect = 'Test<span class="font-bold" style="font-weight: bold;">Test</span>Test';
$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->beforeConvertingHtml(fn (string & $eventHtml, CssInliner $eventInliner, PreCssInlineEvent $event) => $eventHtml .= 'something1')
->beforeConvertingHtml(fn (string & $eventHtml, CssInliner $eventInliner, PreCssInlineEvent $event) => $eventInliner->debug('ran_second_event'))
->afterConvertingHtml(fn (string & $eventHtml, CssInliner $eventInliner, PostCssInlineEvent $event) => $eventHtml .= 'something2')
Expand Down Expand Up @@ -204,7 +204,7 @@
$html = 'Test<span class="font-bold">Test</span>Test';

$expect = $html;
$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->beforeConvertingHtml(fn (string $eventHtml, CssInliner $eventInliner, PreCssInlineEvent $event) => $eventInliner->halt())
->beforeConvertingHtml(fn (string $eventHtml, CssInliner $eventInliner, PreCssInlineEvent $event) => $eventInliner->debug('ran_second_event'))
->addCssRaw($css)
Expand All @@ -227,7 +227,7 @@

$expect = $html;

$actual = CssInliner::create()
$actual = $this->app->make(CssInliner::class)
->beforeConvertingEmail(fn (Email $eventEmail, CssInliner $eventInliner, PreEmailCssInlineEvent $event) => $eventInliner->halt())
->beforeConvertingHtml(fn (Email $eventEmail, CssInliner $eventInliner, PreEmailCssInlineEvent $event) => $eventInliner->debug('ran_second_event'))
->addCssRaw($css)
Expand Down Expand Up @@ -287,7 +287,7 @@
->and('ran_second_event')->debugLogNotExists()
->and('html_conversion_finished')->debugLogNotExists();

CssInliner::flushDebugLog();
CssInline::flushDebugLog();

$html = 'Test<span class="font-bold">Test second</span>Test';
$expect = 'Test<span class="font-bold" style="font-weight: bold;">Test second</span>Test';
Expand Down
9 changes: 3 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace LaravelCssInliner\Tests;

use LaravelCssInliner\CssInliner;
use LaravelCssInliner\Facades\CssInline;
use LaravelCssInliner\LaravelCssInlinerServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
Expand All @@ -24,11 +24,8 @@ protected function setUp(): void
{
parent::setUp();

CssInliner::enableDebug();
CssInliner::flushDebugLog();

// New instance
app()->singleton(CssInliner::class, fn () => new CssInliner());
CssInline::enableDebug();
CssInline::flushDebugLog();
}

protected function tearDown(): void
Expand Down