queue[] = function () use ($command, $next) { return $next($command); }; if ($this->isExecuting) { return; } $this->isExecuting = true; try { $returnValue = $this->executeQueuedJobs(); } catch (\Exception $e) { $this->isExecuting = false; $this->queue = []; throw $e; } $this->isExecuting = false; return $returnValue; } /** * Process any pending commands in the queue. If multiple, jobs are in the * queue, only the first return value is given back. * * @return mixed */ protected function executeQueuedJobs() { $returnValues = []; while ($resumeCommand = array_shift($this->queue)) { $returnValues[] = $resumeCommand(); } return array_shift($returnValues); } }