From a101898830487dd3c7683fdf0137f8740a400cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= Date: Sun, 10 Apr 2022 14:52:04 +0200 Subject: [PATCH 1/5] event dispatcher setter --- src/Wsdl/WebServiceFactory.php | 8 ++++++++ src/Wsdl/WebServiceFactoryInterface.php | 10 ++++++++++ src/Wsdl/WsdlManager.php | 8 ++++++++ 3 files changed, 26 insertions(+) diff --git a/src/Wsdl/WebServiceFactory.php b/src/Wsdl/WebServiceFactory.php index 530ef1a..d8169c4 100644 --- a/src/Wsdl/WebServiceFactory.php +++ b/src/Wsdl/WebServiceFactory.php @@ -47,4 +47,12 @@ 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 + { + $this->eventDispatcher = $eventDispatcher; + } } diff --git a/src/Wsdl/WebServiceFactoryInterface.php b/src/Wsdl/WebServiceFactoryInterface.php index d139b07..f35d546 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. + * + * @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 */ From add268a15b1407bd99a27f7d2635faaf644d0620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= Date: Sun, 10 Apr 2022 19:16:23 +0200 Subject: [PATCH 2/5] do not allow change event dispatcher --- src/Wsdl/WebServiceFactory.php | 4 ++++ src/Wsdl/WebServiceFactoryInterface.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Wsdl/WebServiceFactory.php b/src/Wsdl/WebServiceFactory.php index d8169c4..55cab69 100644 --- a/src/Wsdl/WebServiceFactory.php +++ b/src/Wsdl/WebServiceFactory.php @@ -53,6 +53,10 @@ public function createWebService(string $url, array $options): WebServiceInterfa */ 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 f35d546..884744b 100644 --- a/src/Wsdl/WebServiceFactoryInterface.php +++ b/src/Wsdl/WebServiceFactoryInterface.php @@ -22,7 +22,7 @@ interface WebServiceFactoryInterface public function createWebService(string $url, array $options): WebServiceInterface; /** - * Nastaví event dispatcher. + * Nastaví event dispatcher, pokud uz neni nastaven. * * @param EventDispatcherInterface $eventDispatcher * @return void From 601a4bedcea96cb5baddd10c0292c11a51871f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= Date: Sun, 10 Apr 2022 19:44:41 +0200 Subject: [PATCH 3/5] trace added to PostEvent and FailEvent --- src/Wsdl/Event/RequestFailEvent.php | 17 ++++++++++++++++- src/Wsdl/Event/RequestPostEvent.php | 17 ++++++++++++++++- src/Wsdl/WebService.php | 8 +++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/Wsdl/Event/RequestFailEvent.php b/src/Wsdl/Event/RequestFailEvent.php index 65effcc..a6c2fb0 100644 --- a/src/Wsdl/Event/RequestFailEvent.php +++ b/src/Wsdl/Event/RequestFailEvent.php @@ -46,6 +46,12 @@ class RequestFailEvent implements Serializable */ private $exceptionString; + /** + * @var array> Zasobnik volanych funkci + */ + private $trace; + + /** * @param string $fname Nazev volane funkce * @param array $args Argumenty pozadavku @@ -54,7 +60,8 @@ public function __construct( string $fname, array $args, Throwable $throwable, - float $duration + float $duration, + array $trace ) { $this->fname = $fname; $this->args = $args; @@ -62,6 +69,7 @@ public function __construct( $this->exceptionClass = get_class($throwable); $this->exceptionString = (string) $throwable; $this->time = $duration; + $this->trace = $trace; } /** @@ -162,4 +170,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..7792685 100644 --- a/src/Wsdl/Event/RequestPostEvent.php +++ b/src/Wsdl/Event/RequestPostEvent.php @@ -32,6 +32,12 @@ class RequestPostEvent implements Serializable */ private $result; + /** + * @var array> Zasobnik volanych funkci + */ + private $trace; + + /** * @param string $fname Nazev volane funkce * @param array $args Argumenty pozadavku @@ -41,12 +47,14 @@ 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; } /** @@ -116,4 +124,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..da83e1d 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 = null; 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); } From 558005695251429d6c53ee88e3abd321062b9584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= Date: Sun, 10 Apr 2022 19:53:49 +0200 Subject: [PATCH 4/5] phpstan fix, tests fix --- src/Wsdl/Event/RequestFailEvent.php | 1 + src/Wsdl/Event/RequestPostEvent.php | 1 + src/Wsdl/WebService.php | 2 +- tests/Unit/Wsdl/Event/RequestFailEventTest.php | 9 ++++++--- tests/Unit/Wsdl/Event/RequestPostEventTest.php | 3 ++- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Wsdl/Event/RequestFailEvent.php b/src/Wsdl/Event/RequestFailEvent.php index a6c2fb0..915d617 100644 --- a/src/Wsdl/Event/RequestFailEvent.php +++ b/src/Wsdl/Event/RequestFailEvent.php @@ -55,6 +55,7 @@ class RequestFailEvent implements Serializable /** * @param string $fname Nazev volane funkce * @param array $args Argumenty pozadavku + * @param array> $trace Zasobnik volanych funkci */ public function __construct( string $fname, diff --git a/src/Wsdl/Event/RequestPostEvent.php b/src/Wsdl/Event/RequestPostEvent.php index 7792685..bab34d8 100644 --- a/src/Wsdl/Event/RequestPostEvent.php +++ b/src/Wsdl/Event/RequestPostEvent.php @@ -42,6 +42,7 @@ class RequestPostEvent implements Serializable * @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, diff --git a/src/Wsdl/WebService.php b/src/Wsdl/WebService.php index da83e1d..aba0135 100644 --- a/src/Wsdl/WebService.php +++ b/src/Wsdl/WebService.php @@ -88,7 +88,7 @@ protected function soapCall( ) { $fname = ucfirst($functionName); $args = $this->prepareArgs($fname, $arguments); - $trace = null; + $trace = []; if ($this->eventDispatcher !== null) { $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); 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 */ From 449faf0fed1348c171f68a41e1ad8685383f4a38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= Date: Sun, 10 Apr 2022 19:59:54 +0200 Subject: [PATCH 5/5] serialize trace --- src/Wsdl/Event/RequestFailEvent.php | 2 ++ src/Wsdl/Event/RequestPostEvent.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Wsdl/Event/RequestFailEvent.php b/src/Wsdl/Event/RequestFailEvent.php index 915d617..579d994 100644 --- a/src/Wsdl/Event/RequestFailEvent.php +++ b/src/Wsdl/Event/RequestFailEvent.php @@ -83,6 +83,7 @@ public function __serialize(): array { 'time' => $this->time, 'exception_class' => $this->exceptionClass, 'exception_string' => $this->exceptionString, + 'trace' => $this->trace, ]; } @@ -100,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']; } /** diff --git a/src/Wsdl/Event/RequestPostEvent.php b/src/Wsdl/Event/RequestPostEvent.php index bab34d8..f00b9b9 100644 --- a/src/Wsdl/Event/RequestPostEvent.php +++ b/src/Wsdl/Event/RequestPostEvent.php @@ -68,6 +68,7 @@ public function __serialize(): array 'args' => $this->args, 'time' => $this->time, 'result' => $this->result, + 'trace' => $this->trace, ]; } @@ -85,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']; } /**