canRunAsync($command)) { $this->queueCommand($command); } else { $this->runCommand($command); } } /** * Queue a command in the bus */ abstract protected function queueCommand(ICommand $command); /** * Require all commands using a trait to be run synchronous * * @param string $trait */ public function requireSync(string $trait): void { $this->syncTraits[] = trim($trait, '\\'); } private function runCommand(ICommand $command): void { $command->handle(); } /** * @return bool */ private function canRunAsync(ICommand $command): bool { $traits = $this->getTraits($command); foreach ($traits as $trait) { if (in_array($trait, $this->syncTraits, true)) { return false; } } return true; } /** * @return string[] */ private function getTraits(ICommand $command): array { return class_uses($command); } }