forked from thenbsp/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
363 additions
and
217 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.