AddRadarPlugin.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Artful\Plugin;
  4. use Closure;
  5. use GuzzleHttp\Psr7\Request;
  6. use Yansongda\Artful\Contract\PluginInterface;
  7. use Yansongda\Artful\Logger;
  8. use Yansongda\Artful\Rocket;
  9. use function Yansongda\Artful\get_radar_body;
  10. use function Yansongda\Artful\get_radar_headers;
  11. use function Yansongda\Artful\get_radar_method;
  12. use function Yansongda\Artful\get_radar_url;
  13. class AddRadarPlugin implements PluginInterface
  14. {
  15. public function assembly(Rocket $rocket, Closure $next): Rocket
  16. {
  17. Logger::debug('[AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]);
  18. $payload = $rocket->getPayload();
  19. $rocket->setRadar(new Request(
  20. get_radar_method($payload) ?? 'POST',
  21. get_radar_url($payload),
  22. get_radar_headers($payload) ?? $this->getHeaders(),
  23. get_radar_body($payload),
  24. ));
  25. Logger::info('[AddRadarPlugin] 插件装载完毕', ['rocket' => $rocket]);
  26. return $next($rocket);
  27. }
  28. protected function getHeaders(): array
  29. {
  30. return [
  31. 'User-Agent' => 'yansongda/artful-v1',
  32. 'Content-Type' => 'application/json;charset=utf-8',
  33. ];
  34. }
  35. }