Skip to content

Commit

Permalink
重构了事件系统
Browse files Browse the repository at this point in the history
  • Loading branch information
thenbsp committed Feb 26, 2016
1 parent f52ad0b commit a4088d9
Show file tree
Hide file tree
Showing 26 changed files with 363 additions and 217 deletions.
33 changes: 0 additions & 33 deletions example/event-listener.php

This file was deleted.

32 changes: 32 additions & 0 deletions example/event-system.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

require './example.php';

use Thenbsp\Wechat\Event\Entity\Text;
use Thenbsp\Wechat\Event\Entity\Image;
use Thenbsp\Wechat\Event\Entity\Voice;
use Thenbsp\Wechat\Event\Entity\Video;
use Thenbsp\Wechat\Event\Entity\Shortvideo;
use Thenbsp\Wechat\Event\Entity\Location;
use Thenbsp\Wechat\Event\Entity\Link;

use Thenbsp\Wechat\Event\EventHandler;
use Thenbsp\Wechat\Event\EventListener;

$callable = function($event) {
return $event;
var_dump(get_class($event));
};

$listener = new EventListener();
$listener
->addListener(Text::class, $callable)
->addListener(Image::class, $callable)
->addListener(Voice::class, $callable)
->addListener(Video::class, $callable)
->addListener(Shortvideo::class, $callable)
->addListener(Location::class, $callable)
->addListener(Link::class, $callable);

$handler = new EventHandler();
$handler->handle($listener);
20 changes: 20 additions & 0 deletions src/Bridge/XmlResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Thenbsp\Wechat\Bridge;

use Thenbsp\Wechat\Bridge\Serializer;
use Symfony\Component\HttpFoundation\Response;

class XmlResponse extends Response
{
/**
* 构造方法
*/
public function __construct(array $options, array $headers = array(), $status = 200)
{
$content = Serializer::xmlEncode($options);
$headers = array_replace($headers, array('Content-Type'=>'application/xml'));

parent::__construct($content, $status, $headers);
}
}
25 changes: 25 additions & 0 deletions src/Event/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Thenbsp\Wechat\Event;

use Thenbsp\Wechat\Bridge\Serializer;
use Thenbsp\Wechat\Bridge\XmlResponse;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\Common\Collections\ArrayCollection;

abstract class Entity extends ArrayCollection
{
/**
* send entity
*/
public function send()
{
$response = new XmlResponse($this->toArray());
$response->send();
}

/**
* check entity options is valid event
*/
abstract public function isValid();
}
13 changes: 13 additions & 0 deletions src/Event/Entity/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Thenbsp\Wechat\Event\Entity;

use Thenbsp\Wechat\Event\Entity;

class Image extends Entity
{
public function isValid()
{
return (strtolower($this['MsgType']) === 'image');
}
}
13 changes: 13 additions & 0 deletions src/Event/Entity/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Thenbsp\Wechat\Event\Entity;

use Thenbsp\Wechat\Event\Entity;

class Link extends Entity
{
public function isValid()
{
return (strtolower($this['MsgType']) === 'link');
}
}
13 changes: 13 additions & 0 deletions src/Event/Entity/Location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Thenbsp\Wechat\Event\Entity;

use Thenbsp\Wechat\Event\Entity;

class Location extends Entity
{
public function isValid()
{
return (strtolower($this['MsgType']) === 'location');
}
}
13 changes: 13 additions & 0 deletions src/Event/Entity/Shortvideo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Thenbsp\Wechat\Event\Entity;

use Thenbsp\Wechat\Event\Entity;

class Shortvideo extends Entity
{
public function isValid()
{
return (strtolower($this['MsgType']) === 'shortvideo');
}
}
13 changes: 13 additions & 0 deletions src/Event/Entity/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Thenbsp\Wechat\Event\Entity;

use Thenbsp\Wechat\Event\Entity;

class Text extends Entity
{
public function isValid()
{
return (strtolower($this['MsgType']) === 'text');
}
}
13 changes: 13 additions & 0 deletions src/Event/Entity/Video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Thenbsp\Wechat\Event\Entity;

use Thenbsp\Wechat\Event\Entity;

class Video extends Entity
{
public function isValid()
{
return (strtolower($this['MsgType']) === 'video');
}
}
13 changes: 13 additions & 0 deletions src/Event/Entity/Voice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Thenbsp\Wechat\Event\Entity;

use Thenbsp\Wechat\Event\Entity;

class Voice extends Entity
{
public function isValid()
{
return (strtolower($this['MsgType']) === 'voice');
}
}
61 changes: 61 additions & 0 deletions src/Event/EventHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Thenbsp\Wechat\Event;

use Thenbsp\Wechat\Bridge\Serializer;
use Symfony\Component\HttpFoundation\Request;

class EventHandler implements EventHandlerInterface
{
/**
* Symfony\Component\HttpFoundation\Request
*/
protected $request;

/**
* initialize request
*/
public function __construct(Request $request = null)
{
$request = $request ?: Request::createFromGlobals();

$this->fromRequest($request);
}

/**
* set from request
*/
public function fromRequest(Request $request)
{
$this->request = $request;
}

/**
* handle event
*/
public function handle(EventListenerInterface $listener)
{
if( !$listener->getListeners() ) {
return;
}

$content = $this->request->getContent();

try {
$options = Serializer::parse($content);
} catch (\InvalidArgumentException $e) {
$options = array();
}

foreach( $listener->getListeners() as $namespace => $callable ) {
$handler = new $namespace($options);
if( $handler->isValid() ) {
$callback = $listener->trigger($namespace, $handler);
if( $callback instanceof Entity ) {
$callback->send();
}
break;
}
}
}
}
18 changes: 18 additions & 0 deletions src/Event/EventHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Thenbsp\Wechat\Event;

use Symfony\Component\HttpFoundation\Request;

interface EventHandlerInterface
{
/**
* set from request
*/
public function fromRequest(Request $request);

/**
* handle event
*/
public function handle(EventListenerInterface $listener);
}
76 changes: 76 additions & 0 deletions src/Event/EventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Thenbsp\Wechat\Event;

class EventListener implements EventListenerInterface
{
/**
* list of listeners
*/
protected $listeners = array();

/**
* trigger event
*/
public function trigger($handler, Entity $entity)
{
if( $listener = $this->getListener($handler) ) {
return call_user_func_array($listener, array($entity));
}
}

/**
* add listener
*/
public function addListener($handler, callable $callable)
{
if( !class_exists($handler) ) {
throw new \InvalidArgumentException(sprintf('Invlaid Handler "%s"', $handler));
}

if( !is_subclass_of($handler, Entity::class) ) {
throw new \InvalidArgumentException(sprintf(
'The Handler "%s" must be extends "%s"', $handler, Entity::class));
}

$this->listeners[$handler] = $callable;

return $this;
}

/**
* get listener
*/
public function getListener($handler)
{
if( $this->hasListener($handler) ) {
return $this->listeners[$handler];
}
}

/**
* has listener
*/
public function hasListener($handler)
{
return array_key_exists($handler, $this->listeners);
}

/**
* remove listener
*/
public function removeListener($handler)
{
if( $this->hasListener($handler) ) {
unset($this->listeners[$handler]);
}
}

/**
* get listeners
*/
public function getListeners()
{
return $this->listeners;
}
}
Loading

0 comments on commit a4088d9

Please sign in to comment.