Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract default listeners from Application #161

Open
wants to merge 4 commits into
base: 4.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 1 addition & 52 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
<code><![CDATA[$r]]></code>
</MissingClosureParamType>
<MixedArgument>
<code><![CDATA[$listener]]></code>
<code><![CDATA[$serviceManager->get('Router')]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$listener]]></code>
<code><![CDATA[$response]]></code>
<code><![CDATA[$response]]></code>
<code><![CDATA[$this->request]]></code>
Expand All @@ -18,9 +16,6 @@
<MixedInferredReturnType>
<code><![CDATA[array|object]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[attach]]></code>
</MixedMethodCall>
<MixedReturnStatement>
<code><![CDATA[$this->serviceManager->get('config')]]></code>
</MixedReturnStatement>
Expand Down Expand Up @@ -849,18 +844,9 @@
<code><![CDATA[$container]]></code>
</ArgumentTypeCoercion>
<MixedArgument>
<code><![CDATA[$container->get('EventManager')]]></code>
<code><![CDATA[$container->get('Request')]]></code>
<code><![CDATA[$container->get('Response')]]></code>
<code><![CDATA[$listener]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$listener]]></code>
<code><![CDATA[$listeners]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[attach]]></code>
</MixedMethodCall>
</file>
<file src="src/Service/ControllerManagerFactory.php">
<MixedArgument>
Expand Down Expand Up @@ -1281,9 +1267,6 @@
</UnusedClosureParam>
</file>
<file src="test/Application/BadControllerTrait.php">
<ArgumentTypeCoercion>
<code><![CDATA[$serviceConfig]]></code>
</ArgumentTypeCoercion>
<MissingClosureParamType>
<code><![CDATA[$services]]></code>
<code><![CDATA[$services]]></code>
Expand Down Expand Up @@ -1401,16 +1384,10 @@
</MixedOperand>
</file>
<file src="test/Application/InvalidControllerTypeTrait.php">
<ArgumentTypeCoercion>
<code><![CDATA[$serviceConfig]]></code>
</ArgumentTypeCoercion>
<MissingClosureParamType>
<code><![CDATA[$services]]></code>
<code><![CDATA[$services]]></code>
</MissingClosureParamType>
<MissingClosureReturnType>
<code><![CDATA[static fn($services) => $services->get('HttpRouter')]]></code>
</MissingClosureReturnType>
<MixedArgument>
<code><![CDATA[$services]]></code>
</MixedArgument>
Expand All @@ -1423,20 +1400,10 @@
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[bootstrap]]></code>
<code><![CDATA[get]]></code>
<code><![CDATA[setUri]]></code>
</MixedMethodCall>
</file>
<file src="test/Application/MissingControllerTrait.php">
<ArgumentTypeCoercion>
<code><![CDATA[$serviceConfig]]></code>
</ArgumentTypeCoercion>
<MissingClosureParamType>
<code><![CDATA[$services]]></code>
</MissingClosureParamType>
<MissingClosureReturnType>
<code><![CDATA[static fn($services) => $services->get('HttpRouter')]]></code>
</MissingClosureReturnType>
<MixedAssignment>
<code><![CDATA[$application]]></code>
<code><![CDATA[$request]]></code>
Expand All @@ -1446,21 +1413,15 @@
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[bootstrap]]></code>
<code><![CDATA[get]]></code>
<code><![CDATA[setUri]]></code>
</MixedMethodCall>
</file>
<file src="test/Application/PathControllerTrait.php">
<ArgumentTypeCoercion>
<code><![CDATA[$serviceConfig]]></code>
</ArgumentTypeCoercion>
<MissingClosureParamType>
<code><![CDATA[$services]]></code>
<code><![CDATA[$services]]></code>
</MissingClosureParamType>
<MissingClosureReturnType>
<code><![CDATA[static fn($services) => $services->get('HttpRouter')]]></code>
</MissingClosureReturnType>
<MissingClosureReturnType/>
<MixedArgument>
<code><![CDATA[$services]]></code>
</MixedArgument>
Expand All @@ -1473,7 +1434,6 @@
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[bootstrap]]></code>
<code><![CDATA[get]]></code>
<code><![CDATA[setUri]]></code>
</MixedMethodCall>
</file>
Expand All @@ -1487,10 +1447,7 @@
</file>
<file src="test/ApplicationTest.php">
<ArgumentTypeCoercion>
<code><![CDATA[$eventManager]]></code>
<code><![CDATA[$eventManager]]></code>
<code><![CDATA[$events]]></code>
<code><![CDATA[$serviceConfig]]></code>
</ArgumentTypeCoercion>
<DeprecatedMethod>
<code><![CDATA[setMethods]]></code>
Expand Down Expand Up @@ -1532,7 +1489,6 @@
<code><![CDATA[static function (MvcEvent $event) {]]></code>
</MissingClosureReturnType>
<MixedArgument>
<code><![CDATA[$defaultListenerName]]></code>
<code><![CDATA[$response->getContent()]]></code>
<code><![CDATA[$response->getContent()]]></code>
<code><![CDATA[$router]]></code>
Expand All @@ -1543,17 +1499,10 @@
<MixedAssignment>
<code><![CDATA[$controllerLoader]]></code>
<code><![CDATA[$controllerManager]]></code>
<code><![CDATA[$defaultListener]]></code>
<code><![CDATA[$defaultListenerName]]></code>
<code><![CDATA[$defaultListenersNames]]></code>
<code><![CDATA[$defaultListeners[]]]></code>
<code><![CDATA[$error]]></code>
<code><![CDATA[$error]]></code>
<code><![CDATA[$events]]></code>
<code><![CDATA[$listener]]></code>
<code><![CDATA[$listener]]></code>
<code><![CDATA[$listenerService]]></code>
<code><![CDATA[$registeredListeners[]]]></code>
<code><![CDATA[$request]]></code>
<code><![CDATA[$request]]></code>
<code><![CDATA[$request]]></code>
Expand Down
19 changes: 2 additions & 17 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,6 @@ class Application implements EventsCapableInterface
public const ERROR_EXCEPTION = 'error-exception';
public const ERROR_ROUTER_NO_MATCH = 'error-router-no-match';

/**
* Default application event listeners
*
* @var array
*/
protected $defaultListeners = [
'RouteListener',
'DispatchListener',
'HttpMethodListener',
'ViewManager',
'SendResponseListener',
];

/**
* MVC event token
*
Expand All @@ -78,16 +65,14 @@ class Application implements EventsCapableInterface
public function __construct(
protected ServiceManager $serviceManager,
EventManagerInterface $events,
ApplicationListenerProvider $listenerProvider,
?RequestInterface $request = null,
?ResponseInterface $response = null
) {
$this->setEventManager($events);
$listenerProvider->registerListeners($this);
$this->request = $request ?: $serviceManager->get('Request');
$this->response = $response ?: $serviceManager->get('Response');

foreach ($this->defaultListeners as $listener) {
$serviceManager->get($listener)->attach($this->events);
}
}

/**
Expand Down
94 changes: 94 additions & 0 deletions src/ApplicationListenerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?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;

/**
* Double-dispatch listener provider for Application to ensure default listeners and to ensure listeners attached
* once on Application instantiation.
*
* Delays fetching listener aggregates from container until attempt to attach them is made.
*/
final class ApplicationListenerProvider
{
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);
}
}
}
67 changes: 36 additions & 31 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Laminas\Mvc\RouteListener;
use Laminas\Mvc\SendResponseListener;
use Laminas\Mvc\Service\ApplicationFactory;
use Laminas\Mvc\Service\ApplicationListenerProviderFactory;
use Laminas\Mvc\Service\ControllerManagerFactory;
use Laminas\Mvc\Service\ControllerPluginManagerFactory;
use Laminas\Mvc\Service\DispatchListenerFactory;
Expand Down Expand Up @@ -59,7 +60,10 @@ class ConfigProvider
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
'dependencies' => $this->getDependencies(),
Application::class => [
'listeners' => [],
],
];
}

Expand Down Expand Up @@ -97,36 +101,37 @@ public function getDependencies(): array
ControllerManager::class => 'ControllerManager',
],
'factories' => [
'EventManager' => EventManagerFactory::class,
'SharedEventManager' => static fn() => new SharedEventManager(),
'Application' => ApplicationFactory::class,
'ControllerManager' => ControllerManagerFactory::class,
'ControllerPluginManager' => ControllerPluginManagerFactory::class,
'DispatchListener' => DispatchListenerFactory::class,
'HttpExceptionStrategy' => HttpExceptionStrategyFactory::class,
'HttpMethodListener' => HttpMethodListenerFactory::class,
'HttpRouteNotFoundStrategy' => HttpRouteNotFoundStrategyFactory::class,
'HttpViewManager' => HttpViewManagerFactory::class,
'InjectTemplateListener' => InjectTemplateListenerFactory::class,
'PaginatorPluginManager' => PaginatorPluginManagerFactory::class,
'Request' => RequestFactory::class,
'Response' => ResponseFactory::class,
'ViewHelperManager' => ViewHelperManagerFactory::class,
DefaultRenderingStrategy::class => HttpDefaultRenderingStrategyFactory::class,
'ViewFeedStrategy' => ViewFeedStrategyFactory::class,
'ViewJsonStrategy' => ViewJsonStrategyFactory::class,
'ViewManager' => ViewManagerFactory::class,
'ViewResolver' => ViewResolverFactory::class,
'ViewTemplateMapResolver' => ViewTemplateMapResolverFactory::class,
'ViewTemplatePathStack' => ViewTemplatePathStackFactory::class,
'ViewPrefixPathStackResolver' => ViewPrefixPathStackResolverFactory::class,
RouteListener::class => InvokableFactory::class,
SendResponseListener::class => SendResponseListenerFactory::class,
FeedRenderer::class => InvokableFactory::class,
JsonRenderer::class => InvokableFactory::class,
PhpRenderer::class => ViewPhpRendererFactory::class,
PhpRendererStrategy::class => ViewPhpRendererStrategyFactory::class,
View::class => ViewFactory::class,
'EventManager' => EventManagerFactory::class,
'SharedEventManager' => static fn() => new SharedEventManager(),
'Application' => ApplicationFactory::class,
'ControllerManager' => ControllerManagerFactory::class,
'ControllerPluginManager' => ControllerPluginManagerFactory::class,
'DispatchListener' => DispatchListenerFactory::class,
'HttpExceptionStrategy' => HttpExceptionStrategyFactory::class,
'HttpMethodListener' => HttpMethodListenerFactory::class,
'HttpRouteNotFoundStrategy' => HttpRouteNotFoundStrategyFactory::class,
'HttpViewManager' => HttpViewManagerFactory::class,
'InjectTemplateListener' => InjectTemplateListenerFactory::class,
'PaginatorPluginManager' => PaginatorPluginManagerFactory::class,
'Request' => RequestFactory::class,
'Response' => ResponseFactory::class,
'ViewHelperManager' => ViewHelperManagerFactory::class,
DefaultRenderingStrategy::class => HttpDefaultRenderingStrategyFactory::class,
'ViewFeedStrategy' => ViewFeedStrategyFactory::class,
'ViewJsonStrategy' => ViewJsonStrategyFactory::class,
'ViewManager' => ViewManagerFactory::class,
'ViewResolver' => ViewResolverFactory::class,
'ViewTemplateMapResolver' => ViewTemplateMapResolverFactory::class,
'ViewTemplatePathStack' => ViewTemplatePathStackFactory::class,
'ViewPrefixPathStackResolver' => ViewPrefixPathStackResolverFactory::class,
ApplicationListenerProvider::class => ApplicationListenerProviderFactory::class,
RouteListener::class => InvokableFactory::class,
SendResponseListener::class => SendResponseListenerFactory::class,
FeedRenderer::class => InvokableFactory::class,
JsonRenderer::class => InvokableFactory::class,
PhpRenderer::class => ViewPhpRendererFactory::class,
PhpRendererStrategy::class => ViewPhpRendererStrategyFactory::class,
View::class => ViewFactory::class,
],
'shared' => [
'EventManager' => false,
Expand Down
Loading
Loading