PHP code example of fluffy / connector

1. Go to this page and download the library: Download fluffy/connector library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fluffy / connector example snippets




/**
 * @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');
    }
}



/**
 * @file
 * Contains definition of Receiver class.
 */

/**
 * Class Receiver.
 */
class Receiver
{

  public function slotReactOnSignal($dataFromSignal) {
    echo "Received data: $dataFromSignal";
  }

}


use Fluffy\Connector\ConnectionManager;

$logger = new Logger();
$receiver = new Receiver();

ConnectionManager::connect($logger, 'somethingIsLogged', $receiver, 'slotReactOnSignal');

$logger->log();

ConnectionManager::initConnections([
  ...
  [
    'sender' => new Logger(),
    'signal' => 'somethingIsLogged',
    'receiver' => new Receiver(),
    'slot' => 'slotReactOnSignal',
    'type' => ConnectionManager::CONNECTION_PERMANENT,
  ],
  ...
]);

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();

use Fluffy\Connector\ConnectionManager;

$logger = new Logger();
$receiver = new Receiver();

// This connection has weight 0 by default.
ConnectionManager::connect($logger, 'somethingIsLogged', $receiver, 'slotReactOnSignal');

// And this has weight -10.
ConnectionManager::connect($logger, 'somethingIsLogged', $receiver, 'anotherSlotReactOnSignal', ConnectionManager::CONNECTION_ONE_TIME, -10);

// Signal 'somethingIsLogged' is emitted here. Since we've explicitly defined
// connections weights `ConnectionManager` will call slots in the next order:
// 1. Slot 'anotherSlotReactOnSignal'.
// 2. Slot 'slotReactOnSignal'.
$logger->log();

ConnectionManager::disconnect($logger, 'somethingIsLogged', $receiver, 'slotReactOnSignal');

ConnectionManager::disconnect($logger);

ConnectionManager::disconnect($logger, 'somethingIsLogged');

ConnectionManager::disconnect($logger, 'somethingIsLogged', $receiver);

ConnectionManager::resetAllConnections()

services:
  service.logger:
    class: \Logger
    arguments: [...]
  service.receiver:
    class: \Receiver
    arguments: [...]

// Here you need to pass a yaml string from file and a service container.
// This should be done once somewhere in a front controller of your
// application.
$serviceConnections = ConnectionManager::parseServicesConnections(file_get_contents('services.connections.yml'), $container);
ConnectionManager::initConnections($serviceConnections);

// Receiver will respond to signal "somethingIsLogged" with a slot defined in "services.connections.yml".
$container->get('service.logger')->emit('somethingIsLogged', 'Signal data');