Add a listener for this event to execute custom logic before or instead of the default handler.
Since: 1.0
Author: Bernhard Schussek (bschussek@gmail.com)
Inheritance: extends Symfony\Component\EventDispatcher\Event
 public function initApplication(PreHandleEvent $event)
 {
     if (null !== $this->app) {
         return;
     }
     try {
         $this->app = new Jarvis($this->settings);
     } catch (\Exception $e) {
         $event->getIO()->writeLine("<error>{$e->getMessage()}</error>");
         exit(1);
     }
 }
 public function __invoke(PreHandleEvent $event)
 {
     if (isset($this->container['project_directory'])) {
         return;
     }
     $currentDirectory = str_replace('\\', '/', $this->container['current_dir']);
     $projectDirectory = $event->getArgs()->getOption('project-directory');
     if (!$projectDirectory) {
         $this->container['project_directory'] = $projectDirectory = $this->getProjectDirectory($currentDirectory);
     } else {
         $this->container['project_directory'] = $projectDirectoryResolved = str_replace('\\', '/', realpath($projectDirectory));
         if (!$projectDirectoryResolved) {
             throw new \InvalidArgumentException(sprintf('Project directory "%s" does not exist.', $projectDirectory));
         }
         if ($currentDirectory !== $projectDirectory && $projectDirectoryResolved !== mb_substr($currentDirectory, 0, mb_strlen($projectDirectoryResolved))) {
             throw new \InvalidArgumentException(sprintf('The current directory "%s" does not belong to the project-directory "%s"', $currentDirectory, $projectDirectoryResolved));
         }
     }
     if (is_dir($projectDirectory . '/.dancer')) {
         $this->container['dancer_directory'] = $this->container['project_directory'] . '/.dancer';
     }
     $this->container['config_file'] = $this->findDancerConfigFile($projectDirectory . '/', $currentDirectory, $event->getArgs()->getOption('config-file'));
 }
Esempio n. 3
0
 /**
  * @param PreHandleEvent $event
  */
 protected function onPreHandle(PreHandleEvent $event)
 {
     // convert console args to command
     $this->commandConverter->setArgs($event->getArgs());
     $this->commandConverter->setFormat($event->getCommand()->getArgsFormat());
     /** @var CommandConfig $commandConfig */
     $commandConfig = $event->getCommand()->getConfig();
     if ($event->getCommand()->getName() !== 'help') {
         if ($commandConfig->className() === null && count($commandConfig->getSubCommandConfigs()) === 0) {
             throw new \RuntimeException(sprintf('The command %s definition must set the className using ->setClass() method', $event->getCommand()->getName()));
         }
     }
     $command = $this->commandConverter->getCommandFrom($commandConfig->className());
     $this->defaultHandler->setIo($event->getIO());
     $this->defaultHandler->clearEventHandlers();
     if ($commandConfig->preDispatchEventHandler() !== null) {
         $this->defaultHandler->addPreDispatchEventHandler($commandConfig->preDispatchEventHandler());
     }
     if ($commandConfig->postDispatchEventHandler()) {
         $this->defaultHandler->addPostDispatchEventHandler($commandConfig->postDispatchEventHandler());
     }
     $commandConfig->setEventBus($this->eventBus);
     $commandConfig->addEventSubscriber($this->defaultHandler);
     if ($command !== null) {
         if ($command instanceof ConsoleCommandInterface) {
             $command->setIo($event->getIO());
         }
         try {
             $this->commandBus->dispatch($command);
             $event->setHandled(true);
             $event->setStatusCode(0);
         } catch (NotFoundException $e) {
         }
     } else {
         if ($event->getCommand()->getName() !== 'help') {
             $helpCommand = $this->getCommand('help');
             $args = $event->getArgs();
             $args = $helpCommand->parseArgs($args->getRawArgs());
             $args->setArgument('command', $event->getCommand()->getName());
             $helpConfig = $this->getConfig()->getCommandConfig('help');
             $commandHandler = $helpConfig->getHandler();
             $handlerMethod = $helpConfig->getHandlerMethod();
             $commandHandler->{$handlerMethod}($args, $event->getIO(), $helpCommand);
             $event->setHandled(true);
             $event->setStatusCode(0);
         }
     }
 }
 /**
  * @param PreHandleEvent $event
  */
 public function printVersion(PreHandleEvent $event)
 {
     if ($event->getArgs()->isOptionSet('version')) {
         $version = new NameVersion($event->getCommand()->getApplication()->getConfig());
         $version->render($event->getIO());
         $event->setHandled(true);
     }
 }