@see Router::forCommand
@see GearmanClient::doBackground

@param Command $command

@throws \Exception
14276
protected function doSendCommand(Command $command): void
    {
        if (!$this->shouldUseGearman()) {
            $this->locator->getCommandBus()->receiveCommand($command);
            return;
        }

        $envelope = new TransportEnvelope($command, 'php');
        $channel = $this->router->forCommand($command);

        try {
            $client = $this->getClient();
            @$client->doBackground($channel, $envelope->toString(), (string)$command->get('command_id'));
            $this->validateReturnCode($client, $channel);
        } catch (\GearmanException $ge) {
            if (isset(self::$reconnectCodes[$ge->getCode()])) {
                $this->destroyClient();
                $this->locator->getCommandBus()->receiveCommand($command);
                return;
            }

            throw $ge;
        } catch (\Throwable $e) {
            throw $e;
        }
    }
103905
public function fire(Command $command)
    {
        if (! $this->finder->isFile('.env')) {
            throw new Exception('SocietyCMS is not installed. Please run "php artisan society:install" first.');
        }

        if ($command->option('refresh') && ! App::environment('demo')) {
            throw new Exception('Refresh option is only available in demo mode.');
        }

        if (! $command->option('force') && ! $command->option('refresh')) {
            if (! $command->confirm('Are you sure you want to start Demo Mode?')) {
                throw new Exception('Demo Mode cancelled');
            }
        }
    }
362454
public function exec($command)
    {
        $this->_events[] = $event = new Event($this->_mutex, $command);
        return $event;
    }
750570
public function dispatch(Command $command)
    {
        // If beforeCommand fails abort
        if ($this->_eventsManager->fire('command:beforeCommand', $command) === false) {
            return false;
        }

        // If run the commands fails abort too
        if ($command->run($command->getParameters()) === false) {
            return false;
        }

        $this->_eventsManager->fire('command:afterCommand', $command);

        return true;
    }
700619
public static function onCommand($command, ConstraintViolationListInterface $violations)
    {
        $exception = new static(
            'Validation failed for ' . get_class($command) .
            ' with ' . $violations->count() . ' violation(s).'
        );

        $exception->command    = $command;
        $exception->violations = $violations;

        return $exception;
    }
859489
public function dispatch(CommandInterface $command, $callback = null){

	    if (!$command->getIdentity() && $this->getDefaultIdentity()) {
            $command->setIdentity($this->getDefaultIdentity());
	    }

	    $service     = $command->getService();
	    $operation   = $command->getOperation();
	    $context     = $command->getContext();
	    $argv        = $command->getParams();
	    $exception = null;

	    if(!$this->exists($command->getService())){
	        throw new ServiceNotFoundException(sprintf(
                'Service "%s" not found', $command->getService()));
	    }

	    $responses = null;

        // Notify, that a (background) job is about to start
        if ($command->getJob()) {
            $jobEvent = $this->createEvent(
                'job.start', $command);

            $this->getEventManager()
                ->trigger($jobEvent);
        }

		// Prepare and trigger init.<service>.<operation> event
		$initEvent = strtolower('init.'.$service.'.'.$operation);

		if(!$this->getEventManager()->getListeners($initEvent)->isEmpty()){
		    $e = $this->createEvent($initEvent, $command);

		    $eventResponses = $this->trigger(
	            $e,
	            function($response){if($response === false) return true;}
		    );

		    if($eventResponses->stopped() && $eventResponses->last() === false){
		        $responses = new ResponseCollection();
		        $responses->setStopped(true);
		    }
		}

		// Dispatch command
		if ($responses === null) {
		    try{
		        $responses = $this->getLoader()->getCommandManager()->trigger(
                    $command,
                    $callback
	            );
		    } catch(\Exception $ex) {
		        $exception = $ex;
		    }
		}

		// Prepare and trigger final.<service>.<operation> event
		$finalEvent = strtolower('final.'.$service.'.'.$operation);

		if(!$this->getEventManager()->getListeners($finalEvent)->isEmpty()){

		    $e = $this->createEvent($finalEvent, $command);

		    // Set exception
		    if ($exception) {
		        $e->setException($exception);
		    }

		    $this->trigger($e);

		    // Listeners have a chance to clear (or change) the exception
		    $exception = $e->getException();
		}

        // Notify that the job has ended
        if ($command->getJob()) {
            $jobEvent = $this->createEvent('job.end', $command);

            if ($exception) {
                $jobEvent->setException($exception);
            }
            $this->trigger($jobEvent);
        }

		// Throw exception if it still exists
		if ($exception instanceof \Exception) {
		    throw $exception;
		}

		return $responses;
	}
470246
protected function fireBeforeCommandEvent($command)
	{
		$eventName = $this->commandNameToEventName($command);

		$args = [$command];

		$this->fireEvent($eventName, 'before', $args);
	}
1009586
public function addConsole($command) {
        /* @var $command Command */
        $command->configure();
        $name = $command->name();
        $this->defaultCommand[ $name ] = $command;
    }
923478
public static function forCommand($command)
    {
        $type =  is_object($command) ? get_class($command) : gettype($command);

        $exception = new static('Could not determine command name of ' . $type);
        $exception->command = $command;

        return $exception;
    }
642898
public function addCommand(Command $command) {
        $this->commands[$command->getName()] = $command;
        $command->freeze();
        return $this;
    }
272554
public function command($command)
    {
        if ($command instanceof CommandInterface) {
            $command->setContainer($this);
        }

        $this['console']->add($command);
    }
698289
protected function dispatch(Command $command)
    {
        // re-connect if we have timed out
        if ($this->isTimedOut() === true) {
            $this->close();
            $this->connect();
        }

        // construct message
        $message = $command->getCommand() . "\r\n";

        if (($data = $command->getData()) !== false) {
            $message .= $data . "\r\n";
        }

        // write to stream
        if ($this->stream->write($message) === false) {
            throw new Exception(
                'Error writing data to the server.',
                Exception::SERVER_WRITE
            );
        }

        // read response from stream
        $response = $this->stream->readLine();

        // validate against common errors
        $this->validateResponse($response);

        $data = null;

        if ($command->returnsData()) {
            $bytes = preg_replace('/^.*\b(\d+)$/', '$1', $response);
            $data = $this->stream->read($bytes);
        }

        return $command->parseResponse($response, $data, $this);
    }
1013684
protected function runCommand($command)
    {
        $process = $this->getProcess();

        $process->setCommandLine(trim($this->find() . ' ' . $command));

        $process->run();

        return $this;
    }
1055162
final public function handle(CommandInterface $command)
    {
        $this->validateCommand($command);

        try {
            // @todo - Update this to return the succes event object, rather than using the getSuccessEvent() method
            $this->_handle($command);
        } catch (\Exception $exception) {

            // Log exception
            $commandClass = get_class($command);
            $this->logError(
                "Exception throw while handling {$commandClass} command... {$exception->getMessage()}\n",
                $exception
            );

            // Fire failure event
            $this->emit($this->getFailureEvent($exception));

            throw $exception;
        }

        $event = $this->getSuccessEvent($command);
        $this->emit($event);
    }
1086162
public function execute(Command $command)
    {
        if ($this->locked === true) {
            $this->queue[] = $command;
            return null;
        }

        $this->locked = true;

        $response = $this->executeIgnoringLock($command);

        $this->executeQueue();

        $this->locked = false;

        return $response;
    }
900086
protected function runCommandInBackground(Container $container)
    {
        $this->callBeforeCallbacks($container);

        Process::fromShellCommandline($this->buildCommand(), base_path(), null, null, null)->run();
    }
267303
public function execute(Command $command)
    {
        if (!$this->innerCommandBus) {
            throw new Exception('No inner bus defined for this decorator. Set an inner bus with setInnerBus()');
        }

        $response = $this->innerCommandBus->execute($command);

        $eventName = $this->getEventName($command);

        $this->dispatcher->dispatch($eventName, [$command]);

        return $response;
    }