From e7c160971a883ad743eaf92844cfe2e0cbc0c30c Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Sun, 27 Nov 2022 09:04:48 +0000 Subject: [PATCH 1/2] feat: encapsulate properties --- README.md | 2 +- src/CssInliner.php | 37 ++++++++++++----------------------- tests/Feature/ConvertTest.php | 18 ++++++++--------- tests/Feature/EventsTest.php | 14 ++++++------- tests/TestCase.php | 9 +++------ 5 files changed, 32 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 653f7b5..5aa268d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/CssInliner.php b/src/CssInliner.php index 79d587f..8fa7449 100644 --- a/src/CssInliner.php +++ b/src/CssInliner.php @@ -42,23 +42,10 @@ class CssInliner protected array $interceptCssFiles = []; /** Is debug mode enabled? */ - protected static bool $debug = false; + protected bool $debug = false; /** @var array 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() { @@ -68,25 +55,25 @@ 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 = []; } /** @@ -94,9 +81,9 @@ public static function flushDebugLog(): void * * @return array */ - public static function getDebugLog(): array + public function getDebugLog(): array { - return static::$log; + return $this->log; } /** @@ -104,7 +91,7 @@ public static function getDebugLog(): array */ public function debug(string $message): self { - if (static::$debug) { + if ($this->debug) { $log = sprintf( '[%s]: %s | %s', Carbon::now()->toDateTimeString(), @@ -112,7 +99,7 @@ public function debug(string $message): self $message, ); - static::$log[] = $log; + $this->log[] = $log; } return $this; diff --git a/tests/Feature/ConvertTest.php b/tests/Feature/ConvertTest.php index da48a7d..bb6cdf0 100644 --- a/tests/Feature/ConvertTest.php +++ b/tests/Feature/ConvertTest.php @@ -10,7 +10,7 @@ $expect = 'This is a test'; - $actual = CssInliner::create() + $actual = $this->app->make(CssInliner::class) ->addCssRaw($css) ->convert($html); @@ -24,7 +24,7 @@ $html = 'This is a test'; $expect = 'This is a test'; - $actual = CssInliner::create() + $actual = $this->app->make(CssInliner::class) ->addCssPath($file) ->convert($html); @@ -37,7 +37,7 @@ $expect = 'This is a test'; - $actual = CssInliner::create() + $actual = $this->app->make(CssInliner::class) ->enableCssExtractionFromHtmlContent() ->disableCssRemovalFromHtmlContent() ->convert($html); @@ -52,7 +52,7 @@ $expect = 'This is a test'; - $actual = CssInliner::create() + $actual = $this->app->make(CssInliner::class) ->enableCssExtractionFromHtmlContent() ->disableCssRemovalFromHtmlContent() ->convert($html); @@ -71,7 +71,7 @@ $expect = 'This is a test'; - $actual = CssInliner::create() + $actual = $this->app->make(CssInliner::class) ->addCssPath($file1) ->addCssPath($file2) ->addCssRaw($css1) @@ -114,7 +114,7 @@ /** * Enabled */ - $actual = CssInliner::create() + $actual = $this->app->make(CssInliner::class) ->enableCssExtractionFromHtmlContent() ->enableCssRemovalFromHtmlContent() ->convert($html); @@ -130,7 +130,7 @@ /** * Disabled */ - $actual = CssInliner::create() + $actual = $this->app->make(CssInliner::class) ->enableCssExtractionFromHtmlContent() ->disableCssRemovalFromHtmlContent() ->convert($html); @@ -177,7 +177,7 @@ /** * Enabled */ - $actual = CssInliner::create() + $actual = $this->app->make(CssInliner::class) ->enableCssExtractionFromHtmlContent() ->disableCssRemovalFromHtmlContent() ->convert($html); @@ -188,7 +188,7 @@ /** * Disabled */ - $actual = CssInliner::create() + $actual = $this->app->make(CssInliner::class) ->disableCssExtractionFromHtmlContent() ->disableCssRemovalFromHtmlContent() ->convert($html); diff --git a/tests/Feature/EventsTest.php b/tests/Feature/EventsTest.php index b942312..dc18b76 100644 --- a/tests/Feature/EventsTest.php +++ b/tests/Feature/EventsTest.php @@ -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]++; @@ -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]++; @@ -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]++; }) @@ -175,7 +175,7 @@ $html = 'TestTestTest'; $expect = 'TestTestTest'; - $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') @@ -204,7 +204,7 @@ $html = 'TestTestTest'; $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) @@ -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) @@ -287,7 +287,7 @@ ->and('ran_second_event')->debugLogNotExists() ->and('html_conversion_finished')->debugLogNotExists(); - CssInliner::flushDebugLog(); + CssInline::flushDebugLog(); $html = 'TestTest secondTest'; $expect = 'TestTest secondTest'; diff --git a/tests/TestCase.php b/tests/TestCase.php index 5641f3e..a8727c1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,7 +4,7 @@ namespace LaravelCssInliner\Tests; -use LaravelCssInliner\CssInliner; +use LaravelCssInliner\Facades\CssInline; use LaravelCssInliner\LaravelCssInlinerServiceProvider; class TestCase extends \Orchestra\Testbench\TestCase @@ -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 From 04f753775cc8021cd6f805b06f6c17b206ad42b6 Mon Sep 17 00:00:00 2001 From: Bradie <44430471+bradietilley@users.noreply.github.com> Date: Sun, 27 Nov 2022 18:07:45 +0800 Subject: [PATCH 2/2] docs: update facade method docblocks --- src/Facades/CssInline.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Facades/CssInline.php b/src/Facades/CssInline.php index f017689..a5a89d1 100644 --- a/src/Facades/CssInline.php +++ b/src/Facades/CssInline.php @@ -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() @@ -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) @@ -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 {