-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Application default listeners to dedicated listener provider
Use double-dispatch approach to ensure default listeners are registered. Signed-off-by: Aleksei Khudiakov <[email protected]>
- Loading branch information
Showing
6 changed files
with
401 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Mvc; | ||
|
||
use Laminas\EventManager\ListenerAggregateInterface; | ||
use Laminas\Mvc\Exception\DomainException; | ||
use Psr\Container\ContainerInterface; | ||
|
||
use function array_merge; | ||
use function array_unique; | ||
use function get_debug_type; | ||
use function is_string; | ||
use function sprintf; | ||
|
||
use const SORT_REGULAR; | ||
|
||
/** | ||
* Provides lazy container | ||
*/ | ||
final class ApplicationListenersProvider | ||
{ | ||
public const DEFAULT_LISTENERS = [ | ||
'RouteListener', | ||
'DispatchListener', | ||
'HttpMethodListener', | ||
'ViewManager', | ||
'SendResponseListener', | ||
]; | ||
|
||
/** | ||
* @param array<string|ListenerAggregateInterface> $listeners | ||
*/ | ||
private function __construct(private readonly ContainerInterface $container, private readonly array $listeners) | ||
{ | ||
} | ||
|
||
/** | ||
* @param array<string|ListenerAggregateInterface> $extraListeners | ||
*/ | ||
public static function withDefaultListeners(ContainerInterface $container, array $extraListeners): self | ||
{ | ||
return new self( | ||
$container, | ||
array_unique(array_merge(self::DEFAULT_LISTENERS, $extraListeners), SORT_REGULAR) | ||
); | ||
} | ||
|
||
/** | ||
* @param array<string|ListenerAggregateInterface> $extraListeners | ||
*/ | ||
public static function withoutDefaultListeners(ContainerInterface $container, array $extraListeners): self | ||
{ | ||
return new self( | ||
$container, | ||
array_unique($extraListeners, SORT_REGULAR) | ||
); | ||
} | ||
|
||
/** | ||
* @return array<string|ListenerAggregateInterface> | ||
*/ | ||
public function getListeners(): array | ||
{ | ||
return $this->listeners; | ||
} | ||
|
||
public function registerListeners(Application $application): void | ||
{ | ||
$events = $application->getEventManager(); | ||
foreach ($this->listeners as $listener) { | ||
$msg = ''; | ||
if (is_string($listener)) { | ||
$msg = sprintf(' with container id "%s"', $listener); | ||
/** @var mixed $listener */ | ||
$listener = $this->container->get($listener); | ||
} | ||
if (! $listener instanceof ListenerAggregateInterface) { | ||
throw new DomainException(sprintf( | ||
'Application listener%s expected to be instance of %s, %s given', | ||
$msg, | ||
ListenerAggregateInterface::class, | ||
get_debug_type($listener) | ||
)); | ||
} | ||
|
||
$listener->attach($events); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Mvc\Service; | ||
|
||
use Laminas\Mvc\Application; | ||
use Laminas\Mvc\ApplicationListenersProvider; | ||
use Psr\Container\ContainerInterface; | ||
|
||
use function assert; | ||
use function is_array; | ||
|
||
final class ApplicationListenersProviderFactory | ||
{ | ||
/** | ||
* For default listeners @see ApplicationListenersProvider::DEFAULT_LISTENERS | ||
* | ||
* Extra listeners could be specified via configuration at `$config[Application::class]['listeners']` | ||
* or overridden via delegator factory. | ||
*/ | ||
public function __invoke(ContainerInterface $container): ApplicationListenersProvider | ||
{ | ||
$config = $container->get('config'); | ||
assert(is_array($config)); | ||
|
||
/** @psalm-var list<string> $listeners */ | ||
$listeners = $config[Application::class]['listeners'] ?? []; | ||
return ApplicationListenersProvider::withDefaultListeners($container, $listeners); | ||
} | ||
} |
Oops, something went wrong.