public function notify(OrderCreatedEvent $event) { $captureToken = $this->tokenFactory->createToken($event->getOrder(), $event->getGatewayName()); $this->eventDispatcher->addListener(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use($captureToken) { $event->setResponse(new RedirectResponse($captureToken->getTargetUrl())); }, -1000); }
/** * {@inheritDoc} */ public function addListener($eventName, $listener, $priority = 0) { if (!$this->primaryEventDispatcher) { throw new \RuntimeException('Undefined primary event dispatcher.'); } $this->primaryEventDispatcher->addListener($eventName, $listener, $priority); }
/** * Executes controller. * * @param InputInterface $input * @param OutputInterface $output * * @return null|integer */ public function execute(InputInterface $input, OutputInterface $output) { if (!$input->getOption('stop-on-failure')) { return null; } $this->eventDispatcher->addListener(ScenarioTested::AFTER, array($this, 'exitOnFailure'), -100); $this->eventDispatcher->addListener(ExampleTested::AFTER, array($this, 'exitOnFailure'), -100); }
public function registerListeners(EventDispatcherInterface $dispatcher) { // Pre-render filters $dispatcher->addListener(Events::preRender($this), array($this, 'preFilterLayout')); $dispatcher->addListener(Events::preRender($this), array($this, 'preFilterRelativePosition')); // Post-render filters $dispatcher->addListener(Events::postRender($this), array($this, 'postFilterLayout')); }
/** * @param EventDispatcherInterface $dispatcher * @param OutputInterface $output */ public function __construct(EventDispatcherInterface $dispatcher, OutputInterface $output) { $this->output = $output; $dispatcher->addListener('elasticsearch.has_started_handling', [$this, 'onStartedHandling']); $dispatcher->addListener('elasticsearch.has_started_providing', [$this, 'onStartedProviding']); $dispatcher->addListener('elasticsearch.has_provided_document', [$this, 'onProvidedDocument']); $dispatcher->addListener('elasticsearch.has_finished_providing', [$this, 'onFinishedProviding']); }
/** * @param Router $router * @param Request $request * @param ActionFactoryInterface $action_factory * @param PresenterInterface $presenter * @param EventDispatcherInterface $event_dispatcher * @param EventHandlerInterface $exception_handler */ public function __construct(Router $router, Request $request, ActionFactoryInterface $action_factory, PresenterInterface $presenter, EventDispatcherInterface $event_dispatcher, EventHandlerInterface $exception_handler) { $this->router = $router; $this->request = $request; $this->action_factory = $action_factory; $this->presenter = $presenter; $this->events = $event_dispatcher; $this->events->addListener(DispatchEvents::DISPATCH_ERROR, $exception_handler, DispatchEvents::LATE_EVENT); }
public function __invoke($name, $value, $options) { $placeholder = '<!-- ' . uniqid('section.') . ' -->'; $this->events->addListener(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use($value, $placeholder) { $response = $event->getResponse(); $response->setContent(str_replace($placeholder, $this->render($value), $response->getContent())); }, 10); return $placeholder; }
protected function prepareForInitCollections() { $this->user->expects($this->any())->method('getUID')->will($this->returnValue('alice')); $this->userSession->expects($this->once())->method('getUser')->will($this->returnValue($this->user)); $this->dispatcher->addListener(CommentsEntityEvent::EVENT_ENTITY, function (CommentsEntityEvent $event) { $event->addEntityCollection('files', function () { return true; }); }); }
/** * {@inheritdoc} */ public function execute($object = NULL) { $url = $this->urlGenerator->generateFromPath($this->configuration['url'], array('absolute' => TRUE)); $response = new RedirectResponse($url); $listener = function ($event) use($response) { $event->setResponse($response); }; // Add the listener to the event dispatcher. $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener); }
protected function registerListeners(EventDispatcherInterface $dispatcher) { // $dispatcher->addListener takes two parameters: an event name, and a callable // Default events names can be found in Manialib\XML\Rendering\Events // There are some "global" events $dispatcher->addListener(Events::PRE_RENDER, array($this, 'preGlobalRender')); // But also some events related to the current instance. // Manialib\XML\Rendering\Events provides shortcuts to generate instance-related event names $dispatcher->addListener(Events::preRender($this), array($this, 'preThisRender')); // Full documetation of Event Dispatcher at // http://symfony.com/doc/current/components/event_dispatcher/index.html }
/** * @param StateMachineInterface|Callback $smOrCallback * @param string $event * @param callable $callable * @param array $specs * * @return CallbackHandler */ protected function add($smOrCallback, $event, $callable = null, array $specs = array()) { if ($smOrCallback instanceof Callback) { $this->dispatcher->addListener($event, $smOrCallback); return $this; } trigger_error('Use of CallbackHandler::add without a Callback instance is deprecated and will be removed in 2.0', E_USER_DEPRECATED); $specs = $this->specResolver->resolve($specs); $callback = CallbackBuilder::create($smOrCallback, $specs['from'], $specs['to'], $specs['on'], $callable)->getCallback(); $this->dispatcher->addListener($event, $callback); return $this; }
/** * @param EventDispatcherInterface $dispatcher * @param ProgressHelper $progress * @param OutputInterface $output */ protected function setupProgressListeners(EventDispatcherInterface $dispatcher, ProgressHelper $progress, OutputInterface $output) { $dispatcher->addListener(IoEvents::PRE_EXPORT_FEED, function (ExportFeedEvent $event) use($progress, $output) { $output->writeln(sprintf('Exporting feed for <info>%s</info> to <info>%s</info>', $event->getType()->getName(), $event->getFile())); $progress->start($output, $event->getTotal()); }); $dispatcher->addListener(IoEvents::POST_EXPORT_ITEM, function () use($progress) { $progress->advance(1); }); $dispatcher->addListener(IoEvents::POST_EXPORT_FEED, function () use($progress) { $progress->finish(); }); }
/** * @inheritDoc */ protected function attachEvents(OutputInterface $output, EventDispatcherInterface $dispatcher) { $this->output = $output; if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) { $dispatcher->addListener(EventInterface::COLLECTION_BEFORE, [$this, 'onCollectionBefore']); $dispatcher->addListener(EventInterface::COLLECTION_AFTER, [$this, 'onCollectionAfter']); $dispatcher->addListener(EventInterface::MIGRATION_BEFORE, [$this, 'onMigrationBefore']); $dispatcher->addListener(EventInterface::MIGRATION_AFTER, [$this, 'onMigrationAfter']); } if ($this->saveChanges) { $dispatcher->addListener(EventInterface::MIGRATION_AFTER, [$this, 'saveVersionListener']); } }
/** * @ActivityListQueryDesigner $queryDesigner * * @return DatagridInterface */ public function createGrid(ActivityListQueryDesigner $source) { $this->datagridConfigurationBuilder->setGridName('related-activity'); $this->datagridConfigurationBuilder->setSource($source); $config = $this->datagridConfigurationBuilder->getConfiguration(); $stopPropagationListener = function (Event $e) { $e->stopPropagation(); }; $this->eventDispatcher->addListener(BuildBefore::NAME, $stopPropagationListener, 255); $grid = $this->gridBuilderLink->getService()->build($config, new ParameterBag()); $this->eventDispatcher->removeListener(BuildBefore::NAME, $stopPropagationListener); return $grid; }
/** * {@inheritdoc} */ protected function subscribe(EventDispatcherInterface $dispatcher) { // Install the YAML configuration file. $this->getConfig(); /** @var Application $app */ $app = $this->getContainer(); // Storage events test callback class $storageEventTests = new StorageEventTests($app['storage']); // Storage event listeners $dispatcher->addListener(StorageEvents::PRE_SAVE, [$storageEventTests, 'eventPreSave']); $dispatcher->addListener(StorageEvents::POST_SAVE, [$storageEventTests, 'eventPostSave']); $dispatcher->addListener(StorageEvents::PRE_DELETE, [$storageEventTests, 'eventPreDelete']); $dispatcher->addListener(StorageEvents::POST_DELETE, [$storageEventTests, 'eventPreDelete']); }
public function subscribe(Container $app, EventDispatcherInterface $dispatcher) { $onTerminate = function (PostResponseEvent $event) use($app) { // To speed things up (by avoiding Swift Mailer initialization), flush // messages only if our mailer has been created (potentially used) if ($app['mailer.initialized']) { $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']); } }; $dispatcher->addListener(KernelEvents::TERMINATE, $onTerminate); if (class_exists('Symfony\\Component\\Console\\ConsoleEvents')) { $dispatcher->addListener(ConsoleEvents::TERMINATE, $onTerminate); } }
/** * {@inheritdoc} */ public function execute($order = NULL) { $build = array('#theme' => 'uc_order_invoice', '#order' => $order, '#op' => 'print', '#prefix' => '<div style="page-break-after: always;">', '#suffix' => '</div>'); $output = '<html><head><title>Invoice</title></head>'; $output .= '<body onload="print();">'; $output .= drupal_render($build); $output .= '</body></html>'; $response = new Response($output); $listener = function ($event) use($response) { $event->setResponse($response); }; // Add the listener to the event dispatcher. $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener); }
/** * Executes controller. * * @param InputInterface $input * @param OutputInterface $output * * @return null|integer */ public function execute(InputInterface $input, OutputInterface $output) { $this->eventDispatcher->addListener(StepTested::AFTER, array($this, 'registerUndefinedStep'), -999); $this->output = $output; if ($input->getOption('append-snippets')) { $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'appendAllSnippets'), -999); } if (!$input->getOption('no-snippets') && !$input->getOption('append-snippets')) { $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'printAllSnippets'), -999); } if (!$input->getOption('no-snippets')) { $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'printUndefinedSteps'), -995); } }
/** * Executes controller. * * @param InputInterface $input * @param OutputInterface $output * * @return null|integer */ public function execute(InputInterface $input, OutputInterface $output) { $this->eventDispatcher->addListener(ScenarioTested::AFTER, array($this, 'collectFailedScenario'), -50); $this->eventDispatcher->addListener(ExampleTested::AFTER, array($this, 'collectFailedScenario'), -50); $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'writeCache'), -50); $this->key = $this->generateKey($input); if (!$input->getOption('rerun')) { return; } if (!$this->getFileName() || !file_exists($this->getFileName())) { return; } $input->setArgument('paths', $this->getFileName()); }
/** * @inheritdoc */ public function addSubscriber(EventSubscriberInterface $subscriber) { // This is what the actual Symfony Event Dispatcher does. Replicate it to remove the dependency on its interface. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { if (is_string($params)) { $this->dispatcher->addListener($eventName, array($subscriber, $params)); } elseif (is_string($params[0])) { $this->dispatcher->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0); } else { foreach ($params as $listener) { $this->dispatcher->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0); } } } }
/** * {@inheritdoc} */ public function generate(MediaProviderInterface $provider, MediaInterface $media) { $backend = $this->backend; $id = $this->id; $publish = function () use($backend, $media, $id) { $backend->createAndPublish('sonata.media.create_thumbnail', array('thumbnailId' => $id, 'mediaId' => $media->getId(), 'providerReference' => $media->getProviderReference())); }; // BC compatibility for missing EventDispatcher if (null === $this->dispatcher) { trigger_error('Since version 2.3.3, passing an empty parameter in argument 4 for __construct() in ' . __CLASS__ . ' is deprecated and the workaround for it will be removed in 3.0.', E_USER_DEPRECATED); $publish(); } else { $this->dispatcher->addListener('kernel.finish_request', $publish); } }
/** * @param StateMachineInterface $sm * @param string $event * @param callable $callback * @param array $specs * * @return $this */ protected function add(StateMachineInterface $sm, $event, $callback, array $specs) { $specs = $this->processSpecs($specs); $listener = function (TransitionEvent $e) use($sm, $callback, $specs) { if ($sm !== $e->getStateMachine()) { return; } if (!(in_array(CallbackHandler::ALL, $specs['to']) || in_array($e->getTransition()->getState(), $specs['to']))) { return; } if (!(in_array(CallbackHandler::ALL, $specs['from']) || in_array($e->getInitialState()->getName(), $specs['from']))) { return; } if (in_array($e->getTransition()->getState(), $specs['exclude_to'])) { return; } if (in_array($e->getInitialState()->getName(), $specs['exclude_from'])) { return; } call_user_func($callback, $sm->getObject(), $e); }; $events = array($event); if (count($specs['on']) > 0 && !in_array(self::ALL, $specs['on'])) { $events = array_map(function ($v) use($event) { return $event . '.' . $v; }, $specs['on']); } foreach ($events as $event) { $this->dispatcher->addListener($event, $listener); } return $this; }
/** * @param callable|null $onReject * @param int $priority * @return $this */ public function onReject(callable $onReject = null, $priority = 0) { if (is_callable($onReject)) { $this->eventDispatcher->addListener(sprintf('%s.ERROR', $this->getRequestId()), $onReject, $priority); } return $this; }
public function testDeleteAllEvents() { $firedEvents = ['DataObject.beforeDelete' => 0, 'DataObject.ExtendedDataObject.beforeDelete' => 0, 'DataObject.afterDelete' => 0, 'DataObject.ExtendedDataObject.afterDelete' => 0]; $this->dispatcher->addListener('DataObject.beforeDelete', function (DataObjectEventInterface $event) use(&$firedEvents) { $firedEvents['DataObject.beforeDelete']++; }); $this->dispatcher->addListener('DataObject.ExtendedDataObject.beforeDelete', function (DataObjectEventInterface $event) use(&$firedEvents) { $firedEvents['DataObject.ExtendedDataObject.beforeDelete']++; }); $this->dispatcher->addListener('DataObject.afterDelete', function (DataObjectEventInterface $event) use(&$firedEvents) { $firedEvents['DataObject.afterDelete']++; }); $this->dispatcher->addListener('DataObject.ExtendedDataObject.afterDelete', function (DataObjectEventInterface $event) use(&$firedEvents) { $firedEvents['DataObject.ExtendedDataObject.afterDelete']++; }); $repo = $this->getRepository(); $dataObjects = []; $dataObject = new ExtendedDataObject(); $dataObjects[] = $dataObject->setId('12345'); $dataObject = new ExtendedDataObject(); $dataObjects[] = $dataObject->setId('6789'); $repo->deleteAll($dataObjects); $this->assertEquals(2, $firedEvents['DataObject.beforeDelete']); $this->assertEquals(2, $firedEvents['DataObject.ExtendedDataObject.beforeDelete']); $this->assertEquals(2, $firedEvents['DataObject.afterDelete']); $this->assertEquals(2, $firedEvents['DataObject.ExtendedDataObject.afterDelete']); }
/** * {@inheritdoc} */ public function execute($object = NULL) { $url = $this->configuration['url']; // Leave external URLs unchanged, and assemble others as absolute URLs // relative to the site's base URL. if (!UrlHelper::isExternal($url)) { $parts = UrlHelper::parse($url); // @todo '<front>' is valid input for BC reasons, may be removed by // https://www.drupal.org/node/2421941 if ($parts['path'] === '<front>') { $parts['path'] = ''; } $uri = 'base:' . $parts['path']; $options = ['query' => $parts['query'], 'fragment' => $parts['fragment'], 'absolute' => TRUE]; // Treat this as if it's user input of a path relative to the site's // base URL. $url = $this->unroutedUrlAssembler->assemble($uri, $options); } $response = new RedirectResponse($url); $listener = function ($event) use($response) { $event->setResponse($response); }; // Add the listener to the event dispatcher. $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener); }
protected function addEventListener($event, $name, $handler, $priority = 0) { if ($this->eventDispatcher === null) { throw new \RuntimeException('Cannot register templating event listeners without first setting the EventDispatcher on the TemplateManager.'); } $this->eventDispatcher->addListener("template.{$event}: {$name}", $handler, $priority); }
/** * Constructs widget plugin. * * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * Event dispatcher service. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * Entity manager service. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * Event dispatcher. * @param \Drupal\entity_browser\FieldWidgetDisplayManager $field_display_manager * Field widget display plugin manager. */ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityManagerInterface $entity_manager, EventDispatcherInterface $event_dispatcher, FieldWidgetDisplayManager $field_display_manager) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings); $this->entityManager = $entity_manager; $this->fieldDisplayManager = $field_display_manager; $event_dispatcher->addListener(Events::REGISTER_JS_CALLBACKS, [$this, 'registerJSCallback']); }
/** * @param callable $callable * @param string $eventName */ private function addListener(callable $callable, $eventName) { $this->assertLoopNotStarted(); $this->eventDispatcher->addListener($eventName, function (ProcessCompletedEvent $event) use($callable) { $callable($event->getProcess()); }); }
/** * {@inheritdoc} */ public function addEventListener($eventName, $listener, $priority = 0) { if ($this->locked) { throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.'); } $this->dispatcher->addListener($eventName, $listener, $priority); return $this; }
/** * @param EventDispatcherInterface $dispatcher * @param array $events */ public function registerListener(EventDispatcherInterface $dispatcher, array $events) { $this->checkTunnelStopped(); foreach ($events as $event) { $dispatcher->addListener($event, [$this, 'onEvent']); } array_push($this->dispatchers, $dispatcher); }