Skip to content

Commit

Permalink
allow for creating an Auditable model even if application is not spun up
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Aug 7, 2024
1 parent 9357577 commit e7383f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,21 @@ trait Auditable
*/
public static function bootAuditable()
{
if (static::isAuditingEnabled()) {
try {
$isAuditingEnabled = static::isAuditingEnabled();
} catch (\RuntimeException $e) {
if (stripos($e->getMessage(), "facade root") === false) {
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());
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/OutsideOfAppContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace OwenIt\Auditing\Tests\Unit;

use OwenIt\Auditing\Tests\Models\Article;
use PHPUnit\Framework\TestCase;

class OutsideOfAppContextTest extends TestCase
{
public function test_can_create_new_model(): void
{
$article = new Article();
$this->assertInstanceOf(Article::class, $article);
}
}

0 comments on commit e7383f6

Please sign in to comment.