1. Go to this page and download the library: Download jgswift/observr 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/ */
jgswift / observr example snippets
class User implements observr\SubjectInterface
{
use observr\Subject;
}
$user = new User;
$user->attach("login",function($sender,$e) {
return "login successful";
});
var_dump($user->setState("login")); // returns "login successful"
$user = new User;
$user->attach("login",function($sender,$e) {
$e->cancel(); // manual cancellation
});
$event = new observr\Event($user);
$user->setState("login",$event)
var_dump($event->isCanceled()); // returns true
interface EventInterface {
public function isComplete();
public function isSuccess();
public function isFailure();
public function isCanceled();
public function getException();
public function setException(\Exception $exception);
}
interface EventAwareInterface {
public function cancel(EventInterface $event = null);
public function complete(EventInterface $event);
public function fail(EventInterface $event);
public function succeed(EventInterface $event);
}
class Button implements observr\SubjectInterface {
use observr\Subject;
}
$button = new Button;
$click = new observr\Emitter('click');
$button->setState($click, function($sender,$e) {
echo 'CLICKED!';
});
$click($button); // prints 'CLICKED'!
interface EmitterInterface extends SubjectInterface {
public function getName();
public function emit($e = null);
public function on(callable $callable);
public function bind(callable $callable);
public function unbind(callable $callable);
public function map(callable $callable);
public function filter(callable $callable);
public function __toString();
}
class Button {
function __construct() {
$this->click = new observr\Emitter('click');
$this->mouseup = new observr\Emitter('mouseup');
$this->mousedown = new observr\Emitter('mousedown');
}
}
$button = new Button;
$combinedClick = $button->click->map(function($sender, $e) {
/* extra mapping */
})->merge($button->mousedown->map(function($sender,$e) {
/* extra mapping */
}))->merge($button->mouseup->map(function($sender,$e) {
/* extra mapping */
}));
$combinedClick($button); // performs click, mousedown & mouseup all together
$sending = $button->click
->filter(function($button,$e) {
if($button instanceof Button) { // only changes Button to "Sending..."
return true;
}
return false;
})->map(function($button,$e) {
$button->value = 'Sending...';
});
$sending($button); // triggers click and changes button text to "Sending..."
$bob = new User
$john = new User;
$stream = new observr\Stream('login');
// instruct stream to watch our user login events
$stream->watch($bob);
$stream->watch($john);
$c = 0;
$stream->attach(function($sender,$e=null)use(&$c) {
$c++; // called twice, this is where we intercept the event
});
// open stream
$stream->open();
// trigger some fake logins
$bob->setState('login');
$john->setState('login');
// close stream
$stream->close();
var_dump($c); // 2
interface StreamInterface {
public function close();
public function getSubjects();
public function isOpen();
public function open();
public function watch($pointer);
public function unwatch($pointer);
public function isWatching($pointer);
}
sh
php composer.phar
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.