Skip to content

Commit

Permalink
Symfony >4.2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Kusy committed Sep 8, 2021
1 parent 61022b9 commit ed6047d
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 56 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
51 changes: 30 additions & 21 deletions src/DefinitionsReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
}
);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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'));
};
}

Expand All @@ -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));
};
}

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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));
};
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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));
};
}

Expand All @@ -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));
};
}

Expand All @@ -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));
};
}

Expand Down
1 change: 0 additions & 1 deletion src/Events/Binding/FaultEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/Binding/MessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/Binding/OperationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/BindingEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace GoetasWebservices\XML\WSDLReader\Events;

use GoetasWebservices\XML\WSDLReader\Wsdl\Binding;
use Symfony\Component\EventDispatcher\Event;

class BindingEvent extends WsdlEvent
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/DefinitionsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace GoetasWebservices\XML\WSDLReader\Events;

use GoetasWebservices\XML\WSDLReader\Wsdl\Definitions;
use Symfony\Component\EventDispatcher\Event;

class DefinitionsEvent extends WsdlEvent
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/Message/PartEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/MessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace GoetasWebservices\XML\WSDLReader\Events;

use GoetasWebservices\XML\WSDLReader\Wsdl\Message;
use Symfony\Component\EventDispatcher\Event;

class MessageEvent extends WsdlEvent
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/PortType/FaultEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/PortType/OperationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/PortType/ParamEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/PortTypeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace GoetasWebservices\XML\WSDLReader\Events;

use GoetasWebservices\XML\WSDLReader\Wsdl\PortType;
use Symfony\Component\EventDispatcher\Event;

class PortTypeEvent extends WsdlEvent
{
Expand Down
1 change: 0 additions & 1 deletion src/Events/ServiceEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace GoetasWebservices\XML\WSDLReader\Events;

use GoetasWebservices\XML\WSDLReader\Wsdl\Service;
use Symfony\Component\EventDispatcher\Event;

class ServiceEvent extends WsdlEvent
{
Expand Down
60 changes: 42 additions & 18 deletions src/Events/WsdlEvent.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
<?php
namespace GoetasWebservices\XML\WSDLReader\Events;

use Symfony\Component\EventDispatcher\Event;
use Psr\EventDispatcher\EventDispatcherInterface as PsrEventDispatcherInterface;

abstract class WsdlEvent extends Event
{
/**
*
* @var \DOMElement
*/
protected $node;

public function __construct(\DOMElement $node)
if (interface_exists(PsrEventDispatcherInterface::class)) {
abstract class WsdlEvent extends \Symfony\Contracts\EventDispatcher\Event
{
$this->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;
}
}
}
}
11 changes: 8 additions & 3 deletions tests/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ed6047d

Please sign in to comment.