From 41e4c800adabf22669227b21c7cc855d7dda1d67 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Wed, 7 Aug 2024 08:50:22 -0400 Subject: [PATCH 1/3] unlint --- src/Auditable.php | 17 ++++++++++++++++- tests/Unit/OutsideOfAppContextTest.php | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/OutsideOfAppContextTest.php diff --git a/src/Auditable.php b/src/Auditable.php index 3696afb2..c7cebd36 100644 --- a/src/Auditable.php +++ b/src/Auditable.php @@ -71,7 +71,22 @@ trait Auditable */ public static function bootAuditable() { - if (static::isAuditingEnabled()) { + try { + $isAuditingEnabled = static::isAuditingEnabled(); + } catch (\RuntimeException $e) { + if ($e->getMessage() !== 'A facade root has not been set.') { + throw $e; + } + + /** + * Facade root has not been set. The user may be attempting to use + * their Auditable outside of the application context. We will + * just skip booting for now. + */ + return; + } + + if ($isAuditingEnabled) { static::observe(new AuditableObserver()); } } diff --git a/tests/Unit/OutsideOfAppContextTest.php b/tests/Unit/OutsideOfAppContextTest.php new file mode 100644 index 00000000..48afec09 --- /dev/null +++ b/tests/Unit/OutsideOfAppContextTest.php @@ -0,0 +1,15 @@ +assertInstanceOf(Article::class, $article); + } +} \ No newline at end of file From 84e99079455d639d61023a01031d8837efe18276 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Wed, 7 Aug 2024 08:51:41 -0400 Subject: [PATCH 2/3] ends with new line --- tests/Unit/OutsideOfAppContextTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/OutsideOfAppContextTest.php b/tests/Unit/OutsideOfAppContextTest.php index 48afec09..0f84bbdd 100644 --- a/tests/Unit/OutsideOfAppContextTest.php +++ b/tests/Unit/OutsideOfAppContextTest.php @@ -12,4 +12,4 @@ public function test_can_create_new_model(): void $article = new Article(); $this->assertInstanceOf(Article::class, $article); } -} \ No newline at end of file +} From 612e3f001f4fa08f5e8073f62881338e41d08dcb Mon Sep 17 00:00:00 2001 From: erikn69 Date: Mon, 13 Jan 2025 12:43:14 -0500 Subject: [PATCH 3/3] Check facade instance --- src/Auditable.php | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/Auditable.php b/src/Auditable.php index c7cebd36..3e8d0f55 100644 --- a/src/Auditable.php +++ b/src/Auditable.php @@ -71,22 +71,7 @@ trait Auditable */ public static function bootAuditable() { - try { - $isAuditingEnabled = static::isAuditingEnabled(); - } catch (\RuntimeException $e) { - if ($e->getMessage() !== 'A facade root has not been set.') { - throw $e; - } - - /** - * Facade root has not been set. The user may be attempting to use - * their Auditable outside of the application context. We will - * just skip booting for now. - */ - return; - } - - if ($isAuditingEnabled) { + if (App::getFacadeRoot() && static::isAuditingEnabled()) { static::observe(new AuditableObserver()); } }