本文整理汇总了PHP中Symfony\Component\EventDispatcher\EventDispatcher::addListener方法的典型用法代码示例。如果您正苦于以下问题:PHP EventDispatcher::addListener方法的具体用法?PHP EventDispatcher::addListener怎么用?PHP EventDispatcher::addListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\EventDispatcher\EventDispatcher
的用法示例。
在下文中一共展示了EventDispatcher::addListener方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
public function __construct(EventDispatcher $dispatcher)
{
$this->stdErr = fopen('php://stderr', 'w');
$this->output = new StreamOutput($this->stdErr);
$this->eventDispatcher = $dispatcher;
$this->eventDispatcher->addListener(FixerFileProcessedEvent::NAME, array($this, 'onFixerFileProcessed'));
}
示例2: __construct
public function __construct()
{
parent::__construct(Berk::NAME, Berk::VERSION);
$time = time();
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
$output = $event->getOutput();
$directory = Git::getWorkingDirectory();
chdir($directory);
$output->writeln('<info>Changed current directory to: ' . $directory . '</info>');
$output->writeln('');
$command = $event->getCommand();
if ($command instanceof Command) {
$command->setBerk(new Berk());
}
});
$dispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) use($time) {
$output = $event->getOutput();
$time = time() - $time;
$output->writeln('');
$output->writeln('<info>Command completed in ' . $time . ' seconds. Bye.</info>');
});
$this->setDispatcher($dispatcher);
$this->add(new ServerCommand());
$this->add(new ExportCommand());
$this->add(new DeployCommand());
}
示例3: configureSingleState
/**
* @inheritdoc
*/
public function configureSingleState(EventDispatcher $dispatcher)
{
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
/** @var ConsoleCommand $command */
$command = $event->getCommand();
if (!$command instanceof ConsoleCommand or $command->isSingleInstance() == false) {
return;
}
$commandState = new CommandState($command);
if ($commandState->isAlive()) {
$event->getOutput()->writeln('Already running: ' . $command->getName() . ' Pid:' . $commandState->getPid());
$event->disableCommand();
} else {
$commandState->setPid(getmypid());
}
});
$dispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) {
/** @var ConsoleCommand $command */
$command = $event->getCommand();
if (!$command instanceof ConsoleCommand or $command->isSingleInstance() == false) {
return;
}
if ($event->getExitCode() === 0) {
(new CommandState($command))->unlink();
}
});
return $dispatcher;
}
示例4: __construct
public function __construct()
{
$this->dispatcher = new EventDispatcher();
$this->dispatcher->addListener(Events::TRANSFER_PRE, array(new Listener\Transfer\PreEventListener(), 'onPreAction'));
$this->dispatcher->addListener(Events::TRANSFER_POST, array(new Listener\Transfer\PostEventListener(), 'onPostAction'));
$this->dispatcher->addListener(Events::TRANSFER_TOTAL_TIMEOUT, array(new Listener\Transfer\TotalTimeoutEventListener(), 'onTimeoutAction'));
}
示例5: testRecursiveMessage
public function testRecursiveMessage()
{
$dispatcher = new EventDispatcher();
$backend = new PostponeRuntimeBackend($dispatcher, true);
$dispatcher->addListener('kernel.terminate', array($backend, 'onEvent'));
$message1 = $backend->create('notification.demo1', array());
$message2 = $backend->create('notification.demo2', array());
$backend->publish($message1);
$phpunit = $this;
$phpunit->passed1 = false;
$phpunit->passed2 = false;
$dispatcher->addListener('notification.demo1', function (ConsumerEventInterface $event) use($phpunit, $message1, $message2, $backend, $dispatcher) {
$phpunit->assertSame($message1, $event->getMessage());
$phpunit->passed1 = true;
$backend->publish($message2);
});
$dispatcher->addListener('notification.demo2', function (ConsumerEventInterface $event) use($phpunit, $message2) {
$phpunit->assertSame($message2, $event->getMessage());
$phpunit->passed2 = true;
});
$dispatcher->dispatch('kernel.terminate');
$this->assertTrue($phpunit->passed1);
$this->assertTrue($phpunit->passed2);
$this->assertEquals(MessageInterface::STATE_DONE, $message1->getState());
$this->assertEquals(MessageInterface::STATE_DONE, $message2->getState());
}
示例6: addCookieResponseListener
/**
* Method to add the ResponseListener which sets the cookie. Should only be called once
*
* @see also http://slides.seld.be/?file=2011-10-20+High+Performance+Websites+with+Symfony2.html#45
*
* @return void
*/
public function addCookieResponseListener()
{
if ($this->cookieListenerisAdded !== true) {
$this->dispatcher->addListener(KernelEvents::RESPONSE, array($this, 'onResponse'));
}
$this->cookieListenerisAdded = true;
}
示例7: test
public function test()
{
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener(ImportItemEvent::AFTER_READ, array($this, 'onAfterRead'));
$eventDispatcher->addListener(ImportItemEvent::AFTER_FILTER, array($this, 'onAfterFilter'));
$eventDispatcher->addListener(ImportItemEvent::AFTER_CONVERSION, array($this, 'onAfterConversion'));
$eventDispatcher->addListener(ImportItemEvent::AFTER_CONVERSIONFILTER, array($this, 'onAfterConversionFilter'));
$eventDispatcher->addListener(ImportItemEvent::AFTER_WRITE, array($this, 'onAfterWrite'));
$fileDir = __DIR__ . '/../../../../../metadata/testfiles';
$finder = Finder::create()->in($fileDir)->name('*');
$lfsp = new FinderFileStorageProvider($finder);
$lfsp->setStorageFactory(new FormatDiscoverLocalFileStorageFactory(new MimeTypeDiscoverStrategy(array('text/plain' => new CsvAutoDelimiterFormatFactory()))));
$storageLocator = new StorageLocator();
$storageLocator->register('defaultProvider', $lfsp);
$array = array();
$targetStorage = new ArrayStorage($array);
$importer = Importer::build($targetStorage);
$importRepository = new ImporterRepository();
$importRepository->register('defaultImporter', $importer);
$importRequest = new ImportRequest($fileDir . '/100.csv', 'defaultProvider', 'defaultImporter');
$importBuilder = new ImportBuilder($importRepository, $storageLocator);
$import = $importBuilder->buildFromRequest($importRequest);
$import->mappings()->add('prefix', 'Anrede', 'upperCase')->add('name', 'Name', 'lowerCase');
$importRunner = new ImportRunner(new DefaultWorkflowFactory($eventDispatcher));
$expectedResult = array('Name' => 'jennie abernathy', 'Anrede' => 'MS.', 'street' => '866 Hyatt Isle Apt. 888', 'zip' => '65982', 'city' => 'East Laurie', 'phone' => '(551)436-0391', 'email' => 'runolfsson.moriah@yahoo.com');
$previewResult = $importRunner->preview($import, 0);
$this->assertEquals($expectedResult, $previewResult['to']);
}
示例8: addListener
/**
* Add a callback to a channel which will get triggered when channel receives a message via a publisher.
*
* @param string $channel
* @param callable $callback
* @param int $priority Default: 0
*
* @return PubSubManager
* @throws InvalidArgumentException
*/
public function addListener($channel, callable $callback, $priority = 0)
{
if (empty($channel)) {
throw new InvalidArgumentException('Empty event channel name supplied.');
}
$this->event_dispatcher->addListener(static::generateEventName($channel), $callback, $priority);
return $this;
}
示例9: subscribe
/**
* Subscribes the message handler to the bus.
* @param MessageHandlerInterface $messageHandler The message handler.
*/
public function subscribe(MessageHandlerInterface $messageHandler)
{
foreach ($messageHandler->getMessageSubscriptions() as $subscription) {
$this->eventDispatcher->addListener($subscription, function (GenericEvent $event) use($messageHandler) {
$messageHandler->handle($event->getSubject());
});
}
}
示例10: addHooks
/**
* Set all hooks
*
* @return void
*/
protected function addHooks()
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::COMMAND, new Header());
$dispatcher->addListener(ConsoleEvents::COMMAND, new ValidateDependencies());
$dispatcher->addListener(ConsoleEvents::TERMINATE, new TotalTime($this->timeStart));
$this->setDispatcher($dispatcher);
}
示例11: __construct
public function __construct()
{
$this->repository = new InMemoryExpenseListRepository();
$dispatcher = new EventDispatcher();
$this->createExpenseListCase = new CreateExpenseList($this->repository, $dispatcher);
$dispatcher->addListener(CreateExpenseList::SUCCESS, [$this, 'recordNotification']);
$dispatcher->addListener(CreateExpenseList::FAILURE, [$this, 'recordNotification']);
}
示例12: testTearDown
public function testTearDown()
{
$events = array();
$this->dispatcher->addListener('test.after', function ($e) use(&$events) {
$events[] = $e->getName();
});
$this->testcase->tearDown();
$this->assertEquals($events, array('test.after'));
}
示例13: monitorEvents
/**
* Add listeners to $this->dispatcher which record each invocation of $monitoredEvents
* in $this->actualEventSequence.
*
* @param array $monitoredEvents
* List of event names.
*
*/
public function monitorEvents($monitoredEvents)
{
foreach ($monitoredEvents as $monitoredEvent) {
$test = $this;
$this->dispatcher->addListener($monitoredEvent, function ($event) use($monitoredEvent, &$test) {
$test->actualEventSequence[] = array('name' => $monitoredEvent, 'class' => get_class($event));
}, 2 * Events::W_EARLY);
}
}
示例14: attachListeners
/**
* Attach all event listeners to the $eventDispatcher.
*/
private function attachListeners()
{
// Create the listeners.
$logReferralReasonsListener = new LogReferralReasonsListener(new Datasource_Insurance_PolicyNotes());
$dispatchReferralEmailListener = new DispatchReferralEmailListener($this->getContainer()->get('twig'), $this->_params, new Application_Core_Mail());
// The above two listeners are both listening for the RRPEvents::POLICY_REFERRED event to be fired.
$this->eventDispatcher->addListener(RRPEvents::POLICY_REFERRED, array($logReferralReasonsListener, 'onReferral'));
$this->eventDispatcher->addListener(RRPEvents::POLICY_REFERRED, array($dispatchReferralEmailListener, 'onReferral'));
}
示例15: setUp
public function setUp()
{
$this->dispatcher = new EventDispatcher('UTF-8');
$listener = new RequestListener($this->dispatcher);
$this->dispatcher->addListener(KernelEvents::REQUEST, [$listener, 'onKernelRequest']);
$this->dispatcher->addListener(QPushEvents::Notification('ironmq-test'), [$this, 'IronMqOnNotificationReceived']);
$this->dispatcher->addListener(QPushEvents::Notification('aws-test'), [$this, 'AwsOnNotificationReceived']);
$this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
}