PHP code example of aura / signal

1. Go to this page and download the library: Download aura/signal 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/ */

    

aura / signal example snippets



$signal = 


$signal->handler(
    'Vendor\Package\Example',
    'example_signal',
    function ($arg) { echo $arg; }
);


namespace Vendor\Package;
use Aura\Signal\Manager as SignalManager;

class Example
{
    protected $signal;
    
    public function __construct(SignalManager $signal)
    {
        $this->signal = $signal;
    }
    
    public function doSomething($text)
    {
        echo $text;
        $this->signal->send($this, 'example_signal', $text);
    }
}


namespace Vendor\Package;
use Aura\Signal\Manager as SignalManager;

class ExampleChild extends Example
{
    public function doSomethingElse($text)
    {
        echo $text . $text . $text;
        $this->signal->send($this, 'example_signal', $text);
    }
}

class ExampleOther
{
    protected $signal;
    
    public function __construct(SignalManager $signal)
    {
        $this->signal = $signal;
    }
    
    public function doSomethingElse($text)
    {
        echo $text . $text . $text;
        $this->signal->send($this, 'example_signal', $text)
    }
}


/**
 * @var Aura\Signal\Manager $signal
 */
$object = new Vendor\Package\ExampleChild($signal);

$signal->handler(
    $object,
    'example_signal',
    function ($arg) { echo "$arg!!!";}
);


namespace Vendor\Package;
use Aura\Signal\Manager as SignalManager;

class ExampleAnotherChild extends Example
{
    public function __construct(SignalManager $signal)
    {
        parent::__construct();
        $this->signal = $signal;
        $this->signal->handler($this, 'preAction', [$this, 'preAction']);
        $this->signal->handler($this, 'postAction', [$this, 'postAction']);
    }
    
    public function action()
    {
        $this->signal->send($this, 'preAction');
        $this->doSomething( __METHOD__ );
        $this->signal->send($this, 'postAction');
    }
    
    public function preAction()
    {
        // happens before the main action() logic
    }
    
    public function postAction()
    {
        // happens after the main action() logic
    }
}


// add a closure at position 1000, which means it will be processed
// before all handlers at the default position 5000.
$closure = function() { 
    echo "Before all others."; 
    return "First closure";
};
$signal->handler('Vendor\Package\ExampleChild', 'example_signal', $closure, 1000);

// add a closure at position 9000, which means it will be processed
// after all handlers at the default position 5000.
$closure = function() { 
    echo "After all others."; 
    return "Second closure";
};
$signal->handler('Vendor\Package\ExampleChild', 'example_signal', $closure, 1000);


// send a signal
$this->signal->send($this, 'example_signal');

// get the result collection
$results =  $this->signal->getResults();

// go through each result ...
foreach ($results as $result) {
    
    // ... and echo the value returned by the Handler callback
    echo $result->value;
}


// send a signal and retain the results from each Handler
$results = $this->signal->send($this, 'example_signal');

// get the last result
$result = $results->getLast();

// and echo the value returned by the last Handler callback
echo $result->value;


// add signal handlers
$signal->handler(
    'Vendor\Package\Example',
    'mock_signal',
    function() { return 'first'; }
);

$signal->handler(
    'Vendor\Package\Example',
    'mock_signal',
    function() { return \Aura\Signal\Manager::STOP; }
);

$signal->handler(
    'Vendor\Package\Example',
    'mock_signal',
    function() { return 'third'; }
);


$results = $this->signal->send($this, 'mock_signal');
// Or you can get via 
// $results = $this->signal->getResults();


if ($results->isStopped()) {
    $result = $results->getLast();
    echo "Processing for signal 'mock_signal' stopped "
       . "by handler for " . $result->sender;
}


return [
    // first handler, with a closure
    [
        'Vendor\Package\Example',
        'mock_signal',
        function() { return 'foo'; },
    ],
    // second handler, with a static callback
    [
        'Vendor\Package\Example',
        'mock_signal',
        ['Vendor\Package\SomeClass', 'someMethod'],
    ],
    // third handler, with a closure and position
    [
        'Vendor\Package\Example',
        'mock_signal',
        function() { return 'baz'; },
        1000,
    ],
];


namespace Aura\Signal;
$handlers = ctory,
    new ResultFactory,
    new ResultCollection,
    $handlers
);