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

Commit 334012c

Browse files
committed
Merge branch 'feature/service-manager-v3' into develop
Close #36
2 parents c6b02a9 + 61545b6 commit 334012c

File tree

142 files changed

+4017
-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.

142 files changed

+4017
-2529
lines changed

CHANGELOG.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,57 @@ All notable changes to this project will be documented in this file, in reverse
99
- [#31](https://github.com/zendframework/zend-mvc/pull/31) adds three required
1010
arguments to the `Zend\Mvc\Application` constructor: an EventManager
1111
instance, a Request instance, and a Response instance.
12+
- [#36](https://github.com/zendframework/zend-mvc/pull/36) adds more than a
13+
dozen service factories, primarily to separate conditional factories into
14+
discrete factories.
1215

1316
### Deprecated
1417

1518
- Nothing.
1619

1720
### Removed
1821

19-
- Nothing.
22+
- [#36](https://github.com/zendframework/zend-mvc/pull/36) removes
23+
`Zend\Mvc\Service\ConfigFactory`, as the functionality is now incorporated
24+
into `Zend\ModuleManager\Listener\ServiceListener`.
25+
- [#36](https://github.com/zendframework/zend-mvc/pull/36) removes
26+
the `ServiceLocatorAware` intializer, as zend-servicemanager v3 no longer
27+
defines the interface.
28+
- [#36](https://github.com/zendframework/zend-mvc/pull/36) removes
29+
`Zend\Mvc\Service\ControllerLoaderFactory` and replaces it with
30+
`Zend\Mvc\Service\ControllerManagerFactory`.
31+
- [#36](https://github.com/zendframework/zend-mvc/pull/36) removes
32+
`Zend\Mvc\Service\DiFactory`, `Zend\Mvc\Service\DiAbstractServiceFactory`,
33+
`Zend\Mvc\Service\DiStrictAbstractServiceFactory`,
34+
`Zend\Mvc\Service\DiStrictAbstractServiceFactoryFactory`,
35+
and `Zend\Mvc\Service\DiServiceInitializerFactory`, as zend-servicemanager v3
36+
removes `Zend\Di` integration.
2037

2138
### Fixed
2239

2340
- [#31](https://github.com/zendframework/zend-mvc/pull/31) updates the component
2441
to use zend-eventmanager v3.
42+
- [#36](https://github.com/zendframework/zend-mvc/pull/36) updates the component
43+
to use zend-servicemanager v3, and zend-modulemanager v3. This involves:
44+
- Updating all factories implementing either `FactoryInterface` or
45+
`AbstractFactoryInterface` to the new signatures of those interfaces.
46+
- Updating all plugin managers to the updates to `AbstractPluginManager`.
47+
- Updating how plugin manager factories work (they're now passed the container
48+
instance in their constructor arguments, as well as any build options).
49+
- Added a `RouteInvokableFactory`, which can act as either a
50+
`FactoryInterface` or `AbstractFactoryInterface` for loading invokable route
51+
classes, including by fully qualified class name. This is registered as an
52+
abstract factory by default with the `RoutePluginManager`.
53+
- The `DispatchListener` now receives the controller manager instance at
54+
instantiation.
55+
- The `ViewManager` implementations were updated, and most functionality
56+
within separated into discrete factories. (Previously these instances
57+
injected services and aliases into the service manager instance, which is no
58+
longer possible or desirable with the zend-servicemanager v3 changes.)
59+
- `Application::init()` now pulls the configured service manager from the
60+
`Zend\ModuleManager\Listener\ServiceListener` instance before retrieving and
61+
bootstrapping the `Application` instance; this ensure it is fully
62+
configured at that time.
2563

2664
## 2.6.1 - TBD
2765

composer.json

Lines changed: 1 addition & 1 deletion
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",

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();

0 commit comments

Comments
 (0)