Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Sep 8, 2024
1 parent 1d1615b commit 3519e59
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 51 deletions.
7 changes: 7 additions & 0 deletions src/Providers/LaravelAtomServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Database\Events\TransactionCommitted;
use Illuminate\Database\Events\TransactionRolledBack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Illuminate\Auth\Access\AuthorizationException;
Expand Down Expand Up @@ -48,6 +50,11 @@ public function boot()

// migrations
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

// events
\Event::listen([TransactionCommitted::class, TransactionRolledBack::class], function ($event) {
\App::make(\AnourValar\LaravelAtom\Service::class)->triggerTransaction($event);
});
}

/**
Expand Down
84 changes: 33 additions & 51 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ class Service
*/
protected $lockHook;

/**
* @var array
*/
protected $booted = ['commit' => false, 'rollback' => false];

/**
* @var int
*/
Expand Down Expand Up @@ -134,29 +129,7 @@ public function onCommit(callable $closure, string $connection = null): ?int
return null;
}

$key = $this->registry->push('commit', $connection, $closure);

if ($this->booted['commit']) {
return $key;
}
$this->booted['commit'] = true;

\Event::listen([TransactionCommitted::class, TransactionRolledBack::class], function ($event) {
$connection = $event->connectionName;

if (! $this->shouldCommit($connection)) {
return;
}

$list = $this->registry->pull('commit', $connection);
if ($event instanceof TransactionCommitted) {
foreach ($list as $task) {
$task();
}
}
});

return $key;
return $this->registry->push('commit', $connection, $closure);
}

/**
Expand All @@ -176,29 +149,7 @@ public function onRollBack(callable $closure, string $connection = null): ?int
return null;
}

$key = $this->registry->push('rollback', $connection, $closure);

if ($this->booted['rollback']) {
return $key;
}
$this->booted['rollback'] = true;

\Event::listen([TransactionCommitted::class, TransactionRolledBack::class], function ($event) {
$connection = $event->connectionName;

if (! $this->shouldRollBack($connection)) {
return;
}

$list = $this->registry->pull('rollback', $connection);
if ($event instanceof TransactionRolledBack) {
foreach ($list as $task) {
$task();
}
}
});

return $key;
return $this->registry->push('rollback', $connection, $closure);
}

/**
Expand Down Expand Up @@ -233,6 +184,37 @@ public function removeOnRollBack(int $key, string $connection = null): void
$this->registry->remove('rollback', $connection, $key);
}

/**
* @param mixed $event
* @return void
*/
public function triggerTransaction($event)
{
$connection = $event->connectionName;

if ($event instanceof TransactionCommitted) {
if (! $this->shouldCommit($connection)) {
return;
}

foreach ($this->registry->pull('commit', $connection) as $task) {
$task();
}
$this->registry->pull('rollback', $connection);
}

if ($event instanceof TransactionRolledBack) {
if (! $this->shouldRollBack($connection)) {
return;
}

foreach ($this->registry->pull('rollback', $connection) as $task) {
$task();
}
$this->registry->pull('commit', $connection);
}
}

/**
* @param mixed $value
* @return mixed
Expand Down

0 comments on commit 3519e59

Please sign in to comment.