diff --git a/src/BusServiceProvider.php b/src/BusServiceProvider.php index 2752a93..ec1f742 100644 --- a/src/BusServiceProvider.php +++ b/src/BusServiceProvider.php @@ -2,7 +2,6 @@ namespace TheRezor\TransactionalJobs; -use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider; use Illuminate\Contracts\Bus\Dispatcher as DispatcherContract; use Illuminate\Contracts\Queue\Factory as QueueFactoryContract; @@ -10,6 +9,13 @@ class BusServiceProvider extends ServiceProvider { + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = true; + /** * Register the service provider. * @@ -30,9 +36,20 @@ public function register() $this->app->alias( TransactionalDispatcher::class, QueueingDispatcherContract::class ); + } - $this->app->afterResolving('db', function ($db, Application $app) { - $app->make(TransactionalDispatcher::class); - }); + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return [ + TransactionalDispatcher::class, + DispatcherContract::class, + QueueingDispatcherContract::class, + ]; } } + diff --git a/src/TransactionalDispatcher.php b/src/TransactionalDispatcher.php index 1693e28..92d76d7 100644 --- a/src/TransactionalDispatcher.php +++ b/src/TransactionalDispatcher.php @@ -5,10 +5,9 @@ use Closure; use Illuminate\Bus\Dispatcher; use Illuminate\Contracts\Container\Container; -use Illuminate\Database\Events\TransactionBeginning; +use Illuminate\Database\DatabaseManager; use Illuminate\Database\Events\TransactionCommitted; use Illuminate\Database\Events\TransactionRolledBack; -use Illuminate\Database\ConnectionInterface; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; class TransactionalDispatcher extends Dispatcher @@ -23,69 +22,63 @@ class TransactionalDispatcher extends Dispatcher */ protected $eventDispatcher; + /** + * @var DatabaseManager + */ + protected $db; + public function __construct(Container $container, Closure $queueResolver = null) { parent::__construct($container, $queueResolver); $this->eventDispatcher = $container->make(DispatcherContract::class); + $this->db = $container->make('db'); $this->setUpTransactionListeners(); } public function dispatchToQueue($command) { // Dispatch immediately if no transactions was opened during job - if (empty($command->afterTransactions) || empty($this->pendingCommands)) { + if (empty($command->afterTransactions) || 0 === $this->db->transactionLevel()) { return parent::dispatchToQueue($command); } // Add command to pending list - foreach ($this->pendingCommands as $connection => $items) { - $this->pendingCommands[$connection][] = $command; - } + $this->pendingCommands[] = $command; return null; } - protected function prepareTransaction(ConnectionInterface $connection) + public function commitTransaction() { - if (!isset($this->pendingCommands[$connection->getName()])) { - $this->pendingCommands[$connection->getName()] = []; - } - } - - public function commitTransaction(ConnectionInterface $connection) - { - if (empty($this->pendingCommands[$connection->getName()]) || $connection->transactionLevel() > 0) { + if (empty($this->pendingCommands) || $this->db->transactionLevel() > 0) { return; } - $this->dispatchPendingCommands($connection); + $this->dispatchPendingCommands(); } - public function rollbackTransaction(ConnectionInterface $connection) + public function rollbackTransaction() { - unset($this->pendingCommands[$connection->getName()]); + $this->pendingCommands = []; } - protected function dispatchPendingCommands(ConnectionInterface $connection) + protected function dispatchPendingCommands() { - foreach ($this->pendingCommands[$connection->getName()] as $command) { + foreach ($this->pendingCommands as $command) { parent::dispatchToQueue($command); } - unset($this->pendingCommands[$connection->getName()]); + $this->pendingCommands = []; } protected function setUpTransactionListeners() { - $this->eventDispatcher->listen(TransactionBeginning::class, function ($event) { - $this->prepareTransaction($event->connection); - }); - $this->eventDispatcher->listen(TransactionCommitted::class, function ($event) { - $this->commitTransaction($event->connection); + $this->eventDispatcher->listen(TransactionCommitted::class, function () { + $this->commitTransaction(); }); - $this->eventDispatcher->listen(TransactionRolledBack::class, function ($event) { - $this->rollbackTransaction($event->connection); + $this->eventDispatcher->listen(TransactionRolledBack::class, function () { + $this->rollbackTransaction(); }); } }