Skip to content

Commit

Permalink
Merge pull request #1 from inhere/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
inhere authored Dec 15, 2017
2 parents 73c0ed0 + f231764 commit ad52cd9
Show file tree
Hide file tree
Showing 21 changed files with 876 additions and 520 deletions.
311 changes: 238 additions & 73 deletions README.md

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions examples/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Inhere\Event\Examples;

use Inhere\Event\Event;
use Inhere\Event\EventAwareTrait;
use Inhere\Event\EventManagerAwareTrait;
use Inhere\Event\EventManager;

/**
Expand All @@ -23,7 +23,7 @@ class App
const ON_BEFORE_REQUEST = 'app.beforeRequest';
const ON_AFTER_REQUEST = 'app.afterRequest';

use EventAwareTrait;
use EventManagerAwareTrait;

public function __construct(EventManager $em)
{
Expand All @@ -36,18 +36,15 @@ public function __construct(EventManager $em)

public function run()
{
$this->eventManager->trigger(self::ON_BEFORE_REQUEST, new Event('beforeRequest'));

$sleep = 0;
$this->eventManager->trigger(self::ON_BEFORE_REQUEST, new Event('beforeRequest'));

echo 'handling ';

while ($sleep <= 5) {
echo 'request handling ';
while ($sleep <= 3) {
$sleep++;
echo '.';
sleep(1);
}

echo "\n";

$this->eventManager->trigger(self::ON_AFTER_REQUEST, new Event('afterRequest'));
Expand All @@ -59,4 +56,4 @@ public function __destruct()
'key1' => 'val1'
]));
}
}
}
14 changes: 10 additions & 4 deletions examples/AppListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,30 @@ class AppListener
public function start(EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos\n";
echo "handle the event '{$event->getName()}' on the: $pos\n";
}

public function beforeRequest(EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos\n";
echo "handle the event '{$event->getName()}' on the: $pos\n";
}

public function afterRequest(EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos\n";
echo "handle the event '{$event->getName()}' on the: $pos\n";
}

public function stop(EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos\n";
echo "handle the event '{$event->getName()}' on the: $pos\n";
}

public function allEvent(EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event '{$event->getName()}' on the: $pos\n";
}
}
48 changes: 48 additions & 0 deletions examples/EnumGroupListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Created by PhpStorm.
* User: Inhere
* Date: 2017/12/14 0014
* Time: 22:25
*/

namespace Inhere\Event\Examples;


use Inhere\Event\EventInterface;
use Inhere\Event\EventSubscriberInterface;
use Inhere\Event\ListenerPriority;

/**
* Class EnumGroupListener
* @package Inhere\Event\Examples
*/
class EnumGroupListener implements EventSubscriberInterface
{
const TEST_EVENT = 'test';
const POST_EVENT = 'post';

/**
* 配置事件与对应的处理方法
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
self::TEST_EVENT => 'onTest',
self::POST_EVENT => ['onPost', ListenerPriority::LOW],
];
}

public function onTest(EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos\n";
}

public function onPost(EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos\n";
}
}
6 changes: 3 additions & 3 deletions examples/ExamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
namespace Inhere\Event\Examples;

use Inhere\Event\EventInterface;
use Inhere\Event\HandlerInterface;
use Inhere\Event\EventHandlerInterface;

/**
* Class SingleListener
* @package Inhere\Event
*/
class ExamHandler implements HandlerInterface
class ExamHandler implements EventHandlerInterface
{
/**
* @param EventInterface $event
Expand All @@ -24,7 +24,7 @@ class ExamHandler implements HandlerInterface
public function handle(EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos\n";
echo "handle the event '{$event->getName()}' on the: $pos\n";

return true;
}
Expand Down
29 changes: 17 additions & 12 deletions examples/exam.php → examples/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


use Inhere\Event\Event;
use Inhere\Event\EventAwareTrait;
use Inhere\Event\EventManagerAwareTrait;
use Inhere\Event\EventInterface;
use Inhere\Event\EventManager;
use Inhere\Event\Examples\ExamHandler;
Expand All @@ -12,7 +12,7 @@
function exam_handler(EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos \n";
echo "handle the event '{$event->getName()}' on the: $pos \n";
}

class ExamListener1
Expand All @@ -21,60 +21,65 @@ public function messageSent(EventInterface $event)
{
$pos = __METHOD__;

echo "handle the event {$event->getName()} on the: $pos \n";
echo "handle the event '{$event->getName()}' on the: $pos \n";
}
}

class ExamListener2
{
public function __invoke(EventInterface $event)
{
// $event->stopPropagation(true);
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos\n";
echo "handle the event '{$event->getName()}' on the: $pos\n";
}
}

// create event class
class MessageEvent extends Event
{
protected $name = 'messageSent';

// append property ...
public $message = 'oo a text';
}

class Mailer
{
use EventAwareTrait;
use EventManagerAwareTrait;

const EVENT_MESSAGE_SENT = 'messageSent';

public function send($message)
{
// ...发送 $message 的逻辑...

$event = new MessageEvent;
$event = new MessageEvent(self::EVENT_MESSAGE_SENT);
$event->message = $message;

// trigger event
$this->eventManager->trigger(self::EVENT_MESSAGE_SENT, $event);
$this->eventManager->trigger($event);

// var_dump($event);
}
}

$em = new EventManager();

$em->attach(Mailer::EVENT_MESSAGE_SENT, 'exam_handler');
$em->attach(Mailer::EVENT_MESSAGE_SENT, function (EventInterface $event)
{
$pos = __METHOD__;
echo "handle the event {$event->getName()} on the: $pos\n";
echo "handle the event '{$event->getName()}' on the: $pos\n";
});
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamListener1());
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamListener1(), 10);
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamListener2());
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamHandler());

$em->attach('*', function (EventInterface $event)
{
echo "handle the event '{$event->getName()}' on the global listener.\n";
});

$mailer = new Mailer();
$mailer->setEventManager($em);

$mailer->send('hello, world!');
$mailer->send('hello, world!');
36 changes: 36 additions & 0 deletions examples/enum-group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-12-07
* Time: 14:07
*/

use Inhere\Event\EventManager;
use Inhere\Event\Examples\EnumGroupListener;

require dirname(__DIR__) . '/tests/boot.php';

$em = new EventManager();

// register a group listener
$em->addListener(new EnumGroupListener());

$demo = new class
{
use \Inhere\Event\EventManagerAwareTrait;

public function run()
{
$this->eventManager->trigger(EnumGroupListener::TEST_EVENT);

echo '.';
sleep(1);
echo ".\n";

$this->eventManager->trigger(EnumGroupListener::POST_EVENT);
}
};

$demo->setEventManager($em);
$demo->run();
7 changes: 6 additions & 1 deletion examples/group.php → examples/named-group.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@

$em = new EventManager();

$groupListener = new AppListener();

// register a group listener
$em->attach('app', new AppListener());
$em->attach('app', $groupListener);

// all `app.` prefix events will be handled by `AppListener::allEvent()`
$em->attach('app.*', [$groupListener, 'allEvent']);

// create app
$app = new App($em);
Expand Down
4 changes: 2 additions & 2 deletions src/ClassEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function on($class, $event, callable $handler)

/**
* trigger event
* @param $event
* @param string $event
* @param array $args
* @return bool
*/
Expand All @@ -57,7 +57,7 @@ public static function fire($event, array $args = [])
// call event handlers of the event.
foreach ((array)self::$events[$event] as $cb) {
// return FALSE to stop go on handle.
if (false === \call_user_func_array($cb, $args)) {
if (false === $cb(...$args)) {
break;
}
}
Expand Down
31 changes: 11 additions & 20 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,21 @@
* User: inhere
* Date: 16/8/27
* Time: 下午12:34
* reference windwalker https://github.com/ventoviro/windwalker
* @link windwalker https://github.com/ventoviro/windwalker
*/

namespace Inhere\Event;

//use Inhere\Library\StdObject;

/**
* Class Event
* @package Inhere\LibraryPlus\Event
* @package Inhere\Event
*/
class Event implements EventInterface, \ArrayAccess, \Serializable
{
/**
* @var string 当前的事件名称
*/
/** @var string Event name */
protected $name;

/**
* 参数
* @var array
*/
/** @var array Event params */
protected $params = [];

/**
Expand All @@ -50,7 +43,9 @@ public function __construct($name = null, array $params = [])
$this->setName($name);
}

$this->params = $params;
if ($params) {
$this->params = $params;
}
}

/**
Expand All @@ -60,14 +55,10 @@ public function __construct($name = null, array $params = [])
*/
public static function checkName(string $name)
{
$name = trim($name);

if (!$name || \strlen($name) > 50) {
throw new \InvalidArgumentException('Set up the name can be a not empty string of not more than 50 characters!');
}
$name = trim($name, '. ');

if (!preg_match('/^\w[\w-.]{1,56}$/i', $name)) {
throw new \InvalidArgumentException("The service Id[$name] is invalid string");
if (!$name || \strlen($name) > 64) {
throw new \InvalidArgumentException('Set up the name can be a not empty string of not more than 64 characters!');
}

return $name;
Expand All @@ -91,7 +82,7 @@ public function setName($name)
}

/**
* set all param
* set all params
* @param array $params
*/
public function setParams(array $params)
Expand Down
Loading

0 comments on commit ad52cd9

Please sign in to comment.