diff --git a/src/Wsdl/Event/RequestFailEvent.php b/src/Wsdl/Event/RequestFailEvent.php index 65effcc..579d994 100644 --- a/src/Wsdl/Event/RequestFailEvent.php +++ b/src/Wsdl/Event/RequestFailEvent.php @@ -46,15 +46,23 @@ class RequestFailEvent implements Serializable */ private $exceptionString; + /** + * @var array> Zasobnik volanych funkci + */ + private $trace; + + /** * @param string $fname Nazev volane funkce * @param array $args Argumenty pozadavku + * @param array> $trace Zasobnik volanych funkci */ public function __construct( string $fname, array $args, Throwable $throwable, - float $duration + float $duration, + array $trace ) { $this->fname = $fname; $this->args = $args; @@ -62,6 +70,7 @@ public function __construct( $this->exceptionClass = get_class($throwable); $this->exceptionString = (string) $throwable; $this->time = $duration; + $this->trace = $trace; } /** @@ -74,6 +83,7 @@ public function __serialize(): array { 'time' => $this->time, 'exception_class' => $this->exceptionClass, 'exception_string' => $this->exceptionString, + 'trace' => $this->trace, ]; } @@ -91,6 +101,7 @@ public function __unserialize(array $data): void { $this->time = (float) $data['time']; $this->exceptionClass = (string) $data['exception_class']; $this->exceptionString = (string) $data['exception_string']; + $this->trace = (array) $data['trace']; } /** @@ -162,4 +173,11 @@ public function getThrowable(): ?Throwable return $this->throwable; } + /** + * @return array> + */ + public function getTrace(): array + { + return $this->trace; + } } diff --git a/src/Wsdl/Event/RequestPostEvent.php b/src/Wsdl/Event/RequestPostEvent.php index af28cc7..f00b9b9 100644 --- a/src/Wsdl/Event/RequestPostEvent.php +++ b/src/Wsdl/Event/RequestPostEvent.php @@ -32,21 +32,30 @@ class RequestPostEvent implements Serializable */ private $result; + /** + * @var array> Zasobnik volanych funkci + */ + private $trace; + + /** * @param string $fname Nazev volane funkce * @param array $args Argumenty pozadavku * @param array|stdClass|null $result + * @param array> $trace Zasobnik volanych funkci */ public function __construct( string $fname, array $args, $result, - float $duration + float $duration, + array $trace ) { $this->fname = $fname; $this->args = $args; $this->result = $result; $this->time = $duration; + $this->trace = $trace; } /** @@ -59,6 +68,7 @@ public function __serialize(): array 'args' => $this->args, 'time' => $this->time, 'result' => $this->result, + 'trace' => $this->trace, ]; } @@ -76,6 +86,7 @@ public function __unserialize(array $data): void $this->args = (array) $data['args']; $this->time = (float) $data['time']; $this->result = (array) $data['result']; + $this->trace = (array) $data['trace']; } /** @@ -116,4 +127,11 @@ public function getResult() return $this->result; } + /** + * @return array> + */ + public function getTrace(): array + { + return $this->trace; + } } diff --git a/src/Wsdl/WebService.php b/src/Wsdl/WebService.php index b1ef0d1..aba0135 100644 --- a/src/Wsdl/WebService.php +++ b/src/Wsdl/WebService.php @@ -88,9 +88,11 @@ protected function soapCall( ) { $fname = ucfirst($functionName); $args = $this->prepareArgs($fname, $arguments); + $trace = []; if ($this->eventDispatcher !== null) { - $event = new RequestPreEvent($fname, $args, $options, $inputHeaders, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $event = new RequestPreEvent($fname, $args, $options, $inputHeaders, $trace); $this->eventDispatcher->dispatch($event); } @@ -101,7 +103,7 @@ protected function soapCall( if ($this->eventDispatcher !== null) { $duration = microtime(true) - $requestStart; - $event = new RequestPostEvent($fname, $args, $soapResponse, $duration); + $event = new RequestPostEvent($fname, $args, $soapResponse, $duration, $trace); $this->eventDispatcher->dispatch($event); } @@ -110,7 +112,7 @@ protected function soapCall( if ($this->eventDispatcher !== null) { $duration = microtime(true) - $requestStart; - $event = new RequestFailEvent($fname, $args, $t, $duration); + $event = new RequestFailEvent($fname, $args, $t, $duration, $trace); $this->eventDispatcher->dispatch($event); } diff --git a/src/Wsdl/WebServiceFactory.php b/src/Wsdl/WebServiceFactory.php index 530ef1a..55cab69 100644 --- a/src/Wsdl/WebServiceFactory.php +++ b/src/Wsdl/WebServiceFactory.php @@ -47,4 +47,16 @@ public function createWebService(string $url, array $options): WebServiceInterfa $soapClient = new \SoapClient($url, $options); return new $this->class($soapClient, $options, $this->eventDispatcher); } + + /** + * @inheritdoc + */ + public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void + { + if ($this->eventDispatcher != null) { + throw new InvalidArgumentException("Event dispatcher is already set."); + } + + $this->eventDispatcher = $eventDispatcher; + } } diff --git a/src/Wsdl/WebServiceFactoryInterface.php b/src/Wsdl/WebServiceFactoryInterface.php index d139b07..884744b 100644 --- a/src/Wsdl/WebServiceFactoryInterface.php +++ b/src/Wsdl/WebServiceFactoryInterface.php @@ -3,6 +3,8 @@ namespace Skaut\Skautis\Wsdl; +use Psr\EventDispatcher\EventDispatcherInterface; + /** * Interface továrny pro vytváření objektů webových služeb */ @@ -18,4 +20,12 @@ interface WebServiceFactoryInterface * @return WebServiceInterface */ public function createWebService(string $url, array $options): WebServiceInterface; + + /** + * Nastaví event dispatcher, pokud uz neni nastaven. + * + * @param EventDispatcherInterface $eventDispatcher + * @return void + */ + public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void; } diff --git a/src/Wsdl/WsdlManager.php b/src/Wsdl/WsdlManager.php index c50186e..a4a39cb 100644 --- a/src/Wsdl/WsdlManager.php +++ b/src/Wsdl/WsdlManager.php @@ -3,6 +3,7 @@ namespace Skaut\Skautis\Wsdl; +use Psr\EventDispatcher\EventDispatcherInterface; use Skaut\Skautis\Config; use Skaut\Skautis\User; @@ -75,6 +76,13 @@ public function createWebService(string $name, array $options = []): WebServiceI return $this->webServiceFactory->createWebService($this->getWebServiceUrl($name), $options); } + /** + * Nastaví event dispatcher. + */ + public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void { + $this->webServiceFactory->setEventDispatcher($eventDispatcher); + } + /** * Vrací URL webové služby podle jejího jména */ diff --git a/tests/Unit/Wsdl/Event/RequestFailEventTest.php b/tests/Unit/Wsdl/Event/RequestFailEventTest.php index 41f08b4..35e697a 100644 --- a/tests/Unit/Wsdl/Event/RequestFailEventTest.php +++ b/tests/Unit/Wsdl/Event/RequestFailEventTest.php @@ -17,8 +17,9 @@ class RequestFailEventTest extends TestCase public function testExceptionMessage(): void { $throwable = new RuntimeException('my message'); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - $event = new RequestFailEvent('asd', [], $throwable, 30); + $event = new RequestFailEvent('asd', [], $throwable, 30, $trace); $this->assertStringContainsString('my message', $event->getExceptionString()); $this->assertSame(RuntimeException::class, $event->getExceptionClass()); } @@ -31,8 +32,9 @@ public function testDeserialization(): void 'argument' => 'value', ], ]; + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - $event = new RequestFailEvent('asd', $args, $throwable, 30.22); + $event = new RequestFailEvent('asd', $args, $throwable, 30.22, $trace); $serialized = serialize($event); /** @var RequestFailEvent $unserialized */ @@ -56,8 +58,9 @@ public function testRepeatedSerializationDeserialization(): void 'argument' => 'value', ], ]; + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - $event = new RequestFailEvent('asd', $args, $throwable, 30.22); + $event = new RequestFailEvent('asd', $args, $throwable, 30.22, $trace); $serialized = serialize($event); $unserialized = unserialize($serialized); diff --git a/tests/Unit/Wsdl/Event/RequestPostEventTest.php b/tests/Unit/Wsdl/Event/RequestPostEventTest.php index 351cbfa..b8a1959 100644 --- a/tests/Unit/Wsdl/Event/RequestPostEventTest.php +++ b/tests/Unit/Wsdl/Event/RequestPostEventTest.php @@ -21,8 +21,9 @@ public function testDeserialize(): void ]; $result = [(object)['a' => 'b']]; $duration = 11.11; + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - $event = new RequestPostEvent('asd', $args, $result, $duration); + $event = new RequestPostEvent('asd', $args, $result, $duration, $trace); $serialized = serialize($event); /** @var RequestPostEvent $unserialized */