1. Go to this page and download the library: Download ed-fruty/strange-observer 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/ */
ed-fruty / strange-observer example snippets
use Fruty\Observe\Manager;
use Fruty\Observe\Event;
class User extends SomeClass
{
/**
* @access public
* @param array $attributes
* @return bool
*/
public function register(array $attributes)
{
return $this->save($attributes);
}
}
// create instance
$user = Manager::make('User');
// or we can use instance of User extend of class name
$instance = new User();
$user = Manager::make($instance);
$user->register(array('username' => 'root'));
$user()->before('register', function(Event $event)
{
// validate data
// get method arguments
$params = $event->getParams();
//... some validation logic
// for abort calling register method (if validation is failed) use
$event->stopPropagation();
// for setting some return value
$event->setResult("Validation errors");
});
$user()->after('register', function(Event $event)
{
if ($event->getResult()) {
// in $event->getResult() result of executed method, in our case it is boolean
// and if it true - registration was successfull, so send email
$email = $event->getParams()[0]['email'];
$mailer = new Mailer();
$mailer->body("Congratulations!")
->to($email)
->send();
}
});
$user()->before('register', function(Event $event)
{
// this code will be executed early than validation
// because it have higher priority (Event::PRIORITY_HIGH)
}, Event::PRIORITY_HIGH);
$user()->bind('getOne', function($userId)
{
//$this is instance of Invoker, methods will remap to the instance of User and they can be listened
//$this(true) is instance of User, methods cannot be listened
// NOTE!!!
// if you write here $this->getOne() script will call this action again and again, so you must to use
// $this(true)->getOne() to use real User::getOne()
// if you call here
$this->register(); // it will be called and listened
$this(true)->register(); // it will be called but not listened, this is original User::register()
$this->updateLastLoginTime(); // it will be called and listened
$this(true)->updateLastLoginTime(); // Fatal error, calling undefined method User::updateLastLoginTime()
return array('some data');
});
use Fruty\Observe\Event;
use Fruty\Observe\AbstractSubscriber;
class UserSubscriber extends AbstractSubscriber
{
/**
* This method will call before calling User::register()
*
* @access public
* @static
* @param \Fruty\Observe\Event $event
*/
public static function onBeforeRegister(Event $event)
{
// here code
}
/**
* This method will call after calling User::register()
*
* @access public
* @static
* @param \Fruty\Observe\Event $event
*/
public static function onAfterRegister(Event $event)
{
// here code
}
/**
* We can set priorities to the actions
*
* @access public
* @static
* @return array
*/
public function getPriorities()
{
return array(
'onBeforeRegister' => Event::PRIORITY_HIGHT,
'onAfterRegiter' => Event::PRIORITY_LOW,
);
}
}
// register subscriber
$user()->subscribe('UserSubscriber');
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.