/**
  * Test the triggerEvent method with a previously registered event.
  *
  * @return  void
  *
  * @covers  Joomla\Event\Dispatcher::triggerEvent
  * @since   1.0
  */
 public function testTriggerEventRegistered()
 {
     $event = new Event('onSomething');
     $mockedListener = $this->getMock('Joomla\\Event\\Test\\Stubs\\SomethingListener', array('onSomething'));
     $mockedListener->expects($this->once())->method('onSomething')->with($event);
     $this->instance->addEvent($event);
     $this->instance->addListener($mockedListener);
     $this->instance->triggerEvent('onSomething');
 }
 /**
  * Test the removeSubscriber method.
  *
  * @return  void
  */
 public function testRemoveSubscriber()
 {
     $listener = new SomethingListener();
     // Add our event subscriber
     $this->instance->addSubscriber($listener);
     // And now remove it
     $this->instance->removeSubscriber($listener);
     $this->assertFalse($this->instance->hasListener([$listener, 'onBeforeSomething']));
     $this->assertFalse($this->instance->hasListener([$listener, 'onSomething']));
     $this->assertFalse($this->instance->hasListener([$listener, 'onAfterSomething']));
 }
 /**
  * Fire onContentPrepare for content that isn't part of an article.
  *
  * @param   string  $text     The content to be transformed.
  * @param   array   $params   The content params.
  * @param   string  $context  The context of the content to be transformed.
  *
  * @return  string   The content after transformation.
  *
  * @since   11.1
  */
 public static function prepare($text, $params = null, $context = 'text')
 {
     if ($params === null) {
         $params = new Object();
     }
     $article = new stdClass();
     $article->text = $text;
     Helper::importPlugin('content');
     $dispatcher = Dispatcher::getInstance();
     $dispatcher->trigger('onContentPrepare', array($context, &$article, &$params, 0));
     return $article->text;
 }
 /**
  * Call the extensions
  *
  * @param   EventInterface $event The event
  *
  * @return  EventInterface
  */
 public function dispatch(EventInterface $event)
 {
     $this->logger->debug(__METHOD__ . ": Dispatching " . $event->getName());
     $name = $event->getName();
     if (!key_exists($name, $this->loadedEvents)) {
         $this->logger->debug(__METHOD__ . ": - Loading extension listeners");
         $this->loadExtensionListeners($name, $this->factory->getExtensions());
         $this->loadedEvents[$name] = $name;
     }
     $result = parent::dispatch($event);
     $this->logger->debug(__METHOD__ . ": Done.");
     return $result;
 }
 /**
  * Triggers an event if a listener is set
  *
  * @param   string                 $eventName  Name of the event to trigger
  * @param   AbstractDatabaseTable  $table      Table object
  * @param   array                  $optional   Associative array of optional arguments for the event
  *
  * @return  void
  *
  * @since   1.0
  */
 protected function triggerEvent($eventName, AbstractDatabaseTable $table, array $optional = array())
 {
     if ($this->listenerSet) {
         $event = new Event($eventName);
         // Add the event params
         $event->addArgument('hookData', $this->hookData)->addArgument('table', $table)->addArgument('github', $this->github)->addArgument('logger', $this->logger)->addArgument('project', $this->project);
         // Add optional params if present
         if (count($optional) > 0) {
             foreach ($optional as $name => $value) {
                 $event->addArgument($name, $value);
             }
         }
         // Trigger the event
         $this->dispatcher->triggerEvent($event);
     }
 }
 /**
  * Triggers an event if a listener is set.
  *
  * @param   string  $eventName  Name of the event to trigger.
  * @param   array   $arguments  Associative array of arguments for the event.
  *
  * @return  void
  *
  * @since   1.0
  */
 protected function triggerEvent($eventName, array $arguments)
 {
     if (!$this->listenerSet) {
         return;
     }
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $event = new Event($eventName);
     // Add default event arguments.
     $event->addArgument('github', $this->github ?: GithubFactory::getInstance($application))->addArgument('logger', $application->getLogger())->addArgument('project', $application->getProject());
     // Add event arguments passed as parameters.
     foreach ($arguments as $name => $value) {
         $event->addArgument($name, $value);
     }
     // Trigger the event.
     $this->dispatcher->triggerEvent($event);
 }
 /**
  * Allows the application to load a custom or default dispatcher.
  *
  * The logic and options for creating this object are adequately generic for default cases
  * but for many applications it will make sense to override this method and create event
  * dispatchers, if required, based on more specific needs.
  *
  * @param   JEventDispatcher  $dispatcher  An optional dispatcher object. If omitted, the factory dispatcher is created.
  *
  * @return  JApplicationBase This method is chainable.
  *
  * @since   12.1
  */
 public function loadDispatcher(Dispatcher $dispatcher = null)
 {
     $this->dispatcher = $dispatcher === null ? Dispatcher::getInstance() : $dispatcher;
     return $this;
 }
Example #8
0
 /**
  * Start a session.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function start()
 {
     if ($this->state === 'active') {
         return;
     }
     $this->_start();
     $this->state = 'active';
     // Initialise the session
     $this->_setCounter();
     $this->_setTimers();
     // Perform security checks
     $this->_validate();
     if ($this->dispatcher instanceof Dispatcher) {
         $this->dispatcher->trigger('onAfterSessionStart');
     }
 }
 /**
  * Authorises that a particular user should be able to login
  *
  * @param   AuthenticationResponse  $response  response including username of the user to authorise
  * @param   array                   $options   list of options
  *
  * @return  array[AuthenticationResponse]  results of authorisation
  *
  * @since  11.2
  */
 public static function authorise($response, $options = array())
 {
     // Get plugins in case they haven't been imported already
     Helper::importPlugin('user');
     Helper::importPlugin('authentication');
     $dispatcher = Dispatcher::getInstance();
     $results = $dispatcher->trigger('onUserAuthorisation', array($response, $options));
     return $results;
 }
 /**
  * Method to delete the JUser object from the database
  *
  * @return  boolean  True on success
  *
  * @since   11.1
  */
 public function delete()
 {
     PluginHelper::importPlugin('user');
     // Trigger the onUserBeforeDelete event
     $dispatcher = Dispatcher::getInstance();
     $dispatcher->trigger('onUserBeforeDelete', array($this->getProperties()));
     // Create the user table object
     $table = $this->getTable();
     $result = false;
     if (!($result = $table->delete($this->id))) {
         $this->setError($table->getError());
     }
     // Trigger the onUserAfterDelete event
     $dispatcher->trigger('onUserAfterDelete', array($this->getProperties(), $result, $this->getError()));
     return $result;
 }
 /**
  * Gets the user profile information
  *
  * @param   integer  $userId  The id of the user.
  *
  * @return  object
  *
  * @since   11.1
  */
 public static function getProfile($userId = 0)
 {
     if ($userId == 0) {
         $user = Factory::getUser();
         $userId = $user->id;
     }
     // Get the dispatcher and load the user's plugins.
     $dispatcher = Dispatcher::getInstance();
     Helper::importPlugin('user');
     $data = new Object();
     $data->id = $userId;
     // Trigger the data preparation event.
     $dispatcher->trigger('onContentPrepareData', array('com_users.profile', &$data));
     return $data;
 }
 /**
  * Loads the plugin file.
  *
  * @param   object      $plugin      The plugin.
  * @param   boolean     $autocreate  True to autocreate.
  * @param   Dispatcher  $dispatcher  Optionally allows the plugin to use a different dispatcher.
  *
  * @return  void
  *
  * @since   11.1
  */
 protected static function _import($plugin, $autocreate = true, Dispatcher $dispatcher = null)
 {
     static $paths = array();
     $plugin->type = preg_replace('/[^A-Z0-9_\\.-]/i', '', $plugin->type);
     $plugin->name = preg_replace('/[^A-Z0-9_\\.-]/i', '', $plugin->name);
     $path = JPATH_PLUGINS . '/' . $plugin->type . '/' . $plugin->name . '/' . $plugin->name . '.php';
     if (!isset($paths[$path])) {
         if (file_exists($path)) {
             if (!isset($paths[$path])) {
                 require_once $path;
             }
             $paths[$path] = true;
             if ($autocreate) {
                 // Makes sure we have an event dispatcher
                 if (!is_object($dispatcher)) {
                     $dispatcher = Dispatcher::getInstance();
                 }
                 $className = 'plg' . $plugin->type . $plugin->name;
                 if (class_exists($className)) {
                     // Load the plugin from the database.
                     if (!isset($plugin->params)) {
                         // Seems like this could just go bye bye completely
                         $plugin = self::getPlugin($plugin->type, $plugin->name);
                     }
                     // Instantiate and register the plugin.
                     new $className($dispatcher, (array) $plugin);
                 }
             }
         } else {
             $paths[$path] = false;
         }
     }
 }