Skip to content
This repository has been archived by the owner on May 8, 2021. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyoung committed Apr 14, 2019
0 parents commit 3179228
Show file tree
Hide file tree
Showing 21 changed files with 1,106 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/EasyWeChat/Client.php
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the mingyoung/wechat-playground.
*
* (c) 张铭阳 <mingyoungcheung@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace WeChatPlayground\EasyWeChat;

use EasyWeChat\Kernel\BaseClient;

class Client extends BaseClient
{
/**
* @param string $method
* @param string $uri
* @param array $options
*
* @return \WeChatPlayground\EasyWeChat\Response
*/
public function perform($method, $uri, $options = [])
{
switch ($method) {
case 'GET':
return $this->makesHttpRequest($uri, 'GET', ['query' => $options['query']]);
case 'POST':
return $this->makesHttpRequest($uri, 'POST', ['json' => $options['json']]);
case 'UPLOAD':
$multipart = [];

foreach ($options['multipart'] as $name => $path) {
$multipart[] = [
'name' => $name,
'contents' => fopen($path, 'r'),
];
}

return $this->makesHttpRequest($uri, 'POST', ['query' => $options['query'], 'multipart' => $multipart]);
}
}

/**
* @param string $uri
* @param string $method
* @param array $options
*
* @return \WeChatPlayground\EasyWeChat\Response
*/
protected function makesHttpRequest($uri, $method, $options = [])
{
return new Response(
$this->requestRaw($uri, $method, array_merge(['on_stats' => $statable = new Statable()], $options)), $statable
);
}
}
48 changes: 48 additions & 0 deletions src/EasyWeChat/OfficialAccount.php
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the mingyoung/wechat-playground.
*
* (c) 张铭阳 <mingyoungcheung@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace WeChatPlayground\EasyWeChat;

use EasyWeChat\OfficialAccount\Application;
use WeChatPlayground\Playground;

class OfficialAccount extends Application
{
/**
* @var \Illuminate\Foundation\Application
*/
protected $laravel;

/**
* @param \Illuminate\Foundation\Application $laravel
*/
public function __construct($laravel)
{
$this->laravel = $laravel;

parent::__construct($this->getPlaygroundConfig());

$this->register(new PlaygroundServiceProvider());
}

/**
* @return array
*/
protected function getPlaygroundConfig()
{
$resolved = Playground::usingApp()->__invoke($this->laravel['request']);

return [
'app_id' => $resolved['config']['app_id'],
'secret' => $resolved['config']['secret'],
];
}
}
28 changes: 28 additions & 0 deletions src/EasyWeChat/PlaygroundServiceProvider.php
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the mingyoung/wechat-playground.
*
* (c) 张铭阳 <mingyoungcheung@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace WeChatPlayground\EasyWeChat;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

class PlaygroundServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
$app['playground'] = function ($app) {
return new Client($app);
};
}
}
107 changes: 107 additions & 0 deletions src/EasyWeChat/Response.php
@@ -0,0 +1,107 @@
<?php

/*
* This file is part of the mingyoung/wechat-playground.
*
* (c) 张铭阳 <mingyoungcheung@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace WeChatPlayground\EasyWeChat;

use EasyWeChat\Kernel\Support\File;
use GuzzleHttp\Psr7\Uri;
use Illuminate\Contracts\Support\Responsable;

class Response implements Responsable
{
/**
* @var \EasyWeChat\Kernel\Http\Response
*/
protected $response;

/**
* @var \WeChatPlayground\EasyWeChat\Statable
*/
protected $statable;

/**
* @param \EasyWeChat\Kernel\Http\Response $response
* @param \WeChatPlayground\EasyWeChat\Statable $statable
*/
public function __construct($response, Statable $statable)
{
$this->response = $response;
$this->statable = $statable;
}

/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function toResponse($request)
{
$uri = $this->statable->stats->getEffectiveUri();

return response()->json([
'payload' => $this->getPayload(),
'response' => $this->getResponse(),
'details' => [
['AppId' => app(OfficialAccount::class)['config']['app_id']],
['Method' => $this->statable->stats->getRequest()->getMethod()],
['API' => Uri::composeComponents($uri->getScheme(), $uri->getAuthority(), $uri->getPath(), $this->getQuery($uri), $uri->getFragment())],
['Duration' => $this->statable->stats->getTransferTime().' 秒'],
],
]);
}

/**
* @param \GuzzleHttp\Psr7\Uri $uri
*
* @return string
*/
protected function getQuery($uri)
{
parse_str($uri->getQuery(), $query);
if (isset($query['access_token'])) {
$query['access_token'] = 'ACCESS_TOKEN';
}

return http_build_query($query);
}

/**
* @return array
*/
protected function getPayload()
{
$body = $this->statable->stats->getRequest()->getBody();

return json_decode($body, true) ?? [];
}

/**
* @return array
*/
protected function getResponse()
{
$response = $this->response;

$array = json_decode($response, true);

if (JSON_ERROR_NONE === json_last_error()) {
return ['type' => 'json', 'content' => $array];
}

return [
'type' => 'stream',
'extension' => File::getStreamExt($response),
'content' => base64_encode($response),
];
}
}
30 changes: 30 additions & 0 deletions src/EasyWeChat/Statable.php
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the mingyoung/wechat-playground.
*
* (c) 张铭阳 <mingyoungcheung@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace WeChatPlayground\EasyWeChat;

use GuzzleHttp\TransferStats;

class Statable
{
/**
* @var \GuzzleHttp\TransferStats
*/
public $stats;

/**
* @param \GuzzleHttp\TransferStats $stats
*/
public function __invoke(TransferStats $stats)
{
$this->stats = $stats;
}
}
16 changes: 16 additions & 0 deletions src/Exceptions/RuntimeException.php
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the mingyoung/wechat-playground.
*
* (c) 张铭阳 <mingyoungcheung@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace WeChatPlayground\Exceptions;

class RuntimeException extends \RuntimeException
{
}

0 comments on commit 3179228

Please sign in to comment.