Webmozart\Console\ConsoleApplication::__construct PHP Method

__construct() public method

Creates a new console application.
public __construct ( ApplicationConfig | callable $config )
$config Webmozart\Console\Api\Config\ApplicationConfig | callable The application configuration or a callable that creates the configuration.
    public function __construct($config)
    {
        $this->preliminaryIo = new ConsoleIO();
        // Enable trace output for exceptions thrown during boot
        $this->preliminaryIo->setVerbosity(IO::VERY_VERBOSE);
        if (is_callable($config)) {
            try {
                $config = $config();
            } catch (Exception $e) {
                $trace = new ExceptionTrace($e);
                $trace->render($this->preliminaryIo);
                exit($this->exceptionToExitCode($e->getCode()));
            }
        }
        Assert::isInstanceOf($config, 'Webmozart\\Console\\Api\\Config\\ApplicationConfig', 'The $config argument must be an ApplicationConfig or a callable. Got: %s');
        try {
            $dispatcher = $config->getEventDispatcher();
            if ($dispatcher && $dispatcher->hasListeners(ConsoleEvents::CONFIG)) {
                $dispatcher->dispatch(ConsoleEvents::CONFIG, new ConfigEvent($config));
            }
            $this->config = $config;
            $this->dispatcher = $config->getEventDispatcher();
            $this->commands = new CommandCollection();
            $this->namedCommands = new CommandCollection();
            $this->defaultCommands = new CommandCollection();
            $this->globalArgsFormat = new ArgsFormat(array_merge($config->getOptions(), $config->getArguments()));
            foreach ($config->getCommandConfigs() as $commandConfig) {
                $this->addCommand($commandConfig);
            }
        } catch (Exception $e) {
            if (!$config->isExceptionCaught()) {
                throw $e;
            }
            // Render the trace to the preliminary IO
            $trace = new ExceptionTrace($e);
            $trace->render($this->preliminaryIo);
            // Ignore isTerminatedAfterRun() setting. This is a fatal error.
            exit($this->exceptionToExitCode($e->getCode()));
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * ConsoleApplication constructor.
  *
  * @param callable|ApplicationConfig $config
  * @param CommandBus                 $commandBus
  * @param EventBus                   $eventBus
  */
 public function __construct($config, CommandBus $commandBus, EventBus $eventBus)
 {
     $this->commandBus = $commandBus;
     $this->eventBus = $eventBus;
     $this->defaultHandler = new DefaultEventHandler();
     $this->commandConverter = new ConsoleArgsToCommand();
     parent::__construct($this->normalizeConfig($config));
 }