Store the customized Shopro mall so it can be kept on the remote.
Co-authored-by: Cursor <cursoragent@cursor.com>
这个提交包含在:
@@ -0,0 +1,342 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Container\Container as LaravelContainer;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\MessageInterface;
|
||||
use Throwable;
|
||||
use Yansongda\Artful\Contract\ConfigInterface;
|
||||
use Yansongda\Artful\Contract\DirectionInterface;
|
||||
use Yansongda\Artful\Contract\HttpClientFactoryInterface;
|
||||
use Yansongda\Artful\Contract\PackerInterface;
|
||||
use Yansongda\Artful\Contract\PluginInterface;
|
||||
use Yansongda\Artful\Contract\ServiceProviderInterface;
|
||||
use Yansongda\Artful\Contract\ShortcutInterface;
|
||||
use Yansongda\Artful\Direction\CollectionDirection;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
use Yansongda\Artful\Exception\ContainerNotFoundException;
|
||||
use Yansongda\Artful\Exception\Exception;
|
||||
use Yansongda\Artful\Exception\InvalidParamsException;
|
||||
use Yansongda\Artful\Exception\InvalidResponseException;
|
||||
use Yansongda\Artful\Exception\ServiceNotFoundException;
|
||||
use Yansongda\Artful\Packer\JsonPacker;
|
||||
use Yansongda\Artful\Service\ConfigServiceProvider;
|
||||
use Yansongda\Artful\Service\ContainerServiceProvider;
|
||||
use Yansongda\Artful\Service\EventServiceProvider;
|
||||
use Yansongda\Artful\Service\HttpServiceProvider;
|
||||
use Yansongda\Artful\Service\LoggerServiceProvider;
|
||||
use Yansongda\Supports\Collection;
|
||||
use Yansongda\Supports\Config;
|
||||
use Yansongda\Supports\Pipeline;
|
||||
|
||||
class Artful
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private array $coreService = [
|
||||
ContainerServiceProvider::class,
|
||||
ConfigServiceProvider::class,
|
||||
LoggerServiceProvider::class,
|
||||
EventServiceProvider::class,
|
||||
HttpServiceProvider::class,
|
||||
];
|
||||
|
||||
private static null|Closure|ContainerInterface $container = null;
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
private function __construct(array $config, null|Closure|ContainerInterface $container = null)
|
||||
{
|
||||
$this->registerServices($config, $container);
|
||||
|
||||
Artful::set(DirectionInterface::class, CollectionDirection::class);
|
||||
Artful::set(PackerInterface::class, JsonPacker::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*
|
||||
* @throws ContainerException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
public static function __callStatic(string $service, array $config)
|
||||
{
|
||||
if (!empty($config)) {
|
||||
self::config(...$config);
|
||||
}
|
||||
|
||||
return self::get($service);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public static function config(array $config = [], null|Closure|ContainerInterface $container = null): bool
|
||||
{
|
||||
if (self::hasContainer() && !($config['_force'] ?? false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
new self($config, $container);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public static function set(string $name, mixed $value): void
|
||||
{
|
||||
try {
|
||||
$container = Artful::getContainer();
|
||||
|
||||
if ($container instanceof LaravelContainer) {
|
||||
$container->singleton($name, $value instanceof Closure ? $value : static fn () => $value);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (method_exists($container, 'set')) {
|
||||
$container->set(...func_get_args());
|
||||
|
||||
return;
|
||||
}
|
||||
} catch (ContainerNotFoundException $e) {
|
||||
throw $e;
|
||||
} catch (Throwable $e) {
|
||||
throw new ContainerException('容器异常: '.$e->getMessage());
|
||||
}
|
||||
|
||||
throw new ContainerException('容器异常: 当前容器类型不支持 `set` 方法');
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public static function make(string $service, array $parameters = []): mixed
|
||||
{
|
||||
try {
|
||||
$container = Artful::getContainer();
|
||||
|
||||
if (method_exists($container, 'make')) {
|
||||
return $container->make(...func_get_args());
|
||||
}
|
||||
} catch (ContainerNotFoundException $e) {
|
||||
throw $e;
|
||||
} catch (Throwable $e) {
|
||||
throw new ContainerException('容器异常: '.$e->getMessage());
|
||||
}
|
||||
|
||||
$parameters = array_values($parameters);
|
||||
|
||||
return new $service(...$parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ServiceNotFoundException
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public static function get(string $service): mixed
|
||||
{
|
||||
try {
|
||||
return Artful::getContainer()->get($service);
|
||||
} catch (NotFoundExceptionInterface $e) {
|
||||
throw new ServiceNotFoundException('服务未找到: '.$e->getMessage());
|
||||
} catch (ContainerNotFoundException $e) {
|
||||
throw $e;
|
||||
} catch (Throwable $e) {
|
||||
throw new ContainerException('容器异常: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerNotFoundException
|
||||
*/
|
||||
public static function has(string $service): bool
|
||||
{
|
||||
return Artful::getContainer()->has($service);
|
||||
}
|
||||
|
||||
public static function setContainer(null|Closure|ContainerInterface $container): void
|
||||
{
|
||||
self::$container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerNotFoundException
|
||||
*/
|
||||
public static function getContainer(): ContainerInterface
|
||||
{
|
||||
if (self::$container instanceof ContainerInterface) {
|
||||
return self::$container;
|
||||
}
|
||||
|
||||
if (self::$container instanceof Closure) {
|
||||
return (self::$container)();
|
||||
}
|
||||
|
||||
throw new ContainerNotFoundException('容器未找到: `getContainer()` 方法调用失败! 或许你应该先 `setContainer()`');
|
||||
}
|
||||
|
||||
public static function hasContainer(): bool
|
||||
{
|
||||
return self::$container instanceof ContainerInterface || self::$container instanceof Closure;
|
||||
}
|
||||
|
||||
public static function clear(): void
|
||||
{
|
||||
self::$container = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public static function load(string $service, mixed $data = null): void
|
||||
{
|
||||
self::registerService($service, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public static function registerService(string $service, mixed $data = null): void
|
||||
{
|
||||
$var = new $service();
|
||||
|
||||
if ($var instanceof ServiceProviderInterface) {
|
||||
$var->register($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws InvalidParamsException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
public static function shortcut(string $shortcut, array $params = []): null|Collection|MessageInterface|Rocket
|
||||
{
|
||||
if (!class_exists($shortcut) || !in_array(ShortcutInterface::class, class_implements($shortcut))) {
|
||||
throw new InvalidParamsException(Exception::PARAMS_SHORTCUT_INVALID, "参数异常: [{$shortcut}] 未实现 `ShortcutInterface`");
|
||||
}
|
||||
|
||||
/* @var ShortcutInterface $shortcutInstance */
|
||||
$shortcutInstance = self::get($shortcut);
|
||||
|
||||
return self::artful($shortcutInstance->getPlugins($params), $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws InvalidParamsException
|
||||
*/
|
||||
public static function artful(array $plugins, array $params): null|Collection|MessageInterface|Rocket
|
||||
{
|
||||
Event::dispatch(new Event\ArtfulStart($plugins, $params));
|
||||
|
||||
self::verifyPlugin($plugins);
|
||||
|
||||
/* @var Pipeline $pipeline */
|
||||
$pipeline = self::make(Pipeline::class);
|
||||
|
||||
/* @var Rocket $rocket */
|
||||
$rocket = $pipeline
|
||||
->send((new Rocket())->setParams($params)->setPayload(new Collection()))
|
||||
->through($plugins)
|
||||
->via('assembly')
|
||||
->then(static fn ($rocket) => self::ignite($rocket));
|
||||
|
||||
Event::dispatch(new Event\ArtfulEnd($rocket));
|
||||
|
||||
if (!empty($params['_return_rocket'])) {
|
||||
return $rocket;
|
||||
}
|
||||
|
||||
return $rocket->getDestination();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws InvalidParamsException
|
||||
* @throws InvalidResponseException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
public static function ignite(Rocket $rocket): Rocket
|
||||
{
|
||||
if (!should_do_http_request($rocket->getDirection())) {
|
||||
return $rocket;
|
||||
}
|
||||
|
||||
/* @var HttpClientFactoryInterface $httpFactory */
|
||||
$httpFactory = self::get(HttpClientFactoryInterface::class);
|
||||
/* @var Config $config */
|
||||
$config = self::get(ConfigInterface::class);
|
||||
|
||||
if (!$httpFactory instanceof HttpClientFactoryInterface) {
|
||||
throw new InvalidParamsException(Exception::PARAMS_HTTP_CLIENT_FACTORY_INVALID, '参数异常: 配置的 `HttpClientFactoryInterface` 不符合规范');
|
||||
}
|
||||
|
||||
Logger::info('[Artful] 准备请求第三方 API', $rocket->toArray());
|
||||
|
||||
$http = $httpFactory->create(array_merge($config->get('http', []), $rocket->getPayload()?->get('_http') ?? []));
|
||||
|
||||
Event::dispatch(new Event\HttpStart($rocket));
|
||||
|
||||
try {
|
||||
$response = $http->sendRequest($rocket->getRadar());
|
||||
|
||||
$rocket->setDestination(clone $response)
|
||||
->setDestinationOrigin(clone $response);
|
||||
} catch (Throwable $e) {
|
||||
Logger::error('[Artful] 请求第三方 API 出错', ['message' => $e->getMessage(), 'rocket' => $rocket->toArray(), 'trace' => $e->getTrace()]);
|
||||
|
||||
throw new InvalidResponseException(Exception::REQUEST_RESPONSE_ERROR, '响应异常: 请求第三方 API 出错 - '.$e->getMessage(), [], $e);
|
||||
}
|
||||
|
||||
Logger::info('[Artful] 请求第三方 API 成功', ['rocket' => $rocket->toArray()]);
|
||||
|
||||
Event::dispatch(new Event\HttpEnd($rocket));
|
||||
|
||||
return $rocket;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidParamsException
|
||||
*/
|
||||
protected static function verifyPlugin(array $plugins): void
|
||||
{
|
||||
foreach ($plugins as $plugin) {
|
||||
if (is_callable($plugin)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((is_object($plugin)
|
||||
|| (is_string($plugin) && class_exists($plugin)))
|
||||
&& in_array(PluginInterface::class, class_implements($plugin))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new InvalidParamsException(Exception::PARAMS_PLUGIN_INCOMPATIBLE, "参数异常: [{$plugin}] 插件未实现 `PluginInterface`");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
private function registerServices(array $config, null|Closure|ContainerInterface $container = null): void
|
||||
{
|
||||
foreach ($this->coreService as $service) {
|
||||
self::registerService($service, ContainerServiceProvider::class == $service ? $container : $config);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
interface ConfigInterface
|
||||
{
|
||||
public function get(string $key, mixed $default = null): mixed;
|
||||
|
||||
public function has(string $key): bool;
|
||||
|
||||
public function set(string $key, mixed $value): void;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
interface ContainerInterface extends \Psr\Container\ContainerInterface
|
||||
{
|
||||
public function make(string $name, array $parameters = []): mixed;
|
||||
|
||||
public function set(string $name, mixed $entry): mixed;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
interface DirectionInterface
|
||||
{
|
||||
public function guide(PackerInterface $packer, ?ResponseInterface $response, array $params = []): mixed;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
interface EventDispatcherInterface extends \Psr\EventDispatcher\EventDispatcherInterface {}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
interface HttpClientFactoryInterface
|
||||
{
|
||||
public function create(array $options = []): ClientInterface;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
interface HttpClientInterface extends ClientInterface {}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
interface LoggerInterface extends \Psr\Log\LoggerInterface {}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
use Yansongda\Supports\Collection;
|
||||
|
||||
interface PackerInterface
|
||||
{
|
||||
public function pack(null|array|Collection $payload, ?array $params = null): string;
|
||||
|
||||
public function unpack(string $payload, ?array $params = null): ?array;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
use Closure;
|
||||
use Yansongda\Artful\Rocket;
|
||||
|
||||
interface PluginInterface
|
||||
{
|
||||
public function assembly(Rocket $rocket, Closure $next): Rocket;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
|
||||
interface ServiceProviderInterface
|
||||
{
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public function register(mixed $data = null): void;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Contract;
|
||||
|
||||
interface ShortcutInterface
|
||||
{
|
||||
/**
|
||||
* @return PluginInterface[]|string[]
|
||||
*/
|
||||
public function getPlugins(array $params): array;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Direction;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Yansongda\Artful\Contract\DirectionInterface;
|
||||
use Yansongda\Artful\Contract\PackerInterface;
|
||||
use Yansongda\Artful\Exception\Exception;
|
||||
use Yansongda\Artful\Exception\InvalidResponseException;
|
||||
use Yansongda\Supports\Collection;
|
||||
|
||||
class CollectionDirection implements DirectionInterface
|
||||
{
|
||||
/**
|
||||
* @throws InvalidResponseException
|
||||
*/
|
||||
public function guide(PackerInterface $packer, ?ResponseInterface $response, array $params = []): Collection
|
||||
{
|
||||
if (is_null($response)) {
|
||||
throw new InvalidResponseException(Exception::RESPONSE_EMPTY, '响应异常: 响应为空,不能进行 direction');
|
||||
}
|
||||
|
||||
$body = (string) $response->getBody();
|
||||
|
||||
if (!is_null($result = $packer->unpack($body, $params))) {
|
||||
return new Collection($result);
|
||||
}
|
||||
|
||||
throw new InvalidResponseException(Exception::RESPONSE_UNPACK_ERROR, '响应异常: 解包错误', ['body' => $body, 'response' => $response]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Direction;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Yansongda\Artful\Contract\DirectionInterface;
|
||||
use Yansongda\Artful\Contract\PackerInterface;
|
||||
|
||||
class NoHttpRequestDirection implements DirectionInterface
|
||||
{
|
||||
public function guide(PackerInterface $packer, ?ResponseInterface $response, array $params = []): ?ResponseInterface
|
||||
{
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Direction;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Yansongda\Artful\Contract\DirectionInterface;
|
||||
use Yansongda\Artful\Contract\PackerInterface;
|
||||
use Yansongda\Artful\Exception\Exception;
|
||||
use Yansongda\Artful\Exception\InvalidResponseException;
|
||||
|
||||
class OriginResponseDirection implements DirectionInterface
|
||||
{
|
||||
/**
|
||||
* @throws InvalidResponseException
|
||||
*/
|
||||
public function guide(PackerInterface $packer, ?ResponseInterface $response, array $params = []): ?ResponseInterface
|
||||
{
|
||||
if (is_null($response)) {
|
||||
throw new InvalidResponseException(Exception::RESPONSE_EMPTY, '响应异常: 响应为空,不能进行 direction');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Direction;
|
||||
|
||||
class ResponseDirection extends NoHttpRequestDirection {}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful;
|
||||
|
||||
use Yansongda\Artful\Contract\EventDispatcherInterface;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
use Yansongda\Artful\Exception\Exception;
|
||||
use Yansongda\Artful\Exception\InvalidParamsException;
|
||||
use Yansongda\Artful\Exception\ServiceNotFoundException;
|
||||
|
||||
/**
|
||||
* @method static Event\Event dispatch(object $event)
|
||||
*/
|
||||
class Event
|
||||
{
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws ServiceNotFoundException
|
||||
* @throws InvalidParamsException
|
||||
*/
|
||||
public static function __callStatic(string $method, array $args): void
|
||||
{
|
||||
if (!Artful::hasContainer() || !Artful::has(EventDispatcherInterface::class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$class = Artful::get(EventDispatcherInterface::class);
|
||||
|
||||
if ($class instanceof \Psr\EventDispatcher\EventDispatcherInterface) {
|
||||
$class->{$method}(...$args);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw new InvalidParamsException(Exception::PARAMS_EVENT_DRIVER_INVALID, '参数异常: 配置的 `EventDispatcherInterface` 不符合 PSR 规范');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Event;
|
||||
|
||||
class ArtfulEnd extends Event {}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Event;
|
||||
|
||||
class ArtfulStart extends Event
|
||||
{
|
||||
public function __construct(public array $plugins, public array $params) {}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Event;
|
||||
|
||||
use Yansongda\Artful\Rocket;
|
||||
|
||||
class Event
|
||||
{
|
||||
public function __construct(public ?Rocket $rocket = null) {}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Event;
|
||||
|
||||
class HttpEnd extends Event {}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Event;
|
||||
|
||||
class HttpStart extends Event {}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Exception;
|
||||
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Throwable;
|
||||
|
||||
class ContainerException extends Exception implements ContainerExceptionInterface
|
||||
{
|
||||
public function __construct(string $message = '容器异常', int $code = self::CONTAINER_ERROR, mixed $extra = null, ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $extra, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class ContainerNotFoundException extends ContainerException
|
||||
{
|
||||
public function __construct(string $message = '容器未找到', int $code = self::CONTAINER_NOT_FOUND, mixed $extra = null, ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $extra, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class Exception extends \Exception
|
||||
{
|
||||
public const UNKNOWN_ERROR = 9999;
|
||||
|
||||
/**
|
||||
* 关于容器.
|
||||
*/
|
||||
public const CONTAINER_ERROR = 9100;
|
||||
|
||||
public const CONTAINER_NOT_FOUND = 9101;
|
||||
|
||||
public const CONTAINER_SERVICE_NOT_FOUND = 9102;
|
||||
|
||||
/**
|
||||
* 关于参数.
|
||||
*/
|
||||
public const PARAMS_ERROR = 9200;
|
||||
|
||||
public const PARAMS_DIRECTION_INVALID = 9201;
|
||||
|
||||
public const PARAMS_PACKER_INVALID = 9202;
|
||||
|
||||
public const PARAMS_EVENT_DRIVER_INVALID = 9203;
|
||||
|
||||
public const PARAMS_HTTP_CLIENT_INVALID = 9204;
|
||||
|
||||
public const PARAMS_LOGGER_DRIVER_INVALID = 9205;
|
||||
|
||||
public const PARAMS_HTTP_CLIENT_FACTORY_INVALID = 9206;
|
||||
|
||||
public const PARAMS_PLUGIN_INCOMPATIBLE = 9207;
|
||||
|
||||
public const PARAMS_PARSER_DIRECTION_INVALID = 9208;
|
||||
|
||||
public const PARAMS_SHORTCUT_INVALID = 9209;
|
||||
|
||||
/**
|
||||
* 关于响应.
|
||||
*/
|
||||
public const RESPONSE_ERROR = 9300;
|
||||
|
||||
public const REQUEST_RESPONSE_ERROR = 9301;
|
||||
|
||||
public const RESPONSE_UNPACK_ERROR = 9302;
|
||||
|
||||
public const RESPONSE_EMPTY = 9303;
|
||||
|
||||
/**
|
||||
* 关于配置.
|
||||
*/
|
||||
public const CONFIG_ERROR = 9400;
|
||||
|
||||
public mixed $extra;
|
||||
|
||||
public function __construct(string $message = '未知异常', int $code = self::UNKNOWN_ERROR, mixed $extra = null, ?Throwable $previous = null)
|
||||
{
|
||||
$this->extra = $extra;
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class InvalidConfigException extends Exception
|
||||
{
|
||||
public function __construct(int $code = self::CONFIG_ERROR, string $message = '配置异常', mixed $extra = null, ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $extra, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class InvalidParamsException extends Exception
|
||||
{
|
||||
public function __construct(int $code = self::PARAMS_ERROR, string $message = '参数异常', mixed $extra = null, ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $extra, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class InvalidResponseException extends Exception
|
||||
{
|
||||
public mixed $response;
|
||||
|
||||
public function __construct(
|
||||
int $code = self::RESPONSE_ERROR,
|
||||
string $message = '响应异常',
|
||||
mixed $extra = null,
|
||||
?Throwable $previous = null
|
||||
) {
|
||||
$this->response = $extra;
|
||||
|
||||
parent::__construct($message, $code, $extra, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Exception;
|
||||
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Throwable;
|
||||
|
||||
class ServiceNotFoundException extends Exception implements NotFoundExceptionInterface
|
||||
{
|
||||
public function __construct(string $message = '服务未找到', int $code = self::CONTAINER_SERVICE_NOT_FOUND, mixed $extra = null, ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $extra, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful;
|
||||
|
||||
use Closure;
|
||||
use Yansongda\Artful\Contract\DirectionInterface;
|
||||
use Yansongda\Artful\Contract\PackerInterface;
|
||||
use Yansongda\Artful\Direction\NoHttpRequestDirection;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
use Yansongda\Artful\Exception\Exception;
|
||||
use Yansongda\Artful\Exception\InvalidParamsException;
|
||||
use Yansongda\Artful\Exception\ServiceNotFoundException;
|
||||
use Yansongda\Supports\Collection;
|
||||
use Yansongda\Supports\Str;
|
||||
|
||||
function should_do_http_request(string $direction): bool
|
||||
{
|
||||
return NoHttpRequestDirection::class !== $direction
|
||||
&& !in_array(NoHttpRequestDirection::class, class_parents($direction));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidParamsException
|
||||
*/
|
||||
function get_direction(mixed $direction): DirectionInterface
|
||||
{
|
||||
try {
|
||||
$direction = Artful::get($direction);
|
||||
|
||||
$direction = is_string($direction) ? Artful::get($direction) : $direction;
|
||||
} catch (ContainerException|ServiceNotFoundException) {
|
||||
}
|
||||
|
||||
if (!$direction instanceof DirectionInterface) {
|
||||
throw new InvalidParamsException(Exception::PARAMS_DIRECTION_INVALID, '参数异常: 配置的 `DirectionInterface` 未实现 `DirectionInterface`');
|
||||
}
|
||||
|
||||
return $direction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidParamsException
|
||||
*/
|
||||
function get_packer(mixed $packer): PackerInterface
|
||||
{
|
||||
try {
|
||||
$packer = Artful::get($packer);
|
||||
|
||||
$packer = is_string($packer) ? Artful::get($packer) : $packer;
|
||||
} catch (ContainerException|ServiceNotFoundException) {
|
||||
}
|
||||
|
||||
if (!$packer instanceof PackerInterface) {
|
||||
throw new InvalidParamsException(Exception::PARAMS_PACKER_INVALID, '参数异常: 配置的 `PackerInterface` 未实现 `PackerInterface`');
|
||||
}
|
||||
|
||||
return $packer;
|
||||
}
|
||||
|
||||
function filter_params(null|array|Collection $params, ?Closure $closure = null): Collection
|
||||
{
|
||||
$params = Collection::wrap($params);
|
||||
|
||||
return $params->filter(static fn ($v, $k) => !Str::startsWith($k, '_') && !is_null($v) && (empty($closure) || $closure($k, $v)));
|
||||
}
|
||||
|
||||
function get_radar_method(?Collection $payload): ?string
|
||||
{
|
||||
$string = $payload?->get('_method') ?? null;
|
||||
|
||||
if (is_null($string)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return strtoupper($string);
|
||||
}
|
||||
|
||||
function get_radar_url(?Collection $payload): ?string
|
||||
{
|
||||
return $payload?->get('_url') ?? null;
|
||||
}
|
||||
|
||||
function get_radar_body(?Collection $payload): mixed
|
||||
{
|
||||
return $payload?->get('_body') ?? null;
|
||||
}
|
||||
|
||||
function get_radar_headers(?Collection $payload): mixed
|
||||
{
|
||||
return $payload?->get('_headers') ?? null;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Yansongda\Artful\Contract\HttpClientInterface;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
use Yansongda\Artful\Exception\Exception;
|
||||
use Yansongda\Artful\Exception\InvalidParamsException;
|
||||
|
||||
class HttpClientFactory implements Contract\HttpClientFactoryInterface
|
||||
{
|
||||
public function __construct(private ContainerInterface $container) {}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws ContainerException
|
||||
* @throws InvalidParamsException
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function create(?array $options = []): ClientInterface
|
||||
{
|
||||
if ($this->container->has(HttpClientInterface::class)) {
|
||||
if (($http = $this->container->get(HttpClientInterface::class)) instanceof ClientInterface) {
|
||||
return $http;
|
||||
}
|
||||
|
||||
throw new InvalidParamsException(Exception::PARAMS_HTTP_CLIENT_INVALID, '参数异常: `HttpClient` 不符合 PSR 规范');
|
||||
}
|
||||
|
||||
return new Client($options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful;
|
||||
|
||||
use Yansongda\Artful\Contract\ConfigInterface;
|
||||
use Yansongda\Artful\Contract\LoggerInterface;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
use Yansongda\Artful\Exception\Exception;
|
||||
use Yansongda\Artful\Exception\InvalidParamsException;
|
||||
use Yansongda\Artful\Exception\ServiceNotFoundException;
|
||||
|
||||
/**
|
||||
* @method static void emergency($message, array $context = [])
|
||||
* @method static void alert($message, array $context = [])
|
||||
* @method static void critical($message, array $context = [])
|
||||
* @method static void error($message, array $context = [])
|
||||
* @method static void warning($message, array $context = [])
|
||||
* @method static void notice($message, array $context = [])
|
||||
* @method static void info($message, array $context = [])
|
||||
* @method static void debug($message, array $context = [])
|
||||
* @method static void log($message, array $context = [])
|
||||
*/
|
||||
class Logger
|
||||
{
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws ServiceNotFoundException
|
||||
* @throws InvalidParamsException
|
||||
*/
|
||||
public static function __callStatic(string $method, array $args): void
|
||||
{
|
||||
if (!Artful::hasContainer() || !Artful::has(LoggerInterface::class)
|
||||
|| false === Artful::get(ConfigInterface::class)->get('logger.enable', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$class = Artful::get(LoggerInterface::class);
|
||||
|
||||
if ($class instanceof \Psr\Log\LoggerInterface) {
|
||||
$class->{$method}(...$args);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw new InvalidParamsException(Exception::PARAMS_LOGGER_DRIVER_INVALID, '配置异常: 配置的 `LoggerInterface` 不符合 PSR 规范');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Packer;
|
||||
|
||||
use Yansongda\Artful\Contract\PackerInterface;
|
||||
use Yansongda\Supports\Arr;
|
||||
use Yansongda\Supports\Collection;
|
||||
|
||||
class JsonPacker implements PackerInterface
|
||||
{
|
||||
public function pack(null|array|Collection $payload, ?array $params = null): string
|
||||
{
|
||||
if (($payload instanceof Collection && $payload->isEmpty()) || empty($payload)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return Collection::wrap($payload)->toJson();
|
||||
}
|
||||
|
||||
public function unpack(string $payload, ?array $params = null): ?array
|
||||
{
|
||||
return Arr::wrapJson($payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Packer;
|
||||
|
||||
use Yansongda\Artful\Contract\PackerInterface;
|
||||
use Yansongda\Supports\Arr;
|
||||
use Yansongda\Supports\Collection;
|
||||
|
||||
class QueryPacker implements PackerInterface
|
||||
{
|
||||
public function pack(null|array|Collection $payload, ?array $params = null): string
|
||||
{
|
||||
return Collection::wrap($payload)->query();
|
||||
}
|
||||
|
||||
public function unpack(string $payload, ?array $params = null): array
|
||||
{
|
||||
return Arr::wrapQuery($payload, !empty($params['_unpack_raw']));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Packer;
|
||||
|
||||
use Yansongda\Artful\Contract\PackerInterface;
|
||||
use Yansongda\Supports\Arr;
|
||||
use Yansongda\Supports\Collection;
|
||||
|
||||
class XmlPacker implements PackerInterface
|
||||
{
|
||||
public function pack(null|array|Collection $payload, ?array $params = null): string
|
||||
{
|
||||
return Collection::wrap($payload)->toXml();
|
||||
}
|
||||
|
||||
public function unpack(string $payload, ?array $params = null): array
|
||||
{
|
||||
return Arr::wrapXml($payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Plugin;
|
||||
|
||||
use Closure;
|
||||
use Yansongda\Artful\Contract\PluginInterface;
|
||||
use Yansongda\Artful\Exception\InvalidParamsException;
|
||||
use Yansongda\Artful\Logger;
|
||||
use Yansongda\Artful\Rocket;
|
||||
|
||||
use function Yansongda\Artful\filter_params;
|
||||
use function Yansongda\Artful\get_packer;
|
||||
|
||||
class AddPayloadBodyPlugin implements PluginInterface
|
||||
{
|
||||
/**
|
||||
* @throws InvalidParamsException
|
||||
*/
|
||||
public function assembly(Rocket $rocket, Closure $next): Rocket
|
||||
{
|
||||
Logger::debug('[AddPayloadBodyPlugin] 插件开始装载', ['rocket' => $rocket]);
|
||||
|
||||
$packer = get_packer($rocket->getPacker());
|
||||
|
||||
$rocket->mergePayload(['_body' => $packer->pack(filter_params($rocket->getPayload()))]);
|
||||
|
||||
Logger::info('[AddPayloadBodyPlugin] 插件装载完毕', ['rocket' => $rocket]);
|
||||
|
||||
return $next($rocket);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Plugin;
|
||||
|
||||
use Closure;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Yansongda\Artful\Contract\PluginInterface;
|
||||
use Yansongda\Artful\Logger;
|
||||
use Yansongda\Artful\Rocket;
|
||||
|
||||
use function Yansongda\Artful\get_radar_body;
|
||||
use function Yansongda\Artful\get_radar_headers;
|
||||
use function Yansongda\Artful\get_radar_method;
|
||||
use function Yansongda\Artful\get_radar_url;
|
||||
|
||||
class AddRadarPlugin implements PluginInterface
|
||||
{
|
||||
public function assembly(Rocket $rocket, Closure $next): Rocket
|
||||
{
|
||||
Logger::debug('[AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]);
|
||||
|
||||
$payload = $rocket->getPayload();
|
||||
|
||||
$rocket->setRadar(new Request(
|
||||
get_radar_method($payload) ?? 'POST',
|
||||
get_radar_url($payload),
|
||||
get_radar_headers($payload) ?? $this->getHeaders(),
|
||||
get_radar_body($payload),
|
||||
));
|
||||
|
||||
Logger::info('[AddRadarPlugin] 插件装载完毕', ['rocket' => $rocket]);
|
||||
|
||||
return $next($rocket);
|
||||
}
|
||||
|
||||
protected function getHeaders(): array
|
||||
{
|
||||
return [
|
||||
'User-Agent' => 'yansongda/artful-v1',
|
||||
'Content-Type' => 'application/json;charset=utf-8',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Plugin;
|
||||
|
||||
use Closure;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Yansongda\Artful\Contract\PluginInterface;
|
||||
use Yansongda\Artful\Exception\Exception;
|
||||
use Yansongda\Artful\Exception\InvalidParamsException;
|
||||
use Yansongda\Artful\Logger;
|
||||
use Yansongda\Artful\Rocket;
|
||||
|
||||
use function Yansongda\Artful\get_direction;
|
||||
use function Yansongda\Artful\get_packer;
|
||||
|
||||
class ParserPlugin implements PluginInterface
|
||||
{
|
||||
/**
|
||||
* @throws InvalidParamsException
|
||||
*/
|
||||
public function assembly(Rocket $rocket, Closure $next): Rocket
|
||||
{
|
||||
/* @var Rocket $rocket */
|
||||
$rocket = $next($rocket);
|
||||
|
||||
Logger::debug('[ParserPlugin] 插件开始装载', ['rocket' => $rocket]);
|
||||
|
||||
$response = $rocket->getDestination();
|
||||
$direction = get_direction($rocket->getDirection());
|
||||
$packer = get_packer($rocket->getPacker());
|
||||
$payload = $rocket->getPayload();
|
||||
|
||||
if (!is_null($response) && !($response instanceof ResponseInterface)) {
|
||||
throw new InvalidParamsException(Exception::PARAMS_PARSER_DIRECTION_INVALID, '参数异常: 解析插件中 `Rocket` 的 `destination` 只能是 `null` 或者 `ResponseInterface`');
|
||||
}
|
||||
|
||||
$rocket->setDestination($direction->guide($packer, $response, $payload?->all() ?? []));
|
||||
|
||||
Logger::debug('[ParserPlugin] 插件装载完毕', ['rocket' => $rocket]);
|
||||
|
||||
return $rocket;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Plugin;
|
||||
|
||||
use Closure;
|
||||
use Yansongda\Artful\Contract\PluginInterface;
|
||||
use Yansongda\Artful\Logger;
|
||||
use Yansongda\Artful\Rocket;
|
||||
|
||||
class StartPlugin implements PluginInterface
|
||||
{
|
||||
public function assembly(Rocket $rocket, Closure $next): Rocket
|
||||
{
|
||||
Logger::debug('[StartPlugin] 插件开始装载', ['rocket' => $rocket]);
|
||||
|
||||
$rocket->mergePayload($rocket->getParams());
|
||||
|
||||
Logger::info('[StartPlugin] 插件装载完毕', ['rocket' => $rocket]);
|
||||
|
||||
return $next($rocket);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful;
|
||||
|
||||
use ArrayAccess;
|
||||
use JsonSerializable as JsonSerializableInterface;
|
||||
use Psr\Http\Message\MessageInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Yansongda\Artful\Contract\DirectionInterface;
|
||||
use Yansongda\Artful\Contract\PackerInterface;
|
||||
use Yansongda\Supports\Collection;
|
||||
use Yansongda\Supports\Traits\Accessable;
|
||||
use Yansongda\Supports\Traits\Arrayable;
|
||||
use Yansongda\Supports\Traits\Serializable;
|
||||
|
||||
class Rocket implements JsonSerializableInterface, ArrayAccess
|
||||
{
|
||||
use Accessable;
|
||||
use Arrayable;
|
||||
use Serializable;
|
||||
|
||||
private ?RequestInterface $radar = null;
|
||||
|
||||
private array $params = [];
|
||||
|
||||
private ?Collection $payload = null;
|
||||
|
||||
private string $packer = PackerInterface::class;
|
||||
|
||||
private string $direction = DirectionInterface::class;
|
||||
|
||||
private null|Collection|MessageInterface $destination = null;
|
||||
|
||||
private null|RequestInterface|ResponseInterface $destinationOrigin = null;
|
||||
|
||||
public function getRadar(): ?RequestInterface
|
||||
{
|
||||
return $this->radar;
|
||||
}
|
||||
|
||||
public function setRadar(?RequestInterface $radar): Rocket
|
||||
{
|
||||
$this->radar = $radar;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getParams(): array
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
public function setParams(array $params): Rocket
|
||||
{
|
||||
$this->params = $params;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function mergeParams(array $params): Rocket
|
||||
{
|
||||
$this->params = array_merge($this->params, $params);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPayload(): ?Collection
|
||||
{
|
||||
return $this->payload;
|
||||
}
|
||||
|
||||
public function setPayload(null|array|Collection $payload): Rocket
|
||||
{
|
||||
if (is_array($payload)) {
|
||||
$payload = new Collection($payload);
|
||||
}
|
||||
|
||||
$this->payload = $payload;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function mergePayload(array $payload): Rocket
|
||||
{
|
||||
if (empty($this->payload)) {
|
||||
$this->payload = new Collection();
|
||||
}
|
||||
|
||||
$this->payload = $this->payload->merge($payload);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function exceptPayload(mixed $key): Rocket
|
||||
{
|
||||
if (empty($this->payload)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->payload = $this->payload->except($key);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPacker(): string
|
||||
{
|
||||
return $this->packer;
|
||||
}
|
||||
|
||||
public function setPacker(string $packer): Rocket
|
||||
{
|
||||
$this->packer = $packer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDirection(): string
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
public function setDirection(string $direction): Rocket
|
||||
{
|
||||
$this->direction = $direction;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDestination(): null|Collection|MessageInterface
|
||||
{
|
||||
return $this->destination;
|
||||
}
|
||||
|
||||
public function setDestination(null|Collection|MessageInterface $destination): Rocket
|
||||
{
|
||||
$this->destination = $destination;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDestinationOrigin(): null|RequestInterface|ResponseInterface
|
||||
{
|
||||
return $this->destinationOrigin;
|
||||
}
|
||||
|
||||
public function setDestinationOrigin(null|RequestInterface|ResponseInterface $destinationOrigin): Rocket
|
||||
{
|
||||
$this->destinationOrigin = $destinationOrigin;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
$request = $this->getRadar();
|
||||
$destination = $this->getDestinationOrigin();
|
||||
|
||||
return [
|
||||
'radar' => [
|
||||
'url' => $request?->getUri()->__toString(),
|
||||
'method' => $request?->getMethod(),
|
||||
'headers' => $request?->getHeaders(),
|
||||
'body' => (string) $request?->getBody(),
|
||||
],
|
||||
'params' => $this->getParams(),
|
||||
'payload' => $this->getPayload()?->toArray(),
|
||||
'packer' => $this->getPacker(),
|
||||
'direction' => $this->getDirection(),
|
||||
'destination' => $this->getDestination(),
|
||||
'destination_origin' => [
|
||||
'status' => $destination instanceof ResponseInterface ? $destination->getStatusCode() : null,
|
||||
'headers' => $destination?->getHeaders(),
|
||||
'body' => (string) $destination?->getBody(),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Service;
|
||||
|
||||
use Yansongda\Artful\Artful;
|
||||
use Yansongda\Artful\Contract\ConfigInterface;
|
||||
use Yansongda\Artful\Contract\ServiceProviderInterface;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
use Yansongda\Supports\Config;
|
||||
|
||||
class ConfigServiceProvider implements ServiceProviderInterface
|
||||
{
|
||||
private array $config = [
|
||||
'logger' => [
|
||||
'enable' => false,
|
||||
'file' => null,
|
||||
'identify' => 'yansongda.artful',
|
||||
'level' => 'debug',
|
||||
'type' => 'daily',
|
||||
'max_files' => 30,
|
||||
],
|
||||
'http' => [
|
||||
'timeout' => 5.0,
|
||||
'connect_timeout' => 3.0,
|
||||
'headers' => [
|
||||
'User-Agent' => 'yansongda/artful-v1',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public function register(mixed $data = null): void
|
||||
{
|
||||
$config = new Config(array_replace_recursive($this->config, $data ?? []));
|
||||
|
||||
Artful::set(ConfigInterface::class, $config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Service;
|
||||
|
||||
use Closure;
|
||||
use Hyperf\Context\ApplicationContext as HyperfContainer;
|
||||
use Hyperf\Pimple\ContainerFactory as DefaultContainer;
|
||||
use Illuminate\Container\Container as LaravelContainer;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Yansongda\Artful\Artful;
|
||||
use Yansongda\Artful\Contract\ServiceProviderInterface;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
use Yansongda\Artful\Exception\ContainerNotFoundException;
|
||||
use Yansongda\Artful\Exception\Exception;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ContainerServiceProvider implements ServiceProviderInterface
|
||||
{
|
||||
private array $detectApplication = [
|
||||
'laravel' => LaravelContainer::class,
|
||||
'hyperf' => HyperfContainer::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public function register(mixed $data = null): void
|
||||
{
|
||||
if ($data instanceof ContainerInterface || $data instanceof Closure) {
|
||||
Artful::setContainer($data);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (Artful::hasContainer()) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->detectApplication as $framework => $application) {
|
||||
$method = $framework.'Application';
|
||||
|
||||
if (class_exists($application) && method_exists($this, $method) && $this->{$method}()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->defaultApplication();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws ContainerNotFoundException
|
||||
*/
|
||||
protected function laravelApplication(): bool
|
||||
{
|
||||
Artful::setContainer(static fn () => LaravelContainer::getInstance());
|
||||
|
||||
Artful::set(\Yansongda\Artful\Contract\ContainerInterface::class, LaravelContainer::getInstance());
|
||||
|
||||
if (!Artful::has(ContainerInterface::class)) {
|
||||
Artful::set(ContainerInterface::class, LaravelContainer::getInstance());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws ContainerNotFoundException
|
||||
*/
|
||||
protected function hyperfApplication(): bool
|
||||
{
|
||||
if (!HyperfContainer::hasContainer()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Artful::setContainer(static fn () => HyperfContainer::getContainer());
|
||||
|
||||
Artful::set(\Yansongda\Artful\Contract\ContainerInterface::class, HyperfContainer::getContainer());
|
||||
|
||||
if (!Artful::has(ContainerInterface::class)) {
|
||||
Artful::set(ContainerInterface::class, HyperfContainer::getContainer());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws ContainerNotFoundException
|
||||
*/
|
||||
protected function defaultApplication(): void
|
||||
{
|
||||
if (!class_exists(DefaultContainer::class)) {
|
||||
throw new ContainerNotFoundException('容器未找到: Init failed! Maybe you should install `hyperf/pimple` first', Exception::CONTAINER_NOT_FOUND);
|
||||
}
|
||||
|
||||
$container = (new DefaultContainer())();
|
||||
|
||||
Artful::setContainer($container);
|
||||
|
||||
Artful::set(\Yansongda\Artful\Contract\ContainerInterface::class, $container);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Service;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Yansongda\Artful\Artful;
|
||||
use Yansongda\Artful\Contract\EventDispatcherInterface;
|
||||
use Yansongda\Artful\Contract\ServiceProviderInterface;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
|
||||
class EventServiceProvider implements ServiceProviderInterface
|
||||
{
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public function register(mixed $data = null): void
|
||||
{
|
||||
if (class_exists(EventDispatcher::class)) {
|
||||
Artful::set(EventDispatcherInterface::class, new EventDispatcher());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Service;
|
||||
|
||||
use Yansongda\Artful\Artful;
|
||||
use Yansongda\Artful\Contract\HttpClientFactoryInterface;
|
||||
use Yansongda\Artful\Contract\ServiceProviderInterface;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
use Yansongda\Artful\HttpClientFactory;
|
||||
|
||||
class HttpServiceProvider implements ServiceProviderInterface
|
||||
{
|
||||
/**
|
||||
* @throws ContainerException
|
||||
*/
|
||||
public function register(mixed $data = null): void
|
||||
{
|
||||
$container = Artful::getContainer();
|
||||
|
||||
Artful::set(HttpClientFactoryInterface::class, new HttpClientFactory($container));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Artful\Service;
|
||||
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Handler\RotatingFileHandler;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
use Yansongda\Artful\Artful;
|
||||
use Yansongda\Artful\Contract\ConfigInterface;
|
||||
use Yansongda\Artful\Contract\LoggerInterface;
|
||||
use Yansongda\Artful\Contract\ServiceProviderInterface;
|
||||
use Yansongda\Artful\Exception\ContainerException;
|
||||
use Yansongda\Artful\Exception\ServiceNotFoundException;
|
||||
|
||||
class LoggerServiceProvider implements ServiceProviderInterface
|
||||
{
|
||||
protected array $config = [
|
||||
'enable' => false,
|
||||
'file' => null,
|
||||
'identify' => 'yansongda.artful',
|
||||
'level' => 'DEBUG',
|
||||
'type' => 'daily',
|
||||
'max_files' => 30,
|
||||
'formatter' => "%datetime% > %channel%.%level_name% > %message% %context% %extra%\n\n",
|
||||
];
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
public function register(mixed $data = null): void
|
||||
{
|
||||
/* @var ConfigInterface $config */
|
||||
$config = Artful::get(ConfigInterface::class);
|
||||
|
||||
$this->config = array_merge($this->config, $config->get('logger', []));
|
||||
|
||||
if (class_exists(Logger::class) && (true === ($this->config['enable'] ?? false))) {
|
||||
$logger = new Logger($this->config['identify']);
|
||||
|
||||
$logger->pushHandler($this->getDefaultHandler()
|
||||
->setFormatter($this->getDefaultFormatter()));
|
||||
|
||||
Artful::set(LoggerInterface::class, $logger);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getDefaultFormatter(): LineFormatter
|
||||
{
|
||||
return new LineFormatter(
|
||||
$this->config['formatter'],
|
||||
null,
|
||||
false,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
protected function getDefaultHandler(): AbstractProcessingHandler
|
||||
{
|
||||
$file = $this->config['file'] ?? (sys_get_temp_dir().'/logs/'.$this->config['identify'].'.log');
|
||||
|
||||
return match ($this->config['type']) {
|
||||
'single' => new StreamHandler($file, $this->config['level']),
|
||||
default => new RotatingFileHandler($file, $this->config['max_files'], $this->config['level']),
|
||||
};
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户