Last active
August 17, 2021 13:19
-
-
Save faytekin/140ddc39a6bfc16000c78bc970dc53f8 to your computer and use it in GitHub Desktop.
Soap Laravel Telescope Watcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use Laravel\Telescope\IncomingEntry; | |
use Laravel\Telescope\Telescope; | |
use Laravel\Telescope\Watchers\Watcher; | |
use RicorocksDigitalAgency\Soap\Facades\Soap; | |
use RicorocksDigitalAgency\Soap\Request\Request; | |
use RicorocksDigitalAgency\Soap\Response\Response; | |
class SoapWatcher extends Watcher | |
{ | |
public function register($app) | |
{ | |
Soap::afterRequesting( | |
function ($request, $response) { | |
if (! Telescope::isRecording()) { | |
return; | |
} | |
$this->handleRequest($request, $response); | |
} | |
); | |
} | |
public function handleRequest(Request $request, Response $response) | |
{ | |
Telescope::recordClientRequest(IncomingEntry::make([ | |
'method' => $request->getMethod(), | |
'uri' => $request->getEndpoint(), | |
'headers' => $this->parseHeader($response->trace()->responseHeaders), | |
'payload' => $request->getBody(), | |
'response_status' => $this->parseStatus($response->trace()->responseHeaders), | |
'response_headers' => $this->parseHeader($response->trace()->requestHeaders), | |
'response' => $response->response, | |
])); | |
} | |
private function parseHeader(?string $headers): ?array | |
{ | |
$parsed = []; | |
collect(preg_split("/\r\n|\n|\r/", $headers)) | |
->each(function ($string) use (&$parsed) { | |
$header = explode(': ', $string); | |
if (count($header) > 1) { | |
$parsed[head($header)] = last($header); | |
} | |
}); | |
return array_filter($parsed); | |
} | |
private function parseStatus(string $header): ?int | |
{ | |
$matches = []; | |
preg_match('/HTTP\/\d\.\d\s*\K[\d]+/', $header, $matches); | |
return head($matches); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can add
config/telescope.php
file\App\Services\SoapWatcher::class => env('TELESCOPE_CLIENT_SOAP_REQUEST_WATCHER', true),