Skip to content

Develop #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Foundation/ServiceProviders/ServerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function register(Container $pimple)
};

$pimple['server'] = function ($pimple) {
$server = new Guard();
$server = new Guard($pimple['config']['token']);

$server->debug($pimple['config']['debug']);

Expand Down
23 changes: 10 additions & 13 deletions src/Server/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,31 @@ class Guard
const ALL_MSG = 1048830;

/**
* Request instance.
*
* @var Request
*/
protected $request;

/**
* Encryptor instance.
*
* @var string
*/
protected $token;

/**
* @var Encryptor
*/
protected $encryptor;

/**
* Message listener.
*
* @var string|callable
*/
protected $messageHandler;

/**
* Message type filter.
*
* @var int
*/
protected $messageFilter;

/**
* Message type mapping.
*
* @var array
*/
protected $messageTypeMapping = [
Expand All @@ -98,19 +93,19 @@ class Guard
];

/**
* Debug mode.
*
* @var bool
*/
protected $debug = false;

/**
* Constructor.
*
* @param string $token
* @param Request $request
*/
public function __construct(Request $request = null)
public function __construct($token, Request $request = null)
{
$this->token = $token;
$this->request = $request ?: Request::createFromGlobals();
}

Expand Down Expand Up @@ -145,6 +140,8 @@ public function serve()
'Content' => $this->request->getContent(),
]);

$this->validate($this->token);

if ($str = $this->request->get('echostr')) {
Log::debug("Output 'echostr' is '$str'.");

Expand Down
2 changes: 1 addition & 1 deletion src/Support/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class File
*/
public static function getStreamExt($stream)
{
if (is_file($stream)) {
if (is_dir(pathinfo($stream, PATHINFO_DIRNAME)) && is_readable($stream)) {
$stream = file_get_contents($stream);
}

Expand Down
35 changes: 20 additions & 15 deletions src/Support/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
namespace EasyWeChat\Support;

use SimpleXMLElement;

/**
* Class XML.
*/
Expand All @@ -34,13 +36,7 @@ class XML
*/
public static function parse($xml)
{
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);

if (is_object($data) && get_class($data) === 'SimpleXMLElement') {
$data = self::arrarval($data);
}

return $data;
return self::normalize(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS));
}

/**
Expand Down Expand Up @@ -99,19 +95,28 @@ public static function cdata($string)
*
* @return array
*/
private static function arrarval($data)
protected static function normalize($obj)
{
if (is_object($data) && get_class($data) === 'SimpleXMLElement') {
$data = (array) $data;
$result = null;

if (is_object($obj)) {
$obj = get_object_vars($obj);
}

if (is_array($data)) {
foreach ($data as $index => $value) {
$data[$index] = self::arrarval($value);
if (is_array($obj)) {
foreach ($obj as $key => $value) {
$res = self::normalize($value);
if (($key === '@attributes') && ($key)) {
$result = $res;
} else {
$result[$key] = $res;
}
}
} else {
$result = $obj;
}

return $data;
return $result;
}

/**
Expand All @@ -123,7 +128,7 @@ private static function arrarval($data)
*
* @return string
*/
private static function data2Xml($data, $item = 'item', $id = 'id')
protected static function data2Xml($data, $item = 'item', $id = 'id')
{
$xml = $attr = '';

Expand Down
28 changes: 13 additions & 15 deletions tests/Server/ServerGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@

class ServerGuardTest extends TestCase
{
public function getServer($message = '', $queries = null)
public function getServer($message = '', $queries = [])
{
$request = Mockery::mock(Request::class.'[get,getContent]');

$request->shouldReceive('get')->andReturnUsing(function ($key) use ($queries) {
$queries = $queries ?: [
'signature' => '5fe39987c51aa87c0da1af7420d4649d77850391',
'timestamp' => '1437865042',
'nonce' => '335941714',
];

return isset($queries[$key]) ? $queries[$key] : null;
});

$token = 'test_token';
$nonce = '335941714';
$timestamp = '1437865042';
$signature = 'd060dbd49d56631d867c5fa8063650d3246bd355';

$queries = array_merge([
'signature' => $signature,
'timestamp' => $timestamp,
'nonce' => $nonce,
], $queries);
$message = $message ?: [
'ToUserName' => 'gh_9a1a7e312b32',
'FromUserName' => 'oNlnUjq_uJdd52zt3OxFsJHEr_NY',
Expand All @@ -40,9 +38,9 @@ public function getServer($message = '', $queries = null)
'MsgId' => '6175583331658476609',
];

$request->shouldReceive('getContent')->andReturn(XML::build($message));
$request = new Request($queries, [], [], [], [], [], XML::build($message));

$server = new Guard($request);
$server = new Guard($token, $request);

return $server;
}
Expand Down