setDispatcher() публичный метод

public setDispatcher ( Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher )
$dispatcher Symfony\Component\EventDispatcher\EventDispatcherInterface
Пример #1
0
 /**
  * Registers services on the given container.
  *
  * This method should only be used to configure services and parameters.
  * It should not get services.
  *
  * @param \Pimple\Container $container A container instance
  */
 public function register(Container $container)
 {
     $container['console.name'] = 'Console';
     $container['console.version'] = '1.0';
     $container['console.event_dispatcher'] = null;
     $container['console.enable_xdebug'] = false;
     $container['console'] = function (Container $container) {
         $console = new ConsoleApplication($container['console.name'], $container['console.version']);
         if ($container['console.enable_xdebug'] && function_exists('xdebug_enable')) {
             $definition = $console->getDefinition();
             $definition->addOption(new InputOption('debug', null, InputOption::VALUE_NONE, 'Enable XDebug jit remote mode'));
             $console->setDefinition($definition);
             foreach ($_SERVER['argv'] as $arg) {
                 if ('--debug' === $arg) {
                     poke_xdebug();
                     break;
                 } elseif ('--' === $arg) {
                     break;
                 }
             }
         }
         $dispatcher = $container['console.event_dispatcher'];
         if (is_string($dispatcher)) {
             $dispatcher = $container[$dispatcher];
         }
         if ($dispatcher instanceof EventDispatcherInterface) {
             $console->setDispatcher($dispatcher);
             $dispatcher->dispatch(Events::INIT, new InitializeConsoleEvent($console));
         }
         return $console;
     };
 }
 public function runTest()
 {
     $dispatcher = $this->dispatcher = new EventDispatcher();
     $application = new Application();
     $application->setAutoExit(false);
     $application->setDispatcher($dispatcher);
     $this->testOne();
     $this->testTwo();
     $application->run();
 }
Пример #3
0
 public function run()
 {
     $console = new Application(self::APP_NAME, self::APP_VERSION);
     $console->setDefaultCommand(self::APP_DEFAULT_COMMAND);
     $dispatcher = new EventDispatcher();
     $dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
         // get the input instance
         // $input = $event->getInput();
         // get the output instance
         $output = $event->getOutput();
         // get the command to be executed
         $command = $event->getCommand();
         // write something about the command
         //$output->writeln(sprintf('Before running command <info>%s</info>', $command->getName()));
         if ($command->getName() == StartServer::CLI_COMMAND) {
             $output->write(self::$BANNER);
         }
         // get the application
         // $application = $command->getApplication();
     });
     $console->setDispatcher($dispatcher);
     $console->addCommands([new Controller\StartServer(), new Controller\StopServer(), new Controller\ListServers(), new Controller\RegisterServer()]);
     $console->run();
 }
Пример #4
0
 public function testRunWithDispatcherAddingInputOptions()
 {
     $extraValue = null;
     $dispatcher = $this->getDispatcher();
     $dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use(&$extraValue) {
         $definition = $event->getCommand()->getDefinition();
         $input = $event->getInput();
         $definition->addOption(new InputOption('extra', null, InputOption::VALUE_REQUIRED));
         $input->bind($definition);
         $extraValue = $input->getOption('extra');
     });
     $application = new Application();
     $application->setDispatcher($dispatcher);
     $application->setAutoExit(false);
     $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
         $output->write('foo.');
     });
     $tester = new ApplicationTester($application);
     $tester->run(array('command' => 'foo', '--extra' => 'some test value'));
     $this->assertEquals('some test value', $extraValue);
 }
Пример #5
0
<?php

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
$console = new Application('Artsper Backoffice', 'n/a');
$console->getDefinition()->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev'));
$console->setDispatcher($app['dispatcher']);
$console->register('my-command')->setDefinition(array())->setDescription('My command description')->setCode(function (InputInterface $input, OutputInterface $output) use($app) {
    // do something
});
return $console;
Пример #6
0
 public function testRunWithDispatcherSkippingCommand()
 {
     $application = new Application();
     $application->setDispatcher($this->getDispatcher(true));
     $application->setAutoExit(false);
     $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
         $output->write('foo.');
     });
     $tester = new ApplicationTester($application);
     $exitCode = $tester->run(array('command' => 'foo'));
     $this->assertContains('before.after.', $tester->getDisplay());
     $this->assertEquals(ConsoleCommandEvent::RETURN_CODE_DISABLED, $exitCode);
 }
Пример #7
0
 public function testRunDispatchesAllEventsWithException()
 {
     if (!class_exists('Symfony\\Component\\EventDispatcher\\EventDispatcher')) {
         $this->markTestSkipped('The "EventDispatcher" component is not available');
     }
     $application = new Application();
     $application->setDispatcher($this->getDispatcher());
     $application->setAutoExit(false);
     $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
         $output->write('foo.');
         throw new \RuntimeException('foo');
     });
     $tester = new ApplicationTester($application);
     $tester->run(array('command' => 'foo'));
     $this->assertContains('before.foo.after.caught.', $tester->getDisplay());
 }
Пример #8
0
 public function testRunDispatchesAllEventsWithException()
 {
     $application = new Application();
     $application->setDispatcher($this->getDispatcher());
     $application->setAutoExit(false);
     $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
         $output->write('foo.');
         throw new \RuntimeException('foo');
     });
     $tester = new ApplicationTester($application);
     $tester->run(array('command' => 'foo'));
     $this->assertContains('before.foo.after.caught.', $tester->getDisplay());
 }
 $container = \DG\App\loadContainer();
 $eventDispatcher = $container->get('event_dispatcher.traceable');
 $app = new Application();
 $app->add(new MakesCacheCommand($container));
 $app->add(new CssSelectorTestCommand());
 $app->add($reportCommand = new MakesCacheReportCommand());
 $app->add(new DomCrawlerTestCommand());
 $app->add(new DomCrawlerHHTestCommand());
 $app->add(new ExpressionLanguageCommand());
 $app->add(new LockHandlerTestCommand());
 $app->add(new IntlTestCommand());
 $app->add(new ModelsCommand($container));
 $app->add(new ProcessCommand());
 $app->add(new TestTimingAttackCommand());
 $app->setDefaultCommand($reportCommand->getName());
 $app->setDispatcher($eventDispatcher);
 $eventDispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) use($reportCommand) {
     $output = $event->getOutput();
     $output->writeln("<info>Run command " . $event->getCommand()->getName() . "</info>");
     if ($event->getCommand() instanceof MakesCacheReportCommand) {
         $finder = new Finder();
         $makesCacheFilesCount = $finder->in(CACHE_PATH)->name('makes*.json')->count();
         if ($makesCacheFilesCount === 0 && $event->getCommand()->getName() === $reportCommand->getName()) {
             $output->writeln("<error>There is no cache files. Command execution stopped</error>");
             $event->disableCommand();
         }
     }
 });
 $eventDispatcher->addListener(ConsoleEvents::EXCEPTION, function (ConsoleExceptionEvent $event) {
     //wrap exception
 });
Пример #10
0
    return new Config($c['compose.project']);
};
$container['command.run'] = function ($c) {
    return new RunCommand($c['process.interactive_runner'], $c['compose.config']);
};
$container['event_dispatcher'] = function () {
    return new EventDispatcher();
};
$container['logs'] = function ($c) {
    return new Logs($c['compose.executable_finder'], $c['process.silent_runner']);
};
$container['doctor.tasks'] = function ($c) {
    return [new Doctor\Docker($c['process.silent_runner'], new Doctor\Action\StartMachineOrInstall($c['machine'], $c['installer.docker']), $c['installer.docker']), new Doctor\DnsDock($c['process.silent_runner'], $c['installer.dns.dnsdock'], $c['installer.dns.docker_routing'])];
};
$container['application'] = function ($c) {
    $application = new Application('Dock CLI', '@package_version@');
    $application->setDispatcher($c['event_dispatcher']);
    $application->addCommands([$c['command.selfupdate'], $c['command.doctor'], $c['command.install'], $c['command.restart'], $c['command.run'], $c['command.start'], $c['command.stop'], $c['command.build'], $c['command.reset'], $c['command.ps'], $c['command.logs']]);
    return $application;
};
$osDetector = new OperatingSystemDetector();
if ($osDetector->isMac()) {
    require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'container.mac.php';
} elseif ($osDetector->isDebian()) {
    require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'container.debian.php';
} elseif ($osDetector->isRedHat()) {
    require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'container.redhat.php';
} else {
    throw new \Exception($osDetector->isLinux() ? "Installer does not support linux distribution: " . $osDetector->getLinuxDistribution() : "Installer does not support operating system: " . $osDetector->getOperatingSystem());
}
return $container;
 function assertRunCommandViaApplicationEquals($command, $input, $expectedOutput, $expectedStatusCode = 0)
 {
     $output = new BufferedOutput();
     if ($this->commandFileInstance && method_exists($this->commandFileInstance, 'setOutput')) {
         $this->commandFileInstance->setOutput($output);
     }
     $application = new Application('TestApplication', '0.0.0');
     $alterOptionsEventManager = new AlterOptionsCommandEvent($application);
     $eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
     $eventDispatcher->addSubscriber($this->commandFactory->commandProcessor()->hookManager());
     $eventDispatcher->addSubscriber($alterOptionsEventManager);
     $application->setDispatcher($eventDispatcher);
     $application->setAutoExit(false);
     $application->add($command);
     $statusCode = $application->run($input, $output);
     $commandOutput = trim($output->fetch());
     $this->assertEquals($expectedOutput, $commandOutput);
     $this->assertEquals($expectedStatusCode, $statusCode);
 }
Пример #12
0
 /**
  * Creates an instance of Console App
  * @return ConsoleApplication
  */
 public function createConsole()
 {
     $console = new ConsoleApplication($this['app.console_name']);
     $envOption = new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev');
     $console->getDefinition()->addOption($envOption);
     $console->setDispatcher($this['dispatcher']);
     return $console;
 }
Пример #13
0
 /**
  * {@inheritdoc}
  */
 public function registerConsole(Application $console)
 {
     $eventDispatcher = $this->app->getContainer()->resolve('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $console->setDispatcher($eventDispatcher);
 }