Download the PHP package fluffy/connector without Composer
On this page you can find all versions of the php package fluffy/connector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fluffy/connector
More information about fluffy/connector
Files in fluffy/connector
Package connector
Short Description Provides 'Signals and slots' pattern
License GPLv3
Homepage https://github.com/pavelfluffy/connector
Informations about the package connector
Connector - "Signals and slots" mechanism for PHP.
This library is inspired by Qt "Signals and slots" mechanism so it's pretty similar.
Signals and slots
Signal is something that tell outer world about the internal state of an object. For example: phones can ring, cats can meow etc. Rings and meows are signals. They tell us about changing in their internal states.
You can react on that signals in some way. For example: answer the call or feed the cat. Your reactions are slots.
There are tha same situations in programming: sometimes we need to react on some changes in object's state. And that's what this library for: it makes communication between objects easier. Even easier than it could be with "Observer" pattern.
Installation
Run
or add dependency to your composer.json file
Usage
1. Signals
If you want your object will be able to emit signals you need to implement SignalInterface
and use SignalTrait
. For example you have some logger class and you want to emit signal somethingIsLogged
when logger finished work:
To emit signal you need to call emit
method and pass signal name and data. You can pass whatever you want: array, string, object or number. That's all. Now your logger emits signal to outer world. But nobody is connected to this signal yet. Let do this stuff.
2. Slots
Slot it's a usual class method. Let's define a class with a slot.
3. Connections
For now we have Logger
class that emits signal and Receiver
class with a slot. To react on signal with slot you need to connect them to each other. Let's do it.
Since you called ConnectionManager::connect(SignalInterface $sender, $signalName, $receiver, $slotName);
method signal and slot are connected. It means that after call $logger->log()
the somethingIsLogged
signal will be emitted and the slotReactOnSignal
slot will be called. Result will be "Received data: Some useful data"
. You can connect as many slots to signals as you want. Actually you can create connections like:
- One signal to one slot
- One signal to many slots
- Many signals to many slots
- Many signals to one slot
You can also establish multiple connections by calling ConnectionManager::initConnections(array $connections);
method:
3.1. Connection types
By default ConnectionManager::connect()
method creates permanent connections. It means that slot will not be disconnected from signal after first emission. But you can make one-time connection. Just pass 5th parameter to ConnectionManager::connect()
method as ConnectionManager::CONNECTION_ONE_TIME
. For example:
After second call of Logger::log()
nothing will happen because slot will be disconnected from signal after first emission.
3.2. Connection weights
Since you can connect different receivers with different slots to one sender and signal ConnectionManager
calls connected slots in a special order - by connections weights. Lower weight has higher priority. Connections weights affect on order of slots from all receivers but not on order of slots inside one receiver.
By default ConnectionManager::connect()
method creates permanent connections with zero weight. But you can change it by passing 6th parameter to ConnectionManager::connect()
method. For example:
4. Disconnect
If you don't want to listen signal anymore just disconnect from it.
You can also disconnect connections by conditions:
-
Disconnect all receivers from all signals for a given sender.
-
Disconnect all receivers from a given signal of a given sender.
- Disconnect all slots from a given receiver from a given signal of a given sender.
If you want to reset all existing connections call
5. Services connections
If you are using Symfony Dependency Injection
component you might don't want to create objects manually but retrieve them from service container instead. For such cases you can connect your services defined in services.yml
file without any manual object creation. That's how you can achieve this:
-
Let's say you have
services.yml
file with next services: -
In order to connect a
\Receiver
slotslotReactOnSignal
to a\Logger
signalsomethingIsLogged
create a yaml file somewhere (let's sayservices.connections.yml
) in you project with next content: -
Initialize service connections:
- Now yor
\Logger
service is ready to emit signals and\Receiver
service is ready to react on signals:
Tests
Please see tests for more information and use-cases.
In order to run tests type:
$ composer install
$ ./vendor/bin/phpunit
What for?
I just like Qt's signal and slot system and want to bring it into PHP world.
Any advantages?
- It's lightweight.
- Depends only on one third party library:
symfony/yaml
License
GPLv3. See LICENSE file.