Signals and slots is an approach used in some programming languages and libraries (for example, Boost and Qt) which allows for the implementation of the “observer” pattern, minimizing the writing of repetitive code. The concept is that a component (often a widget) can send signals containing information about an event (for example: the text “word” was highlighted, the second tab was opened). In turn, other components can receive these signals through special functions - slots. A signal and slot system is well suited for describing a graphical user interface. Also, the signal / slot mechanism can be applied to asynchronous I / O (including sockets, pipe, devices with a serial interface, etc.) or event notifications. In the Qt library, thanks to the Meta-Object Compiler (English) Russian. there is no need to write a registration / deregistration / call code, since these sample code sections are generated automatically.Wikipedia tells us.
composer require fluffy/connector
"require": { ... "fluffy/connector": "^1.0" }
composer update
SignalInterface
interface and use the corresponding SignalTrait SignalTrait
. For example, we have a logger class and we want it to send a signal somethingIsLogged
whenever it finishes logging: <?php /** * @file * Contains definition of Logger class. */ use Fluffy\Connector\Signal\SignalInterface; use Fluffy\Connector\Signal\SignalTrait; /** * Class Logger. */ class Logger implements SignalInterface { use SignalTrait; public function log() { // Do logging stuff. ... // Emit signal about successfull logging. $this->emit('somethingIsLogged', 'Some useful data'); } }
emit
method and pass two parameters: the name of the signal and the data that will be transmitted by this signal. You can transfer any type of data: string, number, array, or object. It's all. Now the object logger is able to send signals to the outside world. But as long as no one is connected to this signal, no one will know that this object reports something useful. Let's fix it. <?php /** * @file * Contains definition of Receiver class. */ /** * Class Receiver. */ class Receiver { public function slotReactOnSignal($dataFromSignal) { echo "Received data: $dataFromSignal"; } }
Logger
class with a signal and a Receiver
class with a slot. In order to respond to a signal, you need to connect a slot to it. use Fluffy\Connector\ConnectionManager; $logger = new Logger(); $receiver = new Receiver(); ConnectionManager::connect($logger, 'somethingIsLogged', $receiver, 'slotReactOnSignal'); $logger->log();
ConnectionManager::connect($sender, $signalName, $receiver, $slotName)
we thereby connected the signal of the logger object to the slot of the receiver object. This means that every time $logger->log()
is called, a $logger->log()
signal somethingIsLogged
will be sent, to which the slotReactOnSignal
slot of the slotReactOnSignal
object will respond. We can define as many signal slot connections as we need. All possible types of connections work:ConnectionManager::connect()
method creates a permanent connection. This means that the connection will not be broken after the first signal is sent. However, it is possible to create a one-time connection by passing a constant to the fifth parameter — the type of connection. For example: use Fluffy\Connector\ConnectionManager; $logger = new Logger(); $receiver = new Receiver(); ConnectionManager::connect($logger, 'somethingIsLogged', $receiver, 'slotReactOnSignal', ConnectionManager::CONNECTION_ONE_TIME); $logger->log(); // Log once again. $logger->log();
Logger::log()
nothing will happen as the slot will be disconnected from the signal after its first expulsion. ConnectionManager::disconnect($logger, 'somethingIsLogged', $receiver, 'slotReactOnSignal');
ConnectionManager::resetAllConnections()
Source: https://habr.com/ru/post/304988/
All Articles