1. Go to this page and download the library: Download joomla-x/event library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
joomla-x / event example snippets
namespaceMyApp;
useJoomla\Event\Event;
// Creating an Event called "onSomething".
$event = new Event('onSomething');
// Adding an argument named "foo" with value "bar".
$event->addArgument('foo', 'bar');
// Setting the "foo" argument with a new value.
$event->setArgument('foo', new \stdClass);
// Getting the "foo" argument value.
$foo = $event->getArgument('foo');
$event->stop();
namespaceMyApp;
useJoomla\Event\EventInterface;
/**
* A listener listening to content manipulation events.
*/classContentListener{
/**
* Listens to the onBeforeContentSave event.
*/publicfunctiononBeforeContentSave(EventInterface $event){
// Do something with the event, you might want to inspect its arguments.
}
/**
* Listens to the onAfterContentSave event.
*/publicfunctiononAfterContentSave(EventInterface $event){
}
}
namespaceMyApp;
useJoomla\Event\EventInterface;
$listener = function(EventInterface $event){
// Do something with the event, you might want to inspect its arguments.
};
namespaceMyApp;
useJoomla\Event\Dispatcher;
// Creating a dispatcher.
$dispatcher = new Dispatcher;
/**
* Adding the ContentListener to the Dispatcher.
* By default, it will be registered to all events matching it's method names.
* So, it will be registered to the onBeforeContentSave and onAfterContentSave events.
*/
$dispatcher->addListener(new ContentListener);
namespaceMyApp;
useJoomla\Event\Dispatcher;
useJoomla\Event\Priority;
// Of course, it shouldn't be empty.
$listener = function(EventInterface $event){
};
$dispatcher = new Dispatcher;
/**
* Adding a Closure Listener to the Dispatcher.
* You must specify the event name and the priority of the listener.
* Here, we register it for the onContentSave event with a normal Priority.
*/
$dispatcher->addListener(
$listener,
array('onContentSave' => Priority::NORMAL)
);
// Ensure the dispatcher only registers "on*" methods.
$dispatcher->setListenerFilter('^on');
namespaceMyApp;
useJoomla\Event\Dispatcher;
useJoomla\Event\Priority;
/**
* Adding the ContentListener to the Dispatcher.
* It will be registered with a high priority for the onBeforeContentSave, and
* an "Above normal" priority for the onAfterContentSave event.
*/
$dispatcher->addListener(
new ContentListener,
array(
'onBeforeContentSave' => Priority::HIGH,
'onAfterContentSave' => Priority::ABOVE_NORMAL
)
);
/**
* Here, it won't be registered to the onAfterContentSave event because
* it is not specified.
*
* If you specify a priority for an Event,
* then you must specify the priority for all Events.
*
* It is good pracctice to do so, it will avoid to register the listener
* to "useless" events and by consequence save a bit of memory.
*/
$dispatcher->addListener(
new ContentListener,
array('onBeforeContentSave' => Priority::NORMAL)
);
namespaceMyApp;
useJoomla\Event\Dispatcher;
useJoomla\Event\Event;
// Creating an event with a "foo" argument.
$event = new Event('onBeforeContentSave');
$event->setArgument('foo', 'bar');
// Registering the event to the Dispatcher.
$dispatcher = new Dispatcher;
$dispatcher->addEvent($event);
// Triggering the onAfterSomething Event.
$dispatcher->triggerEvent('onAfterSomething');
namespaceMyApp;
useJoomla\Event\Dispatcher;
useJoomla\Event\Event;
// Creating an event called "onAfterSomething" with a "foo" argument.
$event = new Event('onAfterSomething');
$event->setArgument('foo', 'bar');
$dispatcher = new Dispatcher;
// Triggering the onAfterSomething Event.
$dispatcher->triggerEvent($event);
namespaceMyApp;
useJoomla\Event\DispatcherAwareInterface;
useJoomla\Event\DispatcherInterface;
useJoomla\Event\Event;
classContentModelimplementsDispatcherAwareInterface{
const ON_BEFORE_SAVE_EVENT = 'onBeforeSaveEvent';
const ON_AFTER_SAVE_EVENT = 'onAfterSaveEvent';
/**
* The underlying dispatcher.
*
* @var DispatcherInterface
*/protected $dispatcher;
publicfunctionsave(){
$this->dispatcher->triggerEvent(self::ON_BEFORE_SAVE_EVENT);
// Perform the saving.$this->dispatcher->triggerEvent(self::ON_AFTER_SAVE_EVENT);
}
/**
* Set the dispatcher to use.
*
* @param DispatcherInterface $dispatcher The dispatcher to use.
*
* @return DispatcherAwareInterface This method is chainable.
*/publicfunctionsetDispatcher(DispatcherInterface $dispatcher){
$this->dispatcher = $dispatcher;
}
}
namespaceMyApp;
useJoomla\Event\EventImmutable;
// Creating an immutable event called onSomething with an argument "foo" with value "bar"
$event = new EventImmutable('onSomething', array('foo' => 'bar'));
namespaceMyApp;
useJoomla\Event\DelegatingDispatcher;
useJoomla\Event\Dispatcher;
$dispatcher = new Dispatcher;
// Here you add you listeners and your events....// Instanciating a delegating dispatcher.
$delegatingDispatcher = new DelegatingDispatcher($dispatcher);
// Now you inject this dispatcher in your system, and it has only the triggerEvent method.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.