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

Commit a46be7d

Browse files
committed
Refactored to work with zend-servicemanager v3
- All factories implementing `FactoryInterface` and/or `AbstractFactoryInterface` were updated to the new interface signatures. - All plugin managers were updated to the new `AbstractPluginManager` changes. Whenever possible, they were rewritten to remove invokables and instead define factories and/or aliases. - Added the `RouteInvokableFactory`, which acts both as a specialized invokable factory for route implementations (as they use a `::factory()` method for initialization vs a constructor), and an abstract factory for handling FQCN route names. The `RoutePluginManager` composes this abstract factory by default. - `Application::init` was updated to pull the fully configured service manager from the `ServiceListener` after loading modules. - The `DispatchListener` now receives its controller manager instance during instantiation. - Separated router factory into 3: - `ConsoleRouterFactory` will create a console router - `HttpRouterFactory` will create an HTTP router - `RouterFactory` delegates to the above two, based on status of `Console::isConsole()` The above makes it possible to specify the specific router type you want to use via configuration, making testing possible. - Refactored the ViewManager implementations. Previously, the view managers were adding services and aliases to the service manager duing their bootstrap listeners; with the SM now immutable, that does not make sense, and, in fact, leads to errors. In all cases where object creation was occuring, the code was moved to new factories, which the view manager implementations can now pull specifically. Aliases for these were added where appropriate. However, I stopped short of adding factories for general services such as `DefaultRenderingStrategy` as the implementations differ enough that having such a factory mutate based on the selected view manager would require type-hinting to consume the services regardless. - New integration tests for `Application` were written to ensure the SM is in correct state for `bootstrap()` and `run()`, and that the service listener <-> service manager dance is correctly managed.
1 parent c6b02a9 commit a46be7d

File tree

141 files changed

+3979
-2529
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+3979
-2529
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"require": {
1616
"php": ">=5.5",
1717
"zendframework/zend-eventmanager": "dev-develop as 2.7.0",
18-
"zendframework/zend-servicemanager": "~2.5",
18+
"zendframework/zend-servicemanager": "dev-develop as 2.6.0",
1919
"zendframework/zend-hydrator": "~1.0",
2020
"zendframework/zend-form": "~2.6",
2121
"zendframework/zend-stdlib": "~2.7",
@@ -39,7 +39,7 @@
3939
"zendframework/zend-uri": "~2.5",
4040
"zendframework/zend-validator": "~2.5",
4141
"zendframework/zend-version": "~2.5",
42-
"zendframework/zend-view": "dev-develop as 2.6.0",
42+
"zendframework/zend-view": "dev-develop#82c537d8f as 2.6.0",
4343
"fabpot/php-cs-fixer": "1.7.*",
4444
"phpunit/PHPUnit": "~4.0"
4545
},

src/Application.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function __construct(
125125
*/
126126
public function getConfig()
127127
{
128-
return $this->serviceManager->get('Config');
128+
return $this->serviceManager->get('config');
129129
}
130130

131131
/**
@@ -140,9 +140,10 @@ public function getConfig()
140140
*/
141141
public function bootstrap(array $listeners = [])
142142
{
143-
$serviceManager = $this->serviceManager;
144143
$events = $this->events;
144+
$serviceManager = $this->serviceManager;
145145

146+
// Setup default listeners
146147
$listeners = array_unique(array_merge($this->defaultListeners, $listeners));
147148

148149
foreach ($listeners as $listener) {
@@ -160,6 +161,7 @@ public function bootstrap(array $listeners = [])
160161

161162
// Trigger bootstrap events
162163
$events->triggerEvent($event);
164+
163165
return $this;
164166
}
165167

@@ -252,13 +254,27 @@ public function getEventManager()
252254
*/
253255
public static function init($configuration = [])
254256
{
257+
// Prepare the service manager
255258
$smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : [];
256-
$serviceManager = new ServiceManager(new Service\ServiceManagerConfig($smConfig));
257-
$serviceManager->setService('ApplicationConfig', $configuration);
259+
$smConfig = new Service\ServiceManagerConfig($smConfig);
260+
261+
$serviceManager = new ServiceManager($smConfig->toArray());
262+
$serviceManager = $serviceManager->withConfig(['services' => [
263+
'ApplicationConfig' => $configuration,
264+
]]);
265+
266+
// Load modules
258267
$serviceManager->get('ModuleManager')->loadModules();
259268

269+
// Get the configured SM if necessary.
270+
if ($serviceManager->has('ServiceListener')) {
271+
$serviceListener = $serviceManager->get('ServiceListener');
272+
$serviceManager = $serviceListener->getConfiguredServiceManager();
273+
}
274+
275+
// Prepare list of listeners to bootstrap
260276
$listenersFromAppConfig = isset($configuration['listeners']) ? $configuration['listeners'] : [];
261-
$config = $serviceManager->get('Config');
277+
$config = $serviceManager->get('config');
262278
$listenersFromConfigService = isset($config['listeners']) ? $config['listeners'] : [];
263279

264280
$listeners = array_unique(array_merge($listenersFromConfigService, $listenersFromAppConfig));

src/Controller/AbstractController.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
use Zend\Http\Request as HttpRequest;
1818
use Zend\Mvc\InjectApplicationEventInterface;
1919
use Zend\Mvc\MvcEvent;
20-
use Zend\ServiceManager\ServiceLocatorAwareInterface;
21-
use Zend\ServiceManager\ServiceLocatorInterface;
20+
use Zend\ServiceManager\ServiceManager;
2221
use Zend\Stdlib\DispatchableInterface as Dispatchable;
2322
use Zend\Stdlib\RequestInterface as Request;
2423
use Zend\Stdlib\ResponseInterface as Response;
@@ -46,8 +45,7 @@
4645
abstract class AbstractController implements
4746
Dispatchable,
4847
EventManagerAwareInterface,
49-
InjectApplicationEventInterface,
50-
ServiceLocatorAwareInterface
48+
InjectApplicationEventInterface
5149
{
5250
/**
5351
* @var PluginManager
@@ -74,11 +72,6 @@ abstract class AbstractController implements
7472
*/
7573
protected $events;
7674

77-
/**
78-
* @var ServiceLocatorInterface
79-
*/
80-
protected $serviceLocator;
81-
8275
/**
8376
* @var null|string|string[]
8477
*/
@@ -231,27 +224,6 @@ public function getEvent()
231224
return $this->event;
232225
}
233226

234-
/**
235-
* Set serviceManager instance
236-
*
237-
* @param ServiceLocatorInterface $serviceLocator
238-
* @return void
239-
*/
240-
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
241-
{
242-
$this->serviceLocator = $serviceLocator;
243-
}
244-
245-
/**
246-
* Retrieve serviceManager instance
247-
*
248-
* @return ServiceLocatorInterface
249-
*/
250-
public function getServiceLocator()
251-
{
252-
return $this->serviceLocator;
253-
}
254-
255227
/**
256228
* Get plugin manager
257229
*
@@ -260,7 +232,7 @@ public function getServiceLocator()
260232
public function getPluginManager()
261233
{
262234
if (!$this->plugins) {
263-
$this->setPluginManager(new PluginManager());
235+
$this->setPluginManager(new PluginManager(new ServiceManager()));
264236
}
265237

266238
$this->plugins->setController($this);

src/Controller/ControllerManager.php

Lines changed: 40 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99

1010
namespace Zend\Mvc\Controller;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\EventManager\EventManagerAwareInterface;
1314
use Zend\EventManager\SharedEventManagerInterface;
14-
use Zend\Mvc\Exception;
1515
use Zend\ServiceManager\AbstractPluginManager;
16-
use Zend\ServiceManager\ConfigInterface;
17-
use Zend\ServiceManager\ServiceLocatorAwareInterface;
18-
use Zend\ServiceManager\ServiceLocatorInterface;
1916
use Zend\Stdlib\DispatchableInterface;
2017

2118
/**
@@ -25,113 +22,77 @@
2522
*/
2623
class ControllerManager extends AbstractPluginManager
2724
{
28-
/**
29-
* We do not want arbitrary classes instantiated as controllers.
30-
*
31-
* @var bool
32-
*/
33-
protected $autoAddInvokableClass = false;
25+
protected $instanceOf = DispatchableInterface::class;
3426

3527
/**
3628
* Constructor
3729
*
38-
* After invoking parent constructor, add an initializer to inject the
39-
* service manager, event manager, and plugin manager
30+
* Injects an initializer for injecting controllers with an
31+
* event manager and plugin manager.
4032
*
41-
* @param null|ConfigInterface $configuration
33+
* @param ContainerInterface $container
34+
* @param array $configuration
4235
*/
43-
public function __construct(ConfigInterface $configuration = null)
36+
public function __construct(ContainerInterface $container, array $configuration = [])
4437
{
45-
parent::__construct($configuration);
46-
// Pushing to bottom of stack to ensure this is done last
47-
$this->addInitializer([$this, 'injectControllerDependencies'], false);
38+
$this->initializers[] = [$this, 'injectEventManager'];
39+
$this->initializers[] = [$this, 'injectConsole'];
40+
$this->initializers[] = [$this, 'injectPluginManager'];
41+
parent::__construct($container, $configuration);
4842
}
4943

5044
/**
51-
* Inject required dependencies into the controller.
45+
* Initializer: inject EventManager instance
46+
*
47+
* If we have an event manager composed already, make sure it gets injected
48+
* with the shared event manager.
49+
*
50+
* The AbstractController lazy-instantiates an EM instance, which is why
51+
* the shared EM injection needs to happen; the conditional will always
52+
* pass.
5253
*
53-
* @param DispatchableInterface $controller
54-
* @param ServiceLocatorInterface $serviceLocator
55-
* @return void
54+
* @param ContainerInterface $container
55+
* @param DispatchableInterface $controller
5656
*/
57-
public function injectControllerDependencies($controller, ServiceLocatorInterface $serviceLocator)
57+
public function injectEventManager(ContainerInterface $container, $controller)
5858
{
59-
if (!$controller instanceof DispatchableInterface) {
59+
if (! $controller instanceof EventManagerAwareInterface) {
6060
return;
6161
}
6262

63-
$parentLocator = $serviceLocator->getServiceLocator();
64-
65-
if ($controller instanceof ServiceLocatorAwareInterface) {
66-
$controller->setServiceLocator($parentLocator->get('Zend\ServiceManager\ServiceLocatorInterface'));
67-
}
68-
69-
if ($controller instanceof EventManagerAwareInterface) {
70-
// If we have an event manager composed already, make sure it gets
71-
// injected with the shared event manager.
72-
// The AbstractController lazy-instantiates an EM instance, which
73-
// is why the shared EM injection needs to happen; the conditional
74-
// will always pass.
75-
$events = $controller->getEventManager();
76-
if (! $events || ! $events->getSharedManager() instanceof SharedEventManagerInterface) {
77-
$controller->setEventManager($parentLocator->get('EventManager'));
78-
}
79-
}
80-
81-
if ($controller instanceof AbstractConsoleController) {
82-
$controller->setConsole($parentLocator->get('Console'));
83-
}
84-
85-
if (method_exists($controller, 'setPluginManager')) {
86-
$controller->setPluginManager($parentLocator->get('ControllerPluginManager'));
63+
$events = $controller->getEventManager();
64+
if (! $events || ! $events->getSharedManager() instanceof SharedEventManagerInterface) {
65+
$controller->setEventManager($container->get('EventManager'));
8766
}
8867
}
8968

9069
/**
91-
* Validate the plugin
92-
*
93-
* Ensure we have a dispatchable.
70+
* Initializer: inject Console adapter instance
9471
*
95-
* @param mixed $plugin
96-
* @return true
97-
* @throws Exception\InvalidControllerException
72+
* @param ContainerInterface $container
73+
* @param DispatchableInterface $controller
9874
*/
99-
public function validatePlugin($plugin)
75+
public function injectConsole(ContainerInterface $container, $controller)
10076
{
101-
if ($plugin instanceof DispatchableInterface) {
102-
// we're okay
77+
if (! $controller instanceof AbstractConsoleController) {
10378
return;
10479
}
10580

106-
throw new Exception\InvalidControllerException(sprintf(
107-
'Controller of type %s is invalid; must implement Zend\Stdlib\DispatchableInterface',
108-
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
109-
));
81+
$controller->setConsole($container->get('Console'));
11082
}
11183

11284
/**
113-
* Override: do not use peering service managers
85+
* Initializer: inject plugin manager
11486
*
115-
* @param string|array $name
116-
* @param bool $checkAbstractFactories
117-
* @param bool $usePeeringServiceManagers
118-
* @return bool
87+
* @param ContainerInterface $container
88+
* @param DispatchableInterface $controller
11989
*/
120-
public function has($name, $checkAbstractFactories = true, $usePeeringServiceManagers = false)
90+
public function injectPluginManager(ContainerInterface $container, $controller)
12191
{
122-
return parent::has($name, $checkAbstractFactories, $usePeeringServiceManagers);
123-
}
92+
if (! method_exists($controller, 'setPluginManager')) {
93+
return;
94+
}
12495

125-
/**
126-
* Override: do not use peering service managers
127-
*
128-
* @param string $name
129-
* @param array $options
130-
* @param bool $usePeeringServiceManagers
131-
* @return mixed
132-
*/
133-
public function get($name, $options = [], $usePeeringServiceManagers = false)
134-
{
135-
return parent::get($name, $options, $usePeeringServiceManagers);
96+
$controller->setPluginManager($container->get('ControllerPluginManager'));
13697
}
13798
}

src/Controller/Plugin/Forward.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ protected function getEvent()
236236

237237
$controller = $this->getController();
238238
if (!$controller instanceof InjectApplicationEventInterface) {
239-
throw new Exception\DomainException('Forward plugin requires a controller that implements InjectApplicationEventInterface');
239+
throw new Exception\DomainException(sprintf(
240+
'Forward plugin requires a controller that implements InjectApplicationEventInterface; received %s',
241+
(is_object($controller) ? get_class($controller) : var_export($controller, 1))
242+
));
240243
}
241244

242245
$event = $controller->getEvent();

src/Controller/Plugin/Service/ForwardFactory.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace Zend\Mvc\Controller\Plugin\Service;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
13-
use Zend\ServiceManager\FactoryInterface;
14-
use Zend\ServiceManager\ServiceLocatorInterface;
14+
use Zend\ServiceManager\Factory\FactoryInterface;
1515
use Zend\Mvc\Controller\Plugin\Forward;
1616

1717
class ForwardFactory implements FactoryInterface
@@ -22,24 +22,16 @@ class ForwardFactory implements FactoryInterface
2222
* @return Forward
2323
* @throws ServiceNotCreatedException if Controllermanager service is not found in application service locator
2424
*/
25-
public function createService(ServiceLocatorInterface $plugins)
25+
public function __invoke(ContainerInterface $container, $name, array $options = null)
2626
{
27-
$services = $plugins->getServiceLocator();
28-
if (!$services instanceof ServiceLocatorInterface) {
29-
throw new ServiceNotCreatedException(sprintf(
30-
'%s requires that the application service manager has been injected; none found',
31-
__CLASS__
32-
));
33-
}
34-
35-
if (!$services->has('ControllerManager')) {
27+
if (! $container->has('ControllerManager')) {
3628
throw new ServiceNotCreatedException(sprintf(
3729
'%s requires that the application service manager contains a "%s" service; none found',
3830
__CLASS__,
3931
'ControllerManager'
4032
));
4133
}
42-
$controllers = $services->get('ControllerManager');
34+
$controllers = $container->get('ControllerManager');
4335

4436
return new Forward($controllers);
4537
}

src/Controller/Plugin/Service/IdentityFactory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
namespace Zend\Mvc\Controller\Plugin\Service;
1111

12+
use Interop\Container\ContainerInterface;
13+
use Zend\Authentication\AuthenticationService;
1214
use Zend\Mvc\Controller\Plugin\Identity;
13-
use Zend\ServiceManager\FactoryInterface;
14-
use Zend\ServiceManager\ServiceLocatorInterface;
15+
use Zend\ServiceManager\Factory\FactoryInterface;
1516

1617
class IdentityFactory implements FactoryInterface
1718
{
@@ -20,12 +21,11 @@ class IdentityFactory implements FactoryInterface
2021
*
2122
* @return \Zend\Mvc\Controller\Plugin\Identity
2223
*/
23-
public function createService(ServiceLocatorInterface $serviceLocator)
24+
public function __invoke(ContainerInterface $container, $name, array $options = null)
2425
{
25-
$services = $serviceLocator->getServiceLocator();
2626
$helper = new Identity();
27-
if ($services->has('Zend\Authentication\AuthenticationService')) {
28-
$helper->setAuthenticationService($services->get('Zend\Authentication\AuthenticationService'));
27+
if ($container->has(AuthenticationService::class)) {
28+
$helper->setAuthenticationService($container->get(AuthenticationService::class));
2929
}
3030
return $helper;
3131
}

0 commit comments

Comments
 (0)