container = $container; $this->addHandlers($commandNameToHandlerMap); } /** * Bind a handler instance to receive all commands with a certain class * * @param string $handler Handler to receive class * @param string $commandName Can be a class name or name of a NamedCommand */ public function addHandler($handler, $commandName) { $this->commandNameToHandlerMap[$commandName] = $handler; } /** * Allows you to add multiple handlers at once. * * The map should be an array in the format of: * [ * 'AddTaskCommand' => 'AddTaskCommandHandler', * 'CompleteTaskCommand' => 'CompleteTaskCommandHandler', * ] * * @param array $commandNameToHandlerMap */ public function addHandlers(array $commandNameToHandlerMap) { foreach ($commandNameToHandlerMap as $commandName => $handler) { $this->addHandler($handler, $commandName); } } /** * Retrieves the handler for a specified command * * @param string $commandName * * @return object * * @throws MissingHandlerException */ public function getHandlerForCommand($commandName) { if (!isset($this->commandNameToHandlerMap[$commandName])) { throw MissingHandlerException::forCommand($commandName); } $serviceId = $this->commandNameToHandlerMap[$commandName]; return $this->container->get($serviceId); } }