xp_complex_sig

xp_complex_sig($function[, $rebind_context = true])

Allows for performing complex signal processing using callable PHP variables.

A \Closure will be bound into an object context which allows maintaining variables across emits within $this.

Note

A \Closure can remain bound to its original context by passing $rebind_context as false.

Parameters:
  • callable – Callable variable to use for evaluation.
  • boolean – Rebind the given closures context to this object.
Return type:

\XPSPL\SIG_Complex Complex signal registered to the processor.

Example #1 Detecting a wedding.

<?php
// Once a bride, groom and bell signals are emitted we emit the wedding.
$wedding = xp_complex_sig(function($signal){
    if (!isset($this->reset) || $this->reset) {
        $this->reset = false;
        $this->bride = false;
        $this->groom = false;
        $this->bells = false;
    }
    switch (true) {
        // Signals can be compared using the compare method
        // this will return if the signals are identical
        case $signal->compare(XP_SIG('groom')):
            $this->groom = true;
            break;
        case $signal->compare(XP_SIG('bride')):
            $this->bride = true;
            break;
        case $signal->compare(XP_SIG('bells')):
            $this->bells = true;
            break;
    }
    if ($this->groom && $this->bride && $this->bells) {
        $this->reset = true;
        return true;
    }
    return false;
});

xp_signal($wedding, function(){
    echo 'A wedding just happened.';
});

xp_emit(XP_SIG('bride'));
xp_emit(XP_SIG('groom'));
xp_emit(XP_SIG('bells'));

The above code will output.

A wedding just happened.

Example #2 Detecting a wedding using network recieved signals.

<?php

xp_import('network');

$server = network\connect('0.0.0.0', ['port' => 8000]);
// Setup a server that emits a signal of recieved data
$server->on_read(function($signal){
    $read = trim($signal->socket->read());
    if ($read == null) {
      return false;
    }
    xp_emit(XP_SIG($read));
});

// Once a bride, groom and bell signals are emitted we emit the wedding.
$wedding = xp_complex_sig(function($signal){
    if (!isset($this->reset) || $this->reset) {
      $this->reset = false;
      $this->bride = false;
      $this->groom = false;
      $this->bells = false;
    }
    switch (true) {
      case $signal->compare(XP_SIG('groom')):
          $this->groom = true;
          break;
      case $signal->compare(XP_SIG('bride')):
          $this->bride = true;
          break;
      case $signal->compare(XP_SIG('bells')):
          $this->bells = true;
          break;
    }
    if ($this->groom && $this->bride && $this->bells) {
      $this->reset = true;
      return true;
    }
    return false;
});

xp_signal($wedding, function(){
    echo 'A wedding just happened.';
});

// Start the wait loop
xp_wait_loop();

The above code will output.

A wedding just happened.

Once the bride, groom and bells signals are emitted from the network connection the complex signal will emit the wedding.

Read the Docs v: v5.0.1
Versions
latest
v5.0.1
v5.0.0
v4.0.1
v4.0.0
v3.1.1
v3.1.0
v3.0.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.