Initialize event

@param string $command
@param array  $args

@return CassandraEvent|null Return null if no eventDispatcher available
330308
protected function prepareEvent($command, array $args)
    {
        if (is_null($this->eventDispatcher)) {
            return null;
        }

        $event = new CassandraEvent();
        $event->setCommand($command)
              ->setKeyspace($this->getKeyspace())
              ->setArguments($args)
              ->setExecutionStart();

        return $event;
    }
783601
protected function createEventFromCommand(Command $command, string $suffix): Event
    {
        /** @var NodeRef $nodeRef */
        $nodeRef = $command->get('node_ref') ?: NodeRef::fromNode($command->get('node'));
        $curie = $command::schema()->getCurie();
        $eventCurie = "{$curie->getVendor()}:{$curie->getPackage()}:event:{$nodeRef->getLabel()}-{$suffix}";

        /** @var Event $class */
        $class = MessageResolver::resolveCurie(SchemaCurie::fromString($eventCurie));
        return $class::create();
    }
896584
public function command($command, $args = [], $id = null)
    {
        $target = Router::getInstance()->getTarget($command);
        $fn = function () use ($target) {
            return $target->run();
        };

        return $this->call($fn, $args, $id);
    }
1344283
public function processArgs( ICommand &$command, $args )
    {
        $i = 0;

        $ops = array_slice( $args, 2 );

        while ( $i < count( $ops ) )
        {
            $o = $ops[ $i ];

            if ( Option::isShortCode( $o ) || Option::isLongCode( $o ) )
            {
                $option = Command::getOption( $command, $o );

                if ( $option->isRequired )
                {
                    $i++;

                    if ( !Util::validIndex( $i, $ops ) )
                    {
                        throw new CommandException( 'Missing argument for option: ' . $o );
                    }

                    Option::setRequiredValue( $option, $ops[ $i ] );
                }

                if ( $option->isOptional )
                {
                    $value = NULL;

                    if ( $option->type === Type::BOOLEAN )
                    {
                        $value = TRUE;
                    }
                    else
                    {
                        $i++;

                        $value = $ops[ $i ];
                    }

                    if ( Util::validIndex( $i, $ops ) )
                    {
                        Option::setOptionalValue( $option, $value );
                    }
                    else if ( is_bool( $value ) )
                    {
                        Option::setOptionalValue( $option, $value );
                    }
                }
            }

            $i++;
        }
    }
92489
protected function exec($command, array $arguments = array())
    {
        $command = $this->bin . ' ' . $command;

        $commandline = $this->cli->prepare($command, $arguments);

        if ($this->cli->execute($commandline) <> 0) {
            throw new ProcessFailedException($this->cli->getProcess());
        } elseif ($message = $this->cli->getErrorOutput()) {
            throw new \RuntimeException($message);
        }

        return $this->cli->getOutput();
    }
1089423
public function run(Command $command, array $args) : int
    {
        $programName = basename(array_shift($args));

        $arg = current($args);

        if ($arg === '--help') {
            $this->printUsage($command, $programName);
            return 0;
        }

        if ($arg === '--version') {
            $this->printVersion($command);
            return 0;
        }

        try {
            $changeSet = $command->createChangeSet($args);
        } catch (BadUsage $e) {
            $this->printUsage($command, $programName);
            return 1;
        }

        $loader = new ProjectLoader(getcwd());
        $loader->registerClassLoader();

        $config = $loader->getPhpCodeSnifferConfiguration();

        try {
            if ($config !== null) {
                foreach ($config as $key => $value) {
                    Config::setConfigData($key, $value, true);
                }
            }

            // pass configuration using CLI arguments to override what's defined in the rule set
            $config = new Config(array_merge($args, ['--no-cache']));
            $runner = new Runner($config);

            return $runner->run($changeSet);
        } catch (DeepExitException $e) {
            echo $e->getMessage();
            return $e->getCode();
        }
    }
289368
protected function runCommand(Command $command, array $argv, ConsoleIo $io)
    {
        try {
            return $command->run($argv, $io);
        } catch (StopException $e) {
            return $e->getCode();
        }
    }
746057
public function trigger($event, $target = null, $argv = [])
    {
        $event = $event instanceof EventInterface
            ? $event
            : new Event($event);

        $event->setTarget($target ?: $this);
        $event->setParams($argv);

        $eventName = $event->getName();

        if (!isset($this->eventListeners[$eventName]) || $this->eventListeners[$eventName]->isEmpty()) {
            return null;
        }

        $listeners = clone $this->eventListeners[$eventName];
        $listeners->setExtractFlags(ListenerQueue::EXTR_DATA);
        $result = null;
        foreach ($listeners as $callback) {
            $result = call_user_func($callback, $event);

            if ($event->isPropagationStopped()) {
                return $result;
            }
        }

        return $result;
    }
70214
public function trigger($event, $target = null, $argv = [])
    {
        try {
            $result = false;

            // Create event or complete
            if ($event instanceof EventInterface) {
                if (!is_null($target)) {
                    $event->setTarget($target);
                }
                if (!empty($argv)) {
                    $event->setParams($argv);
                }
            } else {
                $event = new Event($event, $target, $argv);
            }

            // Call listeners
            if (Event::validEventName($event->getName()) && isset($this->listeners[$event->getName()])) {
                // Order by priority
                usort(
                    $this->listeners[$event->getName()],
                    function ($a, $b) {
                        if ($a['priority'] == $b['priority']) {
                            return 0;
                        }

                        return ($a['priority'] < $b['priority']) ? -1 : 1;
                    });

                // Call callbacks
                foreach ($this->listeners[$event->getName()] as $listener) {
                    if (!$event->isPropagationStopped()) {
                        $result = call_user_func($listener['callback'], $event);
                    }
                }
            }

            return $result;
        } catch (\Exception $e) {
            return false;
        }
    }
1168667
public function getJob($command, array $args = [])
    {
        if (null !== $job = $this->findJob($command, $args)) {
            return $job;
        }

        throw new \RuntimeException(sprintf('Found no job for command "%s" with args "%s".', $command, json_encode($args)));
    }
1054163
protected function callService($command, $data = array())
    {
        if (!$this->init) {
            throw new ClientNotInitializedException();
        }

        $data['auth.key'] = $this->clientApiConfig->getKey();
        $data['auth.secret'] = $this->clientApiConfig->getSecret();
        $data['command'] = $command;
        $data['project_id'] = $this->clientApiConfig->getProjectId();

        $msg = json_encode($data).PHP_EOL;

        $this->sendMessage($msg);

        $buffer = $this->readSocket();

        if ($this->debug) {
            print $buffer;
        }

        $result = json_decode($buffer, true);
        if (!count($result)) {
            throw new NullSocketResponseException();
        }

        return $result;
    }
757512
public static function exec($command, array $params = array(), $mergeStdErr=true)
    {
        if (empty($command)) {
            throw new \InvalidArgumentException('Command line is empty');
        }

        $command = self::bindParams($command, $params);

        if ($mergeStdErr) {
            // Redirect stderr to stdout to include it in $output
            $command .= ' 2>&1';
        }

        exec($command, $output, $code);

        if (count($output) === 0) {
            $output = $code;
        } else {
            $output = implode(PHP_EOL, $output);
        }

        if ($code !== 0) {
            throw new CommandException($command, $output, $code);
        }

        return $output;
    }
255606
public function runCommand( $command, $args = array(), $timeout = 86400 ) {

		// Convert a string args to an array
		if(!is_array($args)) $args = array($args);

		// Set a timeout so it's always set to either the default or the passed in parameter
		$timeout = ( isset( $timeout ) ? intval( $timeout ) : $this->connection->timeout );

                // Trim and then cast the command to a string, just to make sure
                $toWrite = strval( trim( $command ));

		// If the args is an array, then first escape double quotes in every element, then implode to strings delimted by enclosing quotes
		if( is_array( $args ) && ( count( $args ) > 0 )) {

			// This was causing an array to string conversion during the status command
			//$toWrite .= ' "' . implode('" "', str_replace( '"', '\"', $args )) . '"';

			// We need to escape any args that contain double quotes
			$args = array_map(function($element) {

				if(is_array($element)) {

					return array(str_replace('"', '\"', current($element)));

				} else {

					return str_replace('"', '\"', $element);
				}

			}, array_filter($args, function($element) {

				// Filter out any array elements that are also arrays
				return !is_array($element);
			}));

			// Make sure the args array is not now an empty array (in case it was filtered above)
			if (count($args) > 0) {

				$toWrite .= ' "' . implode('" "', $args) . '"';
			}
		}

		//Log::info( 'LxMPD runCommand toWrite', array($toWrite));

                // Write command to MPD socket
                $this->write( $toWrite );

		// Set the timeout in seconds
		$this->connection->setStreamTimeout( $timeout );

                // Read the response from the MPD socket
                $response = $this->read();

		// Set the timeout in seconds
		$this->connection->setStreamTimeout( $timeout );

		//Log::info( 'LxMPD runCommand response', array($response));
		//Log::info( 'LxMPD runCommand command', array($command));
		//Log::info( 'LxMPD runCommand args', array($args));

                // Return the parsed response array
                return $this->parse( $response, $command, $args );
        }
202351
protected function createEventFromCommand(Command $command, string $suffix): Event
    {
        $curie = $command::schema()->getCurie();
        $eventCurie = "{$curie->getVendor()}:{$curie->getPackage()}:event:app-{$suffix}";

        /** @var Event $class */
        $class = MessageResolver::resolveCurie(SchemaCurie::fromString($eventCurie));
        return $class::create();
    }
576252
protected function _prepareCommand($operation, $args)
	{
	    $args = ($args) ?:$this->args;
	    $command = new Command($this->service, $operation, $args);
	    
	    if ($this->context) {
	        $command->setContext($this->context);
	    } else {
	        $command->setContext($this->broker->getDefaultContext());
	    }
	    
	    if ($this->identity) {
	        $command->setIdentity($this->identity);
	    } else {
	        $command->setIdentity($this->broker->getDefaultIdentity());
	    }
	    
	    return $command;
	}
260215
protected function notifyEvent($command, $arguments, $return, $time = 0)
    {
        if ($this->eventDispatcher) {
            $event = new $this->eventClass();
            $event->setCommand($command)
                  ->setArguments($arguments)
                  ->setReturn($return)
                  ->setExecutionTime($time);

            $this->eventDispatcher->dispatch('amqp.command', $event);
        }
    }
1250075
public static function main(array $argv = [], $command = null)
    {
        $code = 0;

        $output = new ConsoleOutput();
        $input  = new ArgvInput();

        try {
            $c = static::createCli();

            array_shift($argv);

            if (null !== $command) {
                array_unshift($argv, $command);
            }

            $c->run($input, $output, $argv);
        } catch (\Exception $e) {
            if ('true' !== getenv('DEV_QUIET')) {
                $output->writeln(sprintf('<error>Error #%d: %s</error>', $e->getCode(), $e->getMessage()));
            }

            $code = ($e->getCode() > 0 && $e->getCode() < 255) ? $e->getCode() : 255;
        }

        return $code;
    }