From ed6047d0b7b04eec827497e2e910765d1ec2d7eb Mon Sep 17 00:00:00 2001 From: Tomasz Kusy Date: Wed, 8 Sep 2021 15:22:45 +0200 Subject: [PATCH] Symfony >4.2 compatibility --- composer.json | 6 ++- src/DefinitionsReader.php | 51 +++++++++++++--------- src/Events/Binding/FaultEvent.php | 1 - src/Events/Binding/MessageEvent.php | 1 - src/Events/Binding/OperationEvent.php | 1 - src/Events/BindingEvent.php | 1 - src/Events/DefinitionsEvent.php | 1 - src/Events/Message/PartEvent.php | 1 - src/Events/MessageEvent.php | 1 - src/Events/PortType/FaultEvent.php | 1 - src/Events/PortType/OperationEvent.php | 1 - src/Events/PortType/ParamEvent.php | 1 - src/Events/PortTypeEvent.php | 1 - src/Events/ServiceEvent.php | 1 - src/Events/WsdlEvent.php | 60 ++++++++++++++++++-------- tests/EventsTest.php | 11 +++-- 16 files changed, 84 insertions(+), 56 deletions(-) diff --git a/composer.json b/composer.json index 4eb9dd6..ee7eccb 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,13 @@ "require": { "php": "^7.1|^8.0", "goetas-webservices/xsd-reader": "^0.3", - "symfony/event-dispatcher": "^2.4|^3.0|^4.0|^5.0" + "symfony/event-dispatcher-contracts": "^1.1|^2.0", + "symfony/event-dispatcher": "^2.4|^3.0|^4.0|^5.0", + "ext-dom": "*" }, "require-dev": { "phpunit/phpunit": "^8.0|^9.0", - "jmikola/wildcard-event-dispatcher": "~1.0" + "jmikola/wildcard-event-dispatcher": ">=1.1.2|^2.0" }, "autoload": { "psr-4": { diff --git a/src/DefinitionsReader.php b/src/DefinitionsReader.php index 34ea6b7..30b8106 100644 --- a/src/DefinitionsReader.php +++ b/src/DefinitionsReader.php @@ -33,9 +33,9 @@ use GoetasWebservices\XML\XSDReader\Schema\Schema; use GoetasWebservices\XML\XSDReader\SchemaReader; use GoetasWebservices\XML\XSDReader\Utils\UrlUtils; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventDispatcher; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class DefinitionsReader { @@ -54,14 +54,20 @@ class DefinitionsReader /** * - * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */ private $dispatcher; - public function __construct(SchemaReader $reader = null, EventDispatcherInterface $dispatcher = null) + public function __construct(SchemaReader $reader = null, $dispatcher = null) { $this->reader = $reader ?: new SchemaReader(); $this->dispatcher = $dispatcher ?: new EventDispatcher(); + if ( + !(is_subclass_of($this->dispatcher, EventDispatcherInterface::class)) + && class_exists('\Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy') + ) { + $this->dispatcher = LegacyEventDispatcherProxy::decorate($this->dispatcher); + } } /** @@ -92,16 +98,19 @@ private function loop(DOMElement $node) return $childs; } - private function dispatch($eventName, Event $event) + private function dispatch($eventName, $event) { - ; + if ($this->dispatcher instanceof \Psr\EventDispatcher\EventDispatcherInterface) { + return $this->dispatcher->dispatch($event, $eventName); + } + return $this->dispatcher->dispatch($eventName, $event); } /** * * @param Definitions $definitions * @param DOMElement $node - * @param Definitions $parent + * @param Definitions|null $parent * @return array */ private function rootNode(Definitions $definitions, DOMElement $node, Definitions $parent = null) @@ -141,13 +150,13 @@ private function rootNode(Definitions $definitions, DOMElement $node, Definition ksort($functions); return array( function () use ($functions, $definitions, $node) { - $this->dispatcher->dispatch('definitions_start', new DefinitionsEvent($definitions, $node)); + $this->dispatch('definitions_start', new DefinitionsEvent($definitions, $node)); foreach ($functions as $subFunctions) { foreach ($subFunctions as $function) { call_user_func($function); } } - $this->dispatcher->dispatch('definitions_end', new DefinitionsEvent($definitions, $node)); + $this->dispatch('definitions_end', new DefinitionsEvent($definitions, $node)); } ); } @@ -176,7 +185,7 @@ private function loadBinding(Definitions $definitions, DOMElement $node) list ($name, $ns) = self::splitParts($node, $node->getAttribute("type")); $binding->setType($definitions->findPortType($name, $ns)); - $this->dispatcher->dispatch('binding', new BindingEvent($binding, $node)); + $this->dispatch('binding', new BindingEvent($binding, $node)); foreach ($functions as $function) { call_user_func($function); } @@ -204,7 +213,7 @@ private function loadBindingOperation(Binding $binding, DOMElement $node) } } return function () use ($functions, $bindingOperation, $node) { - $this->dispatcher->dispatch('binding.operation', new BindingOperationEvent($bindingOperation, $node)); + $this->dispatch('binding.operation', new BindingOperationEvent($bindingOperation, $node)); foreach ($functions as $function) { call_user_func($function); } @@ -222,7 +231,7 @@ private function loadBindingOperationMessage(BindingOperation $bindingOperation, } return function () use ($message, $node, $isInput) { - $this->dispatcher->dispatch('binding.operation.message', new BindingOperationMessageEvent($message, $node, $isInput ? 'input' : 'output')); + $this->dispatch('binding.operation.message', new BindingOperationMessageEvent($message, $node, $isInput ? 'input' : 'output')); }; } @@ -233,7 +242,7 @@ private function loadBindingOperationFault(BindingOperation $bindingOperation, D $bindingOperation->addFault($fault); return function () use ($fault, $node) { - $this->dispatcher->dispatch('binding.operation.fault', new BindingOperationFaultEvent($fault, $node)); + $this->dispatch('binding.operation.fault', new BindingOperationFaultEvent($fault, $node)); }; } @@ -252,7 +261,7 @@ private function loadService(Definitions $definitions, DOMElement $node) } } return function () use ($functions, $service, $node) { - $this->dispatcher->dispatch('service', new ServiceEvent($service, $node)); + $this->dispatch('service', new ServiceEvent($service, $node)); foreach ($functions as $function) { call_user_func($function); } @@ -275,7 +284,7 @@ private function loadMessage(Definitions $definitions, DOMElement $node) } return function () use ($functions, $message, $node) { - $this->dispatcher->dispatch('message', new MessageEvent($message, $node)); + $this->dispatch('message', new MessageEvent($message, $node)); foreach ($functions as $function) { call_user_func($function); } @@ -291,7 +300,7 @@ private function loadPort(Service $service, DOMElement $node) list ($name, $ns) = self::splitParts($node, $node->getAttribute("binding")); $port->setBinding($service->getDefinition() ->findBinding($name, $ns)); - $this->dispatcher->dispatch('service.port', new PortEvent($port, $node)); + $this->dispatch('service.port', new PortEvent($port, $node)); }; } @@ -310,7 +319,7 @@ private function loadPortType(Definitions $definitions, DOMElement $node) } } return function () use ($functions, $port, $node) { - $this->dispatcher->dispatch('portType', new PortTypeEvent($port, $node)); + $this->dispatch('portType', new PortTypeEvent($port, $node)); foreach ($functions as $function) { call_user_func($function); } @@ -339,7 +348,7 @@ private function loadPortTypeOperation(PortType $port, DOMElement $node) } } return function () use ($functions, $operation, $node) { - $this->dispatcher->dispatch('portType.operation', new OperationEvent($operation, $node)); + $this->dispatch('portType.operation', new OperationEvent($operation, $node)); foreach ($functions as $function) { call_user_func($function); } @@ -361,7 +370,7 @@ private function loadParam(Operation $operation, DOMElement $node, $in = true) list ($name, $ns) = self::splitParts($node, $node->getAttribute("message")); $param->setMessage($operation->getDefinition() ->findMessage($name, $ns)); - $this->dispatcher->dispatch('portType.operation.param', new ParamEvent($param, $node)); + $this->dispatch('portType.operation.param', new ParamEvent($param, $node)); }; } @@ -375,7 +384,7 @@ private function loadFault(Operation $operation, DOMElement $node) list ($name, $ns) = self::splitParts($node, $node->getAttribute("message")); $fault->setMessage($operation->getDefinition()->findMessage($name, $ns)); - $this->dispatcher->dispatch('portType.operation.fault', new FaultEvent($fault, $node)); + $this->dispatch('portType.operation.fault', new FaultEvent($fault, $node)); }; } @@ -397,7 +406,7 @@ private function loadMessagePart(Message $message, DOMElement $node) ->getSchema() ->findType($name, $ns)); } - $this->dispatcher->dispatch('message.part', new MessagePartEvent($part, $node)); + $this->dispatch('message.part', new MessagePartEvent($part, $node)); }; } diff --git a/src/Events/Binding/FaultEvent.php b/src/Events/Binding/FaultEvent.php index 607e9ee..eb6e01d 100644 --- a/src/Events/Binding/FaultEvent.php +++ b/src/Events/Binding/FaultEvent.php @@ -3,7 +3,6 @@ use GoetasWebservices\XML\WSDLReader\Events\WsdlEvent; use GoetasWebservices\XML\WSDLReader\Wsdl\Binding\OperationFault; -use Symfony\Component\EventDispatcher\Event; class FaultEvent extends WsdlEvent { diff --git a/src/Events/Binding/MessageEvent.php b/src/Events/Binding/MessageEvent.php index 2eb8f38..9236424 100644 --- a/src/Events/Binding/MessageEvent.php +++ b/src/Events/Binding/MessageEvent.php @@ -3,7 +3,6 @@ use GoetasWebservices\XML\WSDLReader\Events\WsdlEvent; use GoetasWebservices\XML\WSDLReader\Wsdl\Binding\OperationMessage; -use Symfony\Component\EventDispatcher\Event; class MessageEvent extends WsdlEvent { diff --git a/src/Events/Binding/OperationEvent.php b/src/Events/Binding/OperationEvent.php index 494a479..f642f4e 100644 --- a/src/Events/Binding/OperationEvent.php +++ b/src/Events/Binding/OperationEvent.php @@ -3,7 +3,6 @@ use GoetasWebservices\XML\WSDLReader\Events\WsdlEvent; use GoetasWebservices\XML\WSDLReader\Wsdl\Binding\Operation; -use Symfony\Component\EventDispatcher\Event; class OperationEvent extends WsdlEvent { diff --git a/src/Events/BindingEvent.php b/src/Events/BindingEvent.php index eaf1840..92b6d9b 100644 --- a/src/Events/BindingEvent.php +++ b/src/Events/BindingEvent.php @@ -2,7 +2,6 @@ namespace GoetasWebservices\XML\WSDLReader\Events; use GoetasWebservices\XML\WSDLReader\Wsdl\Binding; -use Symfony\Component\EventDispatcher\Event; class BindingEvent extends WsdlEvent { diff --git a/src/Events/DefinitionsEvent.php b/src/Events/DefinitionsEvent.php index c3cd3a3..a375397 100644 --- a/src/Events/DefinitionsEvent.php +++ b/src/Events/DefinitionsEvent.php @@ -2,7 +2,6 @@ namespace GoetasWebservices\XML\WSDLReader\Events; use GoetasWebservices\XML\WSDLReader\Wsdl\Definitions; -use Symfony\Component\EventDispatcher\Event; class DefinitionsEvent extends WsdlEvent { diff --git a/src/Events/Message/PartEvent.php b/src/Events/Message/PartEvent.php index 2711fce..a073760 100644 --- a/src/Events/Message/PartEvent.php +++ b/src/Events/Message/PartEvent.php @@ -3,7 +3,6 @@ use GoetasWebservices\XML\WSDLReader\Events\WsdlEvent; use GoetasWebservices\XML\WSDLReader\Wsdl\Message\Part; -use Symfony\Component\EventDispatcher\Event; class PartEvent extends WsdlEvent { diff --git a/src/Events/MessageEvent.php b/src/Events/MessageEvent.php index 7396508..c1ed136 100644 --- a/src/Events/MessageEvent.php +++ b/src/Events/MessageEvent.php @@ -2,7 +2,6 @@ namespace GoetasWebservices\XML\WSDLReader\Events; use GoetasWebservices\XML\WSDLReader\Wsdl\Message; -use Symfony\Component\EventDispatcher\Event; class MessageEvent extends WsdlEvent { diff --git a/src/Events/PortType/FaultEvent.php b/src/Events/PortType/FaultEvent.php index 502f838..eca8b6d 100644 --- a/src/Events/PortType/FaultEvent.php +++ b/src/Events/PortType/FaultEvent.php @@ -3,7 +3,6 @@ use GoetasWebservices\XML\WSDLReader\Events\WsdlEvent; use GoetasWebservices\XML\WSDLReader\Wsdl\PortType\Fault; -use Symfony\Component\EventDispatcher\Event; class FaultEvent extends WsdlEvent { diff --git a/src/Events/PortType/OperationEvent.php b/src/Events/PortType/OperationEvent.php index b1af29e..27a65ab 100644 --- a/src/Events/PortType/OperationEvent.php +++ b/src/Events/PortType/OperationEvent.php @@ -3,7 +3,6 @@ use GoetasWebservices\XML\WSDLReader\Events\WsdlEvent; use GoetasWebservices\XML\WSDLReader\Wsdl\PortType\Operation; -use Symfony\Component\EventDispatcher\Event; class OperationEvent extends WsdlEvent { diff --git a/src/Events/PortType/ParamEvent.php b/src/Events/PortType/ParamEvent.php index 7baaf56..fc0d8cf 100644 --- a/src/Events/PortType/ParamEvent.php +++ b/src/Events/PortType/ParamEvent.php @@ -3,7 +3,6 @@ use GoetasWebservices\XML\WSDLReader\Events\WsdlEvent; use GoetasWebservices\XML\WSDLReader\Wsdl\PortType\Param; -use Symfony\Component\EventDispatcher\Event; class ParamEvent extends WsdlEvent { diff --git a/src/Events/PortTypeEvent.php b/src/Events/PortTypeEvent.php index b3f4c00..3664af4 100644 --- a/src/Events/PortTypeEvent.php +++ b/src/Events/PortTypeEvent.php @@ -2,7 +2,6 @@ namespace GoetasWebservices\XML\WSDLReader\Events; use GoetasWebservices\XML\WSDLReader\Wsdl\PortType; -use Symfony\Component\EventDispatcher\Event; class PortTypeEvent extends WsdlEvent { diff --git a/src/Events/ServiceEvent.php b/src/Events/ServiceEvent.php index b183159..247dfdb 100644 --- a/src/Events/ServiceEvent.php +++ b/src/Events/ServiceEvent.php @@ -2,7 +2,6 @@ namespace GoetasWebservices\XML\WSDLReader\Events; use GoetasWebservices\XML\WSDLReader\Wsdl\Service; -use Symfony\Component\EventDispatcher\Event; class ServiceEvent extends WsdlEvent { diff --git a/src/Events/WsdlEvent.php b/src/Events/WsdlEvent.php index 9f97788..3d78c63 100644 --- a/src/Events/WsdlEvent.php +++ b/src/Events/WsdlEvent.php @@ -1,26 +1,50 @@ node = $node; - } + /** + * + * @var \DOMElement + */ + protected $node; + + public function __construct(\DOMElement $node) + { + $this->node = $node; + } - /** - * @return \DOMElement - */ - public function getNode() + /** + * @return \DOMElement + */ + public function getNode() + { + return $this->node; + } + } +} else { + abstract class WsdlEvent extends \Symfony\Component\EventDispatcher\Event { - return $this->node; + /** + * + * @var \DOMElement + */ + protected $node; + + public function __construct(\DOMElement $node) + { + $this->node = $node; + } + + /** + * @return \DOMElement + */ + public function getNode() + { + return $this->node; + } } -} \ No newline at end of file +} diff --git a/tests/EventsTest.php b/tests/EventsTest.php index f3c3c27..f58588e 100644 --- a/tests/EventsTest.php +++ b/tests/EventsTest.php @@ -4,7 +4,6 @@ use GoetasWebservices\XML\WSDLReader\DefinitionsReader; use Jmikola\WildcardEventDispatcher\WildcardEventDispatcher; use PHPUnit\Framework\TestCase; -use Symfony\Component\EventDispatcher\Event; class EventsTest extends TestCase { @@ -30,7 +29,7 @@ public function setUp(): void public function testReadFile() { $events = array(); - $this->dispatcher->addListener("#", function (Event $e, $name) use (&$events) { + $this->dispatcher->addListener("#", function ($e, $name) use (&$events) { $events[] = [$e, $name]; }); $this->reader->readFile(__DIR__ . '/resources/base-wsdl-events.wsdl'); @@ -75,7 +74,13 @@ public function testReadFile() foreach ($expected as $index => $expectedData) { [$event, $name] = $events[$index]; - $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $event); + $this->assertThat( + $event, + $this->logicalOr( + $this->isInstanceOf('\Symfony\Component\EventDispatcher\Event'), + $this->isInstanceOf('\Symfony\Contracts\EventDispatcher\Event') + ) + ); $this->assertInstanceOf('GoetasWebservices\XML\WSDLReader\Events\WsdlEvent', $event); $this->assertInstanceOf($expectedData[1], $event, "Event name '$expectedData[0]'"); $this->assertEquals($expectedData[0], $name);