Skip to content

Commit

Permalink
Doctrine: require 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 3, 2023
1 parent 79ad20b commit 5a088c9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 46 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"conflict": {
"nette/di": "<3.0.6",
"nette/schema": "<1.1.0"
"nette/schema": "<1.1.0",
"doctrine/event-manager": ">1.2.0"
},
"autoload": {
"psr-4": {
Expand Down
89 changes: 52 additions & 37 deletions src/Events/ContainerAwareEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public function __construct(Container $container)
$this->container = $container;
}

public function dispatchEvent(string $eventName, ?EventArgs $eventArgs = null): void
/**
* @param string $eventName
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function dispatchEvent($eventName, ?EventArgs $eventArgs = null): void
{
$eventArgs = $eventArgs ?? EventArgs::getEmptyInstance();

Expand All @@ -34,28 +38,60 @@ public function dispatchEvent(string $eventName, ?EventArgs $eventArgs = null):
}

/**
* @return array<object|string>
* @param string|null $event
* @return array<object|string>|array<string,array<object|string>>
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function getListeners(string $event): array
public function getListeners($event = null): array
{
return $this->getInitializedListeners($event);
if ($event !== null) {
return $this->getInitializedListeners($event);
}

$stack = [];

foreach ($this->listeners as $eventName => $listeners) {
$stack[$eventName] = $this->getInitializedListeners($eventName);
}

return $stack;
}

/**
* @return array<string, array<object|string>>
* @return array<object|string>|array<string,array<object|string>>
*/
public function getAllListeners(): array
{
$allListeners = [];
return $this->getListeners();
}

/**
* @return array<object|string>
*/
private function getInitializedListeners(string $event): array
{
$initialized = $this->initialized[$event] ?? false;

if ($initialized) {
return $this->listeners[$event] ?? [];
}

foreach ($this->listeners as $event => $listeners) {
$allListeners[$event] = $this->getListeners($event);
foreach ($this->listeners[$event] ?? [] as $hash => $listener) {
if (!is_object($listener)) {
$this->listeners[$event][$hash] = $this->container->getService($listener);
}
}

return $allListeners;
$this->initialized[$event] = true;

return $this->listeners[$event] ?? [];
}

public function hasListeners(string $event): bool
/**
* @param string $event
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function hasListeners($event): bool
{
return !empty($this->listeners[$event]);
}
Expand All @@ -64,7 +100,7 @@ public function hasListeners(string $event): bool
* Adds an event listener that listens on the specified events.
*
* @param string|string[] $events The event(s) to listen on.
* @param string|object $listener The listener object.
* @param string|object $listener The listener object.
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function addEventListener($events, $listener): void
Expand All @@ -90,14 +126,15 @@ public function addEventListener($events, $listener): void
/**
* Removes an event listener from the specified events.
*
* @param string|string[] $events
* @param string|object $listener
* @param string|string[] $events
* @param string|int|object $listener
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function removeEventListener($events, $listener): void
{
$hash = !is_object($listener) ? 'service@' . $listener : spl_object_hash($listener);

$hash = !is_object($listener)
? 'service@' . $listener
: spl_object_hash($listener);
foreach ((array) $events as $event) {
// Check if actually have this listener associated
if (isset($this->listeners[$event][$hash])) {
Expand All @@ -106,26 +143,4 @@ public function removeEventListener($events, $listener): void
}
}

/**
* @return array<object|string>
*/
private function getInitializedListeners(string $event): array
{
$initialized = $this->initialized[$event] ?? false;

if ($initialized) {
return $this->listeners[$event] ?? [];
}

foreach ($this->listeners[$event] ?? [] as $hash => $listener) {
if (!is_object($listener)) {
$this->listeners[$event][$hash] = $this->container->getService($listener);
}
}

$this->initialized[$event] = true;

return $this->listeners[$event] ?? [];
}

}
27 changes: 19 additions & 8 deletions src/Events/DebugEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,38 @@ public function __construct(EventManager $inner)
$this->inner = $inner;
}

public function dispatchEvent(string $eventName, ?EventArgs $eventArgs = null): void
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
* @param string $eventName
*/
public function dispatchEvent($eventName, ?EventArgs $eventArgs = null): void
{
$this->inner->dispatchEvent($eventName, $eventArgs);
}

/**
* @return array<object>
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
* @param string|NULL $event
* @return object[]|object[][]
*/
public function getListeners(string $event): array
public function getListeners($event = null): array
{
return $this->inner->getListeners($event);
}

/**
* @return array<string, array<object>>
* @return object[]|object[][]
*/
public function getAllListeners(): array
{
return $this->inner->getAllListeners();
return $this->getListeners();
}

public function hasListeners(string $event): bool
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
* @param string $event
*/
public function hasListeners($event): bool
{
return $this->inner->hasListeners($event);
}
Expand All @@ -58,10 +68,11 @@ public function addEventListener($events, $listener): void
/**
* Removes an event listener from the specified events.
*
* @param string|string[] $events
* @param string|string $events
* @param object $listener
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function removeEventListener($events, object $listener): void
public function removeEventListener($events, $listener): void
{
$this->inner->removeEventListener($events, $listener);
}
Expand Down

0 comments on commit 5a088c9

Please sign in to comment.