Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit b2a17be

Browse files
committed
Ensure ServiceListenerFactory works on both v2 and v3
- Re-introduced `createService()` method, proxying to `__invoke()`. - Re-defined invokables as aliases + invokable factories. - Re-enabled form abstract factory.
1 parent 5350b19 commit b2a17be

File tree

1 file changed

+56
-42
lines changed

1 file changed

+56
-42
lines changed

src/Service/ServiceListenerFactory.php

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Zend\ModuleManager\Listener\ServiceListenerInterface;
1515
use Zend\Mvc\View;
1616
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
17-
use Zend\ServiceManager\Factory\FactoryInterface;
17+
use Zend\ServiceManager\FactoryInterface;
1818
use Zend\ServiceManager\Factory\InvokableFactory;
1919

2020
class ServiceListenerFactory implements FactoryInterface
@@ -36,14 +36,33 @@ class ServiceListenerFactory implements FactoryInterface
3636
* @var array
3737
*/
3838
protected $defaultServiceConfig = [
39-
'invokables' => [
40-
'MiddlewareListener' => 'Zend\Mvc\MiddlewareListener',
41-
'RouteListener' => 'Zend\Mvc\RouteListener',
42-
'SendResponseListener' => 'Zend\Mvc\SendResponseListener',
43-
'ViewJsonRenderer' => 'Zend\View\Renderer\JsonRenderer',
44-
'ViewFeedRenderer' => 'Zend\View\Renderer\FeedRenderer',
39+
'aliases' => [
40+
'Config' => 'config',
41+
'Configuration' => 'config',
42+
'configuration' => 'config',
43+
'Console' => 'ConsoleAdapter',
44+
'ConsoleDefaultRenderingStrategy' => View\Console\DefaultRenderingStrategy::class,
45+
'HttpDefaultRenderingStrategy' => View\Http\DefaultRenderingStrategy::class,
46+
'MiddlewareListener' => 'Zend\Mvc\MiddlewareListener',
47+
'RouteListener' => 'Zend\Mvc\RouteListener',
48+
'SendResponseListener' => 'Zend\Mvc\SendResponseListener',
49+
'View' => 'Zend\View\View',
50+
'ViewFeedRenderer' => 'Zend\View\Renderer\FeedRenderer',
51+
'ViewJsonRenderer' => 'Zend\View\Renderer\JsonRenderer',
52+
'ViewPhpRendererStrategy' => 'Zend\View\Strategy\PhpRendererStrategy',
53+
'ViewPhpRenderer' => 'Zend\View\Renderer\PhpRenderer',
54+
'ViewRenderer' => 'Zend\View\Renderer\PhpRenderer',
55+
'Zend\Form\Annotation\FormAnnotationBuilder' => 'FormAnnotationBuilder',
56+
'Zend\Mvc\Controller\PluginManager' => 'ControllerPluginManager',
57+
'Zend\Mvc\View\Http\InjectTemplateListener' => 'InjectTemplateListener',
58+
'Zend\View\Renderer\RendererInterface' => 'Zend\View\Renderer\PhpRenderer',
59+
'Zend\View\Resolver\TemplateMapResolver' => 'ViewTemplateMapResolver',
60+
'Zend\View\Resolver\TemplatePathStack' => 'ViewTemplatePathStack',
61+
'Zend\View\Resolver\AggregateResolver' => 'ViewResolver',
62+
'Zend\View\Resolver\ResolverInterface' => 'ViewResolver',
4563
],
46-
'factories' => [
64+
'invokables' => [],
65+
'factories' => [
4766
'Application' => 'Zend\Mvc\Service\ApplicationFactory',
4867
'config' => 'Zend\Mvc\Service\ConfigFactory',
4968
'ControllerManager' => 'Zend\Mvc\Service\ControllerManagerFactory',
@@ -86,56 +105,40 @@ class ServiceListenerFactory implements FactoryInterface
86105
'ViewTemplateMapResolver' => 'Zend\Mvc\Service\ViewTemplateMapResolverFactory',
87106
'ViewTemplatePathStack' => 'Zend\Mvc\Service\ViewTemplatePathStackFactory',
88107
'ViewPrefixPathStackResolver' => 'Zend\Mvc\Service\ViewPrefixPathStackResolverFactory',
108+
'Zend\Mvc\MiddlewareListener' => InvokableFactory::class,
109+
'Zend\Mvc\RouteListener' => InvokableFactory::class,
110+
'Zend\Mvc\SendResponseListener' => InvokableFactory::class,
111+
'Zend\View\Renderer\FeedRenderer' => InvokableFactory::class,
112+
'Zend\View\Renderer\JsonRenderer' => InvokableFactory::class,
89113
'Zend\View\Renderer\PhpRenderer' => ViewPhpRendererFactory::class,
90114
'Zend\View\Strategy\PhpRendererStrategy' => ViewPhpRendererStrategyFactory::class,
91115
'Zend\View\View' => ViewFactory::class,
92116
],
93-
'aliases' => [
94-
'Config' => 'config',
95-
'Configuration' => 'config',
96-
'configuration' => 'config',
97-
'Console' => 'ConsoleAdapter',
98-
'ConsoleDefaultRenderingStrategy' => View\Console\DefaultRenderingStrategy::class,
99-
'HttpDefaultRenderingStrategy' => View\Http\DefaultRenderingStrategy::class,
100-
'View' => 'Zend\View\View',
101-
'ViewPhpRendererStrategy' => 'Zend\View\Strategy\PhpRendererStrategy',
102-
'ViewPhpRenderer' => 'Zend\View\Renderer\PhpRenderer',
103-
'ViewRenderer' => 'Zend\View\Renderer\PhpRenderer',
104-
'Zend\Form\Annotation\FormAnnotationBuilder' => 'FormAnnotationBuilder',
105-
'Zend\Mvc\Controller\PluginManager' => 'ControllerPluginManager',
106-
'Zend\Mvc\View\Http\InjectTemplateListener' => 'InjectTemplateListener',
107-
'Zend\View\Renderer\RendererInterface' => 'Zend\View\Renderer\PhpRenderer',
108-
'Zend\View\Resolver\TemplateMapResolver' => 'ViewTemplateMapResolver',
109-
'Zend\View\Resolver\TemplatePathStack' => 'ViewTemplatePathStack',
110-
'Zend\View\Resolver\AggregateResolver' => 'ViewResolver',
111-
'Zend\View\Resolver\ResolverInterface' => 'ViewResolver',
112-
],
113-
/*
114117
'abstract_factories' => [
115118
'Zend\Form\FormAbstractServiceFactory',
116119
],
117-
*/
118120
];
119121

120122
/**
121123
* Create the service listener service
122124
*
123125
* Tries to get a service named ServiceListenerInterface from the service
124-
* locator, otherwise creates a Zend\ModuleManager\Listener\ServiceListener
125-
* service, passing it the service locator instance and the default service
126-
* configuration, which can be overridden by modules.
126+
* locator, otherwise creates a ServiceListener instance, passing it the
127+
* container instance and the default service configuration, which can be
128+
* overridden by modules.
127129
*
128130
* It looks for the 'service_listener_options' key in the application
129-
* config and tries to add service manager as configured. The value of
130-
* 'service_listener_options' must be a list (array) which contains the
131+
* config and tries to add service/plugin managers as configured. The value
132+
* of 'service_listener_options' must be a list (array) which contains the
131133
* following keys:
132-
* - service_manager: the name of the service manage to create as string
133-
* - config_key: the name of the configuration key to search for as string
134-
* - interface: the name of the interface that modules can implement as string
135-
* - method: the name of the method that modules have to implement as string
134+
*
135+
* - service_manager: the name of the service manage to create as string
136+
* - config_key: the name of the configuration key to search for as string
137+
* - interface: the name of the interface that modules can implement as string
138+
* - method: the name of the method that modules have to implement as string
136139
*
137140
* @param ServiceLocatorInterface $serviceLocator
138-
* @return ServiceListener
141+
* @return ServiceListenerInterface
139142
* @throws ServiceNotCreatedException for invalid ServiceListener service
140143
* @throws ServiceNotCreatedException For invalid configurations.
141144
*/
@@ -149,8 +152,8 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
149152

150153
if (! $serviceListener instanceof ServiceListenerInterface) {
151154
throw new ServiceNotCreatedException(
152-
'The service named ServiceListenerInterface must implement ' .
153-
'Zend\ModuleManager\Listener\ServiceListenerInterface'
155+
'The service named ServiceListenerInterface must implement '
156+
. ServiceListenerInterface::class
154157
);
155158
}
156159

@@ -161,6 +164,17 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
161164
return $serviceListener;
162165
}
163166

167+
/**
168+
* Create and return the ServiceListener (v2)
169+
*
170+
* @param ServiceLocatorInterface $container
171+
* @return ServiceListenerInterface
172+
*/
173+
public function createService(ServiceLocatorInterface $container)
174+
{
175+
return $this($container, ServiceListener::class);
176+
}
177+
164178
/**
165179
* Validate and inject plugin manager options into the service listener.
166180
*

0 commit comments

Comments
 (0)