Skip to content

Commit

Permalink
Merge pull request #1308 from thephpleague/feature/league-event-3
Browse files Browse the repository at this point in the history
[UPGRADE] Upgrade league/event to version 3.
  • Loading branch information
Sephster authored Nov 30, 2023
2 parents 68cf217 + 4862516 commit 6406b66
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"php": "^8.0",
"ext-openssl": "*",
"league/event": "^2.2",
"league/event": "^3.0",
"league/uri": "^6.7 || ^7.0",
"lcobucci/jwt": "^4.3 || ^5.0",
"psr/http-message": "^1.0.1 || ^2.0",
Expand Down
6 changes: 3 additions & 3 deletions src/AuthorizationServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

use DateInterval;
use Defuse\Crypto\Key;
use League\Event\EmitterAwareInterface;
use League\Event\EmitterAwareTrait;
use League\OAuth2\Server\EventEmitting\EmitterAwareInterface;
use League\OAuth2\Server\EventEmitting\EmitterAwarePolyfill;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\GrantTypeInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
Expand All @@ -30,7 +30,7 @@

class AuthorizationServer implements EmitterAwareInterface
{
use EmitterAwareTrait;
use EmitterAwarePolyfill;

/**
* @var GrantTypeInterface[]
Expand Down
44 changes: 44 additions & 0 deletions src/EventEmitting/AbstractEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\EventEmitting;

use League\Event\HasEventName;
use Psr\EventDispatcher\StoppableEventInterface;

class AbstractEvent implements StoppableEventInterface, HasEventName
{
private bool $propagationStopped = false;

public function __construct(private string $name)
{
}

public function eventName(): string
{
return $this->name;
}

/**
* Backwards compatibility method
*
* @deprecated use eventName instead
*/
public function getName(): string
{
return $this->name;
}

public function isPropagationStopped(): bool
{
return $this->propagationStopped;
}

public function stopPropagation(): self
{
$this->propagationStopped = true;

return $this;
}
}
12 changes: 12 additions & 0 deletions src/EventEmitting/EmitterAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\EventEmitting;

interface EmitterAwareInterface
{
public function getEmitter(): EventEmitter;

public function setEmitter(EventEmitter $emitter): self;
}
35 changes: 35 additions & 0 deletions src/EventEmitting/EmitterAwarePolyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\EventEmitting;

use League\Event\ListenerRegistry;
use Psr\EventDispatcher\EventDispatcherInterface;

trait EmitterAwarePolyfill
{
private EventEmitter $emitter;

public function getEmitter(): EventEmitter
{
return $this->emitter ?? new EventEmitter();
}

public function setEmitter(EventEmitter $emitter): self
{
$this->emitter = $emitter;

return $this;
}

public function getEventDispatcher(): EventDispatcherInterface
{
return $this->getEmitter();
}

public function getListenerRegistry(): ListenerRegistry
{
return $this->getEmitter();
}
}
23 changes: 23 additions & 0 deletions src/EventEmitting/EventEmitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\EventEmitting;

use League\Event\EventDispatcher;
use League\Event\ListenerPriority;

final class EventEmitter extends EventDispatcher
{
public function addListener(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): self
{
$this->subscribeTo($event, $listener, $priority);

return $this;
}

public function emit(object $event): object
{
return $this->dispatch($event);
}
}
4 changes: 2 additions & 2 deletions src/Grant/AbstractGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
use DomainException;
use Error;
use Exception;
use League\Event\EmitterAwareTrait;
use League\OAuth2\Server\CryptKeyInterface;
use League\OAuth2\Server\CryptTrait;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\EventEmitting\EmitterAwarePolyfill;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
use League\OAuth2\Server\RedirectUriValidators\RedirectUriValidator;
Expand Down Expand Up @@ -60,7 +60,7 @@
*/
abstract class AbstractGrant implements GrantTypeInterface
{
use EmitterAwareTrait;
use EmitterAwarePolyfill;
use CryptTrait;

protected const SCOPE_DELIMITER_STRING = ' ';
Expand Down
2 changes: 1 addition & 1 deletion src/Grant/GrantTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

use DateInterval;
use Defuse\Crypto\Key;
use League\Event\EmitterAwareInterface;
use League\OAuth2\Server\CryptKeyInterface;
use League\OAuth2\Server\EventEmitting\EmitterAwareInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
Expand Down
4 changes: 2 additions & 2 deletions src/RequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

namespace League\OAuth2\Server;

use League\Event\Event;
use League\OAuth2\Server\EventEmitting\AbstractEvent;
use Psr\Http\Message\ServerRequestInterface;

class RequestEvent extends Event
class RequestEvent extends AbstractEvent
{
public const CLIENT_AUTHENTICATION_FAILED = 'client.authentication.failed';
public const USER_AUTHENTICATION_FAILED = 'user.authentication.failed';
Expand Down
8 changes: 4 additions & 4 deletions tests/Stubs/GrantType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use DateInterval;
use Defuse\Crypto\Key;
use League\Event\EmitterInterface;
use League\OAuth2\Server\CryptKeyInterface;
use League\OAuth2\Server\EventEmitting\EventEmitter;
use League\OAuth2\Server\Grant\GrantTypeInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
Expand All @@ -20,16 +20,16 @@

final class GrantType implements GrantTypeInterface
{
private ?EmitterInterface $emitter;
private EventEmitter $emitter;

public function setEmitter(EmitterInterface $emitter = null): self
public function setEmitter(EventEmitter $emitter = null): self
{
$this->emitter = $emitter;

return $this;
}

public function getEmitter(): ?EmitterInterface
public function getEmitter(): EventEmitter
{
return $this->emitter;
}
Expand Down

0 comments on commit 6406b66

Please sign in to comment.