Skip to content

Commit e7383f6

Browse files
committed
allow for creating an Auditable model even if application is not spun up
1 parent 9357577 commit e7383f6

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Auditable.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,21 @@ trait Auditable
7171
*/
7272
public static function bootAuditable()
7373
{
74-
if (static::isAuditingEnabled()) {
74+
try {
75+
$isAuditingEnabled = static::isAuditingEnabled();
76+
} catch (\RuntimeException $e) {
77+
if (stripos($e->getMessage(), "facade root") === false) {
78+
throw $e;
79+
}
80+
/**
81+
* Facade root has not been set. The user may be attempting to use
82+
* their Auditable outside of the application context. We will
83+
* just skip booting for now.
84+
*/
85+
return;
86+
}
87+
88+
if ($isAuditingEnabled) {
7589
static::observe(new AuditableObserver());
7690
}
7791
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace OwenIt\Auditing\Tests\Unit;
4+
5+
use OwenIt\Auditing\Tests\Models\Article;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class OutsideOfAppContextTest extends TestCase
9+
{
10+
public function test_can_create_new_model(): void
11+
{
12+
$article = new Article();
13+
$this->assertInstanceOf(Article::class, $article);
14+
}
15+
}

0 commit comments

Comments
 (0)