初始上传
This commit is contained in:
251
vendor/overtrue/wechat/src/Kernel/Traits/HasAttributes.php
vendored
Executable file
251
vendor/overtrue/wechat/src/Kernel/Traits/HasAttributes.php
vendored
Executable file
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace EasyWeChat\Kernel\Traits;
|
||||
|
||||
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
|
||||
use EasyWeChat\Kernel\Support\Arr;
|
||||
use EasyWeChat\Kernel\Support\Str;
|
||||
|
||||
/**
|
||||
* Trait Attributes.
|
||||
*/
|
||||
trait HasAttributes
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $snakeable = true;
|
||||
|
||||
/**
|
||||
* Set Attributes.
|
||||
*
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setAttributes(array $attributes = [])
|
||||
{
|
||||
$this->attributes = $attributes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set attribute.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setAttribute($attribute, $value)
|
||||
{
|
||||
Arr::set($this->attributes, $attribute, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAttribute($attribute, $default = null)
|
||||
{
|
||||
return Arr::get($this->attributes, $attribute, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRequired($attribute)
|
||||
{
|
||||
return in_array($attribute, $this->getRequired(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function getRequired()
|
||||
{
|
||||
return property_exists($this, 'required') ? $this->required : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set attribute.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function with($attribute, $value)
|
||||
{
|
||||
$this->snakeable && $attribute = Str::snake($attribute);
|
||||
|
||||
$this->setAttribute($attribute, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override parent set() method.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set($attribute, $value)
|
||||
{
|
||||
$this->setAttribute($attribute, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override parent get() method.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($attribute, $default = null)
|
||||
{
|
||||
return $this->getAttribute($attribute, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $key)
|
||||
{
|
||||
return Arr::has($this->attributes, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function merge(array $attributes)
|
||||
{
|
||||
$this->attributes = array_merge($this->attributes, $attributes);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string $keys
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function only($keys)
|
||||
{
|
||||
return Arr::only($this->attributes, $keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all items.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
$this->checkRequiredAttributes();
|
||||
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic call.
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $args
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function __call($method, $args)
|
||||
{
|
||||
if (0 === stripos($method, 'with')) {
|
||||
return $this->with(substr($method, 4), array_shift($args));
|
||||
}
|
||||
|
||||
throw new \BadMethodCallException(sprintf('Method "%s" does not exists.', $method));
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic get.
|
||||
*
|
||||
* @param string $property
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($property)
|
||||
{
|
||||
return $this->get($property);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic set.
|
||||
*
|
||||
* @param string $property
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function __set($property, $value)
|
||||
{
|
||||
return $this->with($property, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not an data exists by key.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset($key)
|
||||
{
|
||||
return isset($this->attributes[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check required attributes.
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
protected function checkRequiredAttributes()
|
||||
{
|
||||
foreach ($this->getRequired() as $attribute) {
|
||||
if (is_null($this->get($attribute))) {
|
||||
throw new InvalidArgumentException(sprintf('"%s" cannot be empty.', $attribute));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
231
vendor/overtrue/wechat/src/Kernel/Traits/HasHttpRequests.php
vendored
Executable file
231
vendor/overtrue/wechat/src/Kernel/Traits/HasHttpRequests.php
vendored
Executable file
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace EasyWeChat\Kernel\Traits;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Trait HasHttpRequests.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
trait HasHttpRequests
|
||||
{
|
||||
use ResponseCastable;
|
||||
|
||||
/**
|
||||
* @var \GuzzleHttp\ClientInterface
|
||||
*/
|
||||
protected $httpClient;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $middlewares = [];
|
||||
|
||||
/**
|
||||
* @var \GuzzleHttp\HandlerStack
|
||||
*/
|
||||
protected $handlerStack;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $defaults = [
|
||||
'curl' => [
|
||||
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Set guzzle default settings.
|
||||
*
|
||||
* @param array $defaults
|
||||
*/
|
||||
public static function setDefaultOptions($defaults = [])
|
||||
{
|
||||
self::$defaults = $defaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return current guzzle default settings.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getDefaultOptions(): array
|
||||
{
|
||||
return self::$defaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set GuzzleHttp\Client.
|
||||
*
|
||||
* @param \GuzzleHttp\ClientInterface $httpClient
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setHttpClient(ClientInterface $httpClient)
|
||||
{
|
||||
$this->httpClient = $httpClient;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return GuzzleHttp\ClientInterface instance.
|
||||
*
|
||||
* @return ClientInterface
|
||||
*/
|
||||
public function getHttpClient(): ClientInterface
|
||||
{
|
||||
if (!($this->httpClient instanceof ClientInterface)) {
|
||||
if (property_exists($this, 'app') && $this->app['http_client']) {
|
||||
$this->httpClient = $this->app['http_client'];
|
||||
} else {
|
||||
$this->httpClient = new Client(['handler' => HandlerStack::create($this->getGuzzleHandler())]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->httpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a middleware.
|
||||
*
|
||||
* @param callable $middleware
|
||||
* @param string $name
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function pushMiddleware(callable $middleware, string $name = null)
|
||||
{
|
||||
if (!is_null($name)) {
|
||||
$this->middlewares[$name] = $middleware;
|
||||
} else {
|
||||
array_push($this->middlewares, $middleware);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all middlewares.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getMiddlewares(): array
|
||||
{
|
||||
return $this->middlewares;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param array $options
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function request($url, $method = 'GET', $options = []): ResponseInterface
|
||||
{
|
||||
$method = strtoupper($method);
|
||||
|
||||
$options = array_merge(self::$defaults, $options, ['handler' => $this->getHandlerStack()]);
|
||||
|
||||
$options = $this->fixJsonIssue($options);
|
||||
|
||||
if (property_exists($this, 'baseUri') && !is_null($this->baseUri)) {
|
||||
$options['base_uri'] = $this->baseUri;
|
||||
}
|
||||
|
||||
$response = $this->getHttpClient()->request($method, $url, $options);
|
||||
$response->getBody()->rewind();
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \GuzzleHttp\HandlerStack $handlerStack
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setHandlerStack(HandlerStack $handlerStack)
|
||||
{
|
||||
$this->handlerStack = $handlerStack;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a handler stack.
|
||||
*
|
||||
* @return \GuzzleHttp\HandlerStack
|
||||
*/
|
||||
public function getHandlerStack(): HandlerStack
|
||||
{
|
||||
if ($this->handlerStack) {
|
||||
return $this->handlerStack;
|
||||
}
|
||||
|
||||
$this->handlerStack = HandlerStack::create($this->getGuzzleHandler());
|
||||
|
||||
foreach ($this->middlewares as $name => $middleware) {
|
||||
$this->handlerStack->push($middleware, $name);
|
||||
}
|
||||
|
||||
return $this->handlerStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function fixJsonIssue(array $options): array
|
||||
{
|
||||
if (isset($options['json']) && is_array($options['json'])) {
|
||||
$options['headers'] = array_merge($options['headers'] ?? [], ['Content-Type' => 'application/json']);
|
||||
|
||||
if (empty($options['json'])) {
|
||||
$options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_FORCE_OBJECT);
|
||||
} else {
|
||||
$options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
unset($options['json']);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get guzzle handler.
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
protected function getGuzzleHandler()
|
||||
{
|
||||
if (property_exists($this, 'app') && isset($this->app['guzzle_handler'])) {
|
||||
return is_string($handler = $this->app->raw('guzzle_handler'))
|
||||
? new $handler()
|
||||
: $handler;
|
||||
}
|
||||
|
||||
return \GuzzleHttp\choose_handler();
|
||||
}
|
||||
}
|
||||
105
vendor/overtrue/wechat/src/Kernel/Traits/InteractsWithCache.php
vendored
Executable file
105
vendor/overtrue/wechat/src/Kernel/Traits/InteractsWithCache.php
vendored
Executable file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace EasyWeChat\Kernel\Traits;
|
||||
|
||||
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
|
||||
use EasyWeChat\Kernel\ServiceContainer;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
|
||||
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
||||
use Symfony\Component\Cache\Psr16Cache;
|
||||
use Symfony\Component\Cache\Simple\FilesystemCache;
|
||||
|
||||
/**
|
||||
* Trait InteractsWithCache.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
trait InteractsWithCache
|
||||
{
|
||||
/**
|
||||
* @var \Psr\SimpleCache\CacheInterface
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* Get cache instance.
|
||||
*
|
||||
* @return \Psr\SimpleCache\CacheInterface
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
public function getCache()
|
||||
{
|
||||
if ($this->cache) {
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
if (property_exists($this, 'app') && $this->app instanceof ServiceContainer && isset($this->app['cache'])) {
|
||||
$this->setCache($this->app['cache']);
|
||||
|
||||
// Fix PHPStan error
|
||||
assert($this->cache instanceof \Psr\SimpleCache\CacheInterface);
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
return $this->cache = $this->createDefaultCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cache instance.
|
||||
*
|
||||
* @param \Psr\SimpleCache\CacheInterface|\Psr\Cache\CacheItemPoolInterface $cache
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
public function setCache($cache)
|
||||
{
|
||||
if (empty(\array_intersect([SimpleCacheInterface::class, CacheItemPoolInterface::class], \class_implements($cache)))) {
|
||||
throw new InvalidArgumentException(\sprintf('The cache instance must implements %s or %s interface.', SimpleCacheInterface::class, CacheItemPoolInterface::class));
|
||||
}
|
||||
|
||||
if ($cache instanceof CacheItemPoolInterface) {
|
||||
if (!$this->isSymfony43OrHigher()) {
|
||||
throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class));
|
||||
}
|
||||
$cache = new Psr16Cache($cache);
|
||||
}
|
||||
|
||||
$this->cache = $cache;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Psr\SimpleCache\CacheInterface
|
||||
*/
|
||||
protected function createDefaultCache()
|
||||
{
|
||||
if ($this->isSymfony43OrHigher()) {
|
||||
return new Psr16Cache(new FilesystemAdapter('easywechat', 1500));
|
||||
}
|
||||
|
||||
return new FilesystemCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function isSymfony43OrHigher(): bool
|
||||
{
|
||||
return \class_exists('Symfony\Component\Cache\Psr16Cache');
|
||||
}
|
||||
}
|
||||
273
vendor/overtrue/wechat/src/Kernel/Traits/Observable.php
vendored
Executable file
273
vendor/overtrue/wechat/src/Kernel/Traits/Observable.php
vendored
Executable file
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace EasyWeChat\Kernel\Traits;
|
||||
|
||||
use EasyWeChat\Kernel\Clauses\Clause;
|
||||
use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
|
||||
use EasyWeChat\Kernel\Decorators\FinallyResult;
|
||||
use EasyWeChat\Kernel\Decorators\TerminateResult;
|
||||
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
|
||||
use EasyWeChat\Kernel\ServiceContainer;
|
||||
|
||||
/**
|
||||
* Trait Observable.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
trait Observable
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $handlers = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $clauses = [];
|
||||
|
||||
/**
|
||||
* @param \Closure|EventHandlerInterface|callable|string $handler
|
||||
* @param \Closure|EventHandlerInterface|callable|string $condition
|
||||
*
|
||||
* @return \EasyWeChat\Kernel\Clauses\Clause
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function push($handler, $condition = '*')
|
||||
{
|
||||
list($handler, $condition) = $this->resolveHandlerAndCondition($handler, $condition);
|
||||
|
||||
if (!isset($this->handlers[$condition])) {
|
||||
$this->handlers[$condition] = [];
|
||||
}
|
||||
|
||||
array_push($this->handlers[$condition], $handler);
|
||||
|
||||
return $this->newClause($handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Closure|EventHandlerInterface|string $handler
|
||||
* @param \Closure|EventHandlerInterface|string $condition
|
||||
*
|
||||
* @return \EasyWeChat\Kernel\Clauses\Clause
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function unshift($handler, $condition = '*')
|
||||
{
|
||||
list($handler, $condition) = $this->resolveHandlerAndCondition($handler, $condition);
|
||||
|
||||
if (!isset($this->handlers[$condition])) {
|
||||
$this->handlers[$condition] = [];
|
||||
}
|
||||
|
||||
array_unshift($this->handlers[$condition], $handler);
|
||||
|
||||
return $this->newClause($handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $condition
|
||||
* @param \Closure|EventHandlerInterface|string $handler
|
||||
*
|
||||
* @return \EasyWeChat\Kernel\Clauses\Clause
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function observe($condition, $handler)
|
||||
{
|
||||
return $this->push($handler, $condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $condition
|
||||
* @param \Closure|EventHandlerInterface|string $handler
|
||||
*
|
||||
* @return \EasyWeChat\Kernel\Clauses\Clause
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function on($condition, $handler)
|
||||
{
|
||||
return $this->push($handler, $condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|int $event
|
||||
* @param mixed ...$payload
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function dispatch($event, $payload)
|
||||
{
|
||||
return $this->notify($event, $payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|int $event
|
||||
* @param mixed ...$payload
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function notify($event, $payload)
|
||||
{
|
||||
$result = null;
|
||||
|
||||
foreach ($this->handlers as $condition => $handlers) {
|
||||
if ('*' === $condition || ($condition & $event) === $event) {
|
||||
foreach ($handlers as $handler) {
|
||||
if ($clause = $this->clauses[$this->getHandlerHash($handler)] ?? null) {
|
||||
if ($clause->intercepted($payload)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$response = $this->callHandler($handler, $payload);
|
||||
|
||||
switch (true) {
|
||||
case $response instanceof TerminateResult:
|
||||
return $response->content;
|
||||
case true === $response:
|
||||
continue 2;
|
||||
case false === $response:
|
||||
break 2;
|
||||
case !empty($response) && !($result instanceof FinallyResult):
|
||||
$result = $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result instanceof FinallyResult ? $result->content : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getHandlers()
|
||||
{
|
||||
return $this->handlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $handler
|
||||
*
|
||||
* @return \EasyWeChat\Kernel\Clauses\Clause
|
||||
*/
|
||||
protected function newClause($handler): Clause
|
||||
{
|
||||
return $this->clauses[$this->getHandlerHash($handler)] = new Clause();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $handler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getHandlerHash($handler)
|
||||
{
|
||||
if (is_string($handler)) {
|
||||
return $handler;
|
||||
}
|
||||
|
||||
if (is_array($handler)) {
|
||||
return is_string($handler[0])
|
||||
? $handler[0].'::'.$handler[1]
|
||||
: get_class($handler[0]).$handler[1];
|
||||
}
|
||||
|
||||
return spl_object_hash($handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $handler
|
||||
* @param mixed $payload
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function callHandler(callable $handler, $payload)
|
||||
{
|
||||
try {
|
||||
return call_user_func_array($handler, [$payload]);
|
||||
} catch (\Exception $e) {
|
||||
if (property_exists($this, 'app') && $this->app instanceof ServiceContainer) {
|
||||
$this->app['logger']->error($e->getCode().': '.$e->getMessage(), [
|
||||
'code' => $e->getCode(),
|
||||
'message' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $handler
|
||||
*
|
||||
* @return \Closure
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected function makeClosure($handler)
|
||||
{
|
||||
if (is_callable($handler)) {
|
||||
return $handler;
|
||||
}
|
||||
|
||||
if (is_string($handler) && '*' !== $handler) {
|
||||
if (!class_exists($handler)) {
|
||||
throw new InvalidArgumentException(sprintf('Class "%s" not exists.', $handler));
|
||||
}
|
||||
|
||||
if (!in_array(EventHandlerInterface::class, (new \ReflectionClass($handler))->getInterfaceNames(), true)) {
|
||||
throw new InvalidArgumentException(sprintf('Class "%s" not an instance of "%s".', $handler, EventHandlerInterface::class));
|
||||
}
|
||||
|
||||
return function ($payload) use ($handler) {
|
||||
return (new $handler($this->app ?? null))->handle($payload);
|
||||
};
|
||||
}
|
||||
|
||||
if ($handler instanceof EventHandlerInterface) {
|
||||
return function () use ($handler) {
|
||||
return $handler->handle(...func_get_args());
|
||||
};
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('No valid handler is found in arguments.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $handler
|
||||
* @param mixed $condition
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected function resolveHandlerAndCondition($handler, $condition): array
|
||||
{
|
||||
if (is_int($handler) || (is_string($handler) && !class_exists($handler))) {
|
||||
list($handler, $condition) = [$condition, $handler];
|
||||
}
|
||||
|
||||
return [$this->makeClosure($handler), $condition];
|
||||
}
|
||||
}
|
||||
93
vendor/overtrue/wechat/src/Kernel/Traits/ResponseCastable.php
vendored
Executable file
93
vendor/overtrue/wechat/src/Kernel/Traits/ResponseCastable.php
vendored
Executable file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace EasyWeChat\Kernel\Traits;
|
||||
|
||||
use EasyWeChat\Kernel\Contracts\Arrayable;
|
||||
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
|
||||
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
|
||||
use EasyWeChat\Kernel\Http\Response;
|
||||
use EasyWeChat\Kernel\Support\Collection;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Trait ResponseCastable.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
trait ResponseCastable
|
||||
{
|
||||
/**
|
||||
* @param \Psr\Http\Message\ResponseInterface $response
|
||||
* @param string|null $type
|
||||
*
|
||||
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
*/
|
||||
protected function castResponseToType(ResponseInterface $response, $type = null)
|
||||
{
|
||||
$response = Response::buildFromPsrResponse($response);
|
||||
$response->getBody()->rewind();
|
||||
|
||||
switch ($type ?? 'array') {
|
||||
case 'collection':
|
||||
return $response->toCollection();
|
||||
case 'array':
|
||||
return $response->toArray();
|
||||
case 'object':
|
||||
return $response->toObject();
|
||||
case 'raw':
|
||||
return $response;
|
||||
default:
|
||||
if (!is_subclass_of($type, Arrayable::class)) {
|
||||
throw new InvalidConfigException(sprintf('Config key "response_type" classname must be an instanceof %s', Arrayable::class));
|
||||
}
|
||||
|
||||
return new $type($response);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $response
|
||||
* @param string|null $type
|
||||
*
|
||||
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
*
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
*/
|
||||
protected function detectAndCastResponseToType($response, $type = null)
|
||||
{
|
||||
switch (true) {
|
||||
case $response instanceof ResponseInterface:
|
||||
$response = Response::buildFromPsrResponse($response);
|
||||
|
||||
break;
|
||||
case $response instanceof Arrayable:
|
||||
$response = new Response(200, [], json_encode($response->toArray()));
|
||||
|
||||
break;
|
||||
case ($response instanceof Collection) || is_array($response) || is_object($response):
|
||||
$response = new Response(200, [], json_encode($response));
|
||||
|
||||
break;
|
||||
case is_scalar($response):
|
||||
$response = new Response(200, [], (string) $response);
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response)));
|
||||
}
|
||||
|
||||
return $this->castResponseToType($response, $type);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user