/src/SkynetUser/
It's called:
SkynetEventListenerMyListener.php
When you open this file you will see empty methods for each event calling by Skynet.
This empty event listener is registered in Skynet by default, so if you place here some code it will be executed when Skynet starts.
To registering event listeners Skynet serves a factory class:
/src/Skynet/EventListener/SkynetEventListenersFactory.php
In this factory you will find all listeners assigned to Skynet:
private function registerEventListeners() { $this->register('clusters', new SkynetEventListenerClusters()); $this->register('cloner', new SkynetEventListenerCloner()); $this->register('console', new SkynetEventListenerConsole()); $this->register('options', new SkynetEventListenerOptions()); $this->register('registry', new SkynetEventListenerRegistry()); $this->register('my', new \SkynetUser\SkynetEventListenerMyListener()); $this->register('echo', new SkynetEventListenerEcho()); $this->register('sleeper', new SkynetEventListenerSleeper()); $this->register('updater', new SkynetEventListenerUpdater()); }
As you see - your empty listener template is already registered as "my".
If you will create another listeners you would need to register them here if you want to include them in Skynet.
Notice that user defined listeners should be exists in namespace:
\SkynetUser
and should be placed into directory:
/src/SkynetUser/
This is for update and compile purposes.
When you will updating Skynet's core (e.g. by newest version) your custom classed will be still here in custom folder and after compiling new Skynet's core + your listeners will be included into new version of package.
OK. Let's write some code.
All events whitch you can use are described in Event Listeners sections.
Let's check how to use them and what API methods you can use in your custom listener.
Request and response access
In every event listener you have fully access to request and response objects:
$this->request
and
$this->response
Setting and getting data from this data are served by set() and get() methods.
Take a look at example:
public function onResponse($context) { if($context == "beforeSend") { if($this->request->get('foo') == 'bar') { $this->response->set('bar', 'foo'); } } }
That is the first method.
To get fields from both of this objects you can also use arrays:
$this->requestsData[]
and
$this->responseData[]
Example:
public function onResponse($context) { if($context == "beforeSend") { if(isset($this->requestsData['foo']) && $this->requestsData['foo'] == 'bar') { if(!isset($this->responseData['bar'])) { $this->response->set('bar', 'foo'); } } } }
Notice that arrays with fields are read-only and you can only getting data with this method.
If you will change any data in $responseData[], your operation will not be included in response - to do this you must use set() method.
Inheriting
Every Event Listener must:
- extends \Skynet\EventListener\SkynetEventListenerAbstract class
- implements \Skynet\EventListener\SkynetEventListenerInterface interface
After creating your listener you should launch Skynet compiler to compile it into cluster file.
When you creating much more complicated files structure of your own classes (extending, implementing, etc.) remember that abstract classes files shout be named as:
ClassNameAbstract
Interfaces should be named as:
ClassNameInterface
Traits should be named:
ClassNameTrait
This is for compilation reason - files with abstract classes, interfaces and trait are compiling at the beginning of compiled file (when you inherits from non-existing yet class PHP generates error), so remember of this.
You can create files structure as you want - it will all be compiled into standalone file.
Notice: when compiling - all use (excepts traits use in class definition) and namespace directives will be erased.
API Methods available under Event Listener are described in next section.