From 605a8de3a5abc21f7dbf5d10d5c2f77754fb0e5b Mon Sep 17 00:00:00 2001 From: Markus Thielen Date: Mon, 17 Jun 2024 12:10:47 +0200 Subject: [PATCH] Introduce an optional PSR Eventdispatcher for Processor --- composer.json | 1 + src/CXml/Processor/Event/CXmlProcessEvent.php | 28 +++++++++++++++++++ src/CXml/Processor/Processor.php | 11 +++++++- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/CXml/Processor/Event/CXmlProcessEvent.php diff --git a/composer.json b/composer.json index b468d0d..9d92771 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "beberlei/assert": "@stable", "jms/serializer": "^3.28.0", "psr/log": "^1.0 || ^2.0 || ^3.0", + "psr/event-dispatcher": "^1.0.0", "doctrine/annotations": "@stable" }, "suggest": { diff --git a/src/CXml/Processor/Event/CXmlProcessEvent.php b/src/CXml/Processor/Event/CXmlProcessEvent.php new file mode 100644 index 0000000..90db811 --- /dev/null +++ b/src/CXml/Processor/Event/CXmlProcessEvent.php @@ -0,0 +1,28 @@ +cxml = $cxml; + $this->context = $context; + } + + public function getCxml(): CXml + { + return $this->cxml; + } + + public function getContext(): Context + { + return $this->context; + } +} diff --git a/src/CXml/Processor/Processor.php b/src/CXml/Processor/Processor.php index 7cd2662..561ac2d 100644 --- a/src/CXml/Processor/Processor.php +++ b/src/CXml/Processor/Processor.php @@ -21,7 +21,9 @@ use CXml\Model\Response\Response; use CXml\Model\Response\ResponsePayloadInterface; use CXml\Model\Status; +use CXml\Processor\Event\CXmlProcessEvent; use CXml\Processor\Exception\CXmlProcessException; +use Psr\EventDispatcher\EventDispatcherInterface; class Processor { @@ -108,15 +110,18 @@ class Processor private HeaderProcessor $headerProcessor; private HandlerRegistryInterface $handlerRegistry; private Builder $builder; + private ?EventDispatcherInterface $eventDispatcher; public function __construct( HeaderProcessor $requestProcessor, HandlerRegistryInterface $handlerRepository, - Builder $builder + Builder $builder, + EventDispatcherInterface $eventDispatcher = null ) { $this->headerProcessor = $requestProcessor; $this->handlerRegistry = $handlerRepository; $this->builder = $builder; + $this->eventDispatcher = $eventDispatcher; } /** @@ -127,6 +132,10 @@ public function process(CXml $cxml, Context $context = null): ?CXml $context ??= Context::create(); $context->setCXml($cxml); + if ($this->eventDispatcher) { + $this->eventDispatcher->dispatch(new CXmlProcessEvent($cxml, $context)); + } + $request = $cxml->getRequest(); if ($request) { return $this->processRequest($request, $context);