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

Commit 7800a56

Browse files
committed
Vary PluginManager implementation based on zend-servicemanager version detected
`PluginManager` overrides the `get()` method, which has a different signature in v2 than in v3. Until PHP 7.2, this was not a problem; however, in 7.2, even optional values must follow the exact same signature as the parent. As a result, we now need to vary the implementations. This patch provides `AbstractPluginManager`, which contains the bulk of the logic for implementing the `PluginManager`. It then introduces the following: - `PluginManagerSM2`, which extends `AbstractPluginManager` and implements the zend-servicemanager v2 `get()` signature. - `PluginManagerSM3`, which extends `AbstractPluginManager` and implements the zend-servicemanager v3 `get()` signature. It removes the `PluginManager` class. It then adds a file-based autoloader that checks to see if `Zend\ServiceManager\PluginManagerInterface` exists. If it does, it aliases `PluginManager` to `PluginManagerSM3`; otherwise, it aliases it to `PluginManagerSM2`. Additionally, this patch updates to zend-servicemanager 2.7.10, which has important fixes to how it handles creation options with InvokableFactory that are necessary here.
1 parent f82295e commit 7800a56

7 files changed

+103
-36
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
],
99
"homepage": "https://github.com/zendframework/zend-mvc",
1010
"autoload": {
11+
"files": [
12+
"src/autoload.php"
13+
],
1114
"psr-4": {
1215
"Zend\\Mvc\\": "src/"
1316
}
1417
},
1518
"require": {
1619
"php": "^5.5 || ^7.0",
1720
"zendframework/zend-eventmanager": "^2.6.4 || ^3.0",
18-
"zendframework/zend-servicemanager": "^2.7.9 || ^3.0.3",
21+
"zendframework/zend-servicemanager": "^2.7.10 || ^3.0.3",
1922
"zendframework/zend-hydrator": "^1.1 || ^2.4",
2023
"zendframework/zend-form": "^2.11",
2124
"zendframework/zend-stdlib": "^2.7.5 || ^3.0",

composer.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Controller/PluginManager.php renamed to src/Controller/AbstractPluginManager.php

+10-32
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
<?php
22
/**
3-
* Zend Framework (http://framework.zend.com/)
4-
*
5-
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7-
* @license http://framework.zend.com/license/new-bsd New BSD License
3+
* @see https://github.com/zendframework/zend-mvc for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License
86
*/
97

108
namespace Zend\Mvc\Controller;
119

1210
use Zend\Mvc\Exception;
13-
use Zend\ServiceManager\AbstractPluginManager;
11+
use Zend\ServiceManager\AbstractPluginManager as BasePluginManager;
1412
use Zend\ServiceManager\Exception\InvalidServiceException;
1513
use Zend\ServiceManager\Factory\InvokableFactory;
1614
use Zend\Stdlib\DispatchableInterface;
1715

1816
/**
19-
* Plugin manager implementation for controllers
17+
* Base functionality for the controller plugins plugin manager.
2018
*
21-
* Registers a number of default plugins, and contains an initializer for
22-
* injecting plugins with the current controller.
19+
* Functionality is split between two concrete implementations as the signatures
20+
* for `get()` vary between zend-servicemanager v2 and v3. The autoloader aliases
21+
* `Zend\Mvc\Controller\PluginManager` to the version-appropriate class, which
22+
* in turn composses this trait.
2323
*/
24-
class PluginManager extends AbstractPluginManager
24+
abstract class AbstractPluginManager extends BasePluginManager
2525
{
2626
/**
2727
* Plugins must be of this type.
@@ -106,28 +106,6 @@ class PluginManager extends AbstractPluginManager
106106
*/
107107
protected $controller;
108108

109-
/**
110-
* Retrieve a registered instance
111-
*
112-
* After the plugin is retrieved from the service locator, inject the
113-
* controller in the plugin every time it is requested. This is required
114-
* because a controller can use a plugin and another controller can be
115-
* dispatched afterwards. If this second controller uses the same plugin
116-
* as the first controller, the reference to the controller inside the
117-
* plugin is lost.
118-
*
119-
* @param string $name
120-
* @return DispatchableInterface
121-
*/
122-
public function get($name, array $options = null, $usePeeringServiceManagers = true)
123-
{
124-
$options = $options ?: [];
125-
$plugin = parent::get($name, $options);
126-
$this->injectController($plugin);
127-
128-
return $plugin;
129-
}
130-
131109
/**
132110
* Set controller
133111
*

src/Controller/ControllerManager.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Zend\EventManager\EventManagerAwareInterface;
1414
use Zend\EventManager\SharedEventManagerInterface;
1515
use Zend\Mvc\Exception;
16-
use Zend\ServiceManager\AbstractPluginManager;
16+
use Zend\ServiceManager\AbstractPluginManager as BasePluginManager;
1717
use Zend\ServiceManager\ConfigInterface;
1818
use Zend\ServiceManager\Exception\InvalidServiceException;
1919
use Zend\ServiceManager\ServiceLocatorAwareInterface;
@@ -24,7 +24,7 @@
2424
*
2525
* Does not define any controllers by default, but does add a validator.
2626
*/
27-
class ControllerManager extends AbstractPluginManager
27+
class ControllerManager extends BasePluginManager
2828
{
2929
/**
3030
* We do not want arbitrary classes instantiated as controllers.

src/Controller/PluginManagerSM2.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-mvc for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\Mvc\Controller;
9+
10+
use Zend\Stdlib\DispatchableInterface;
11+
12+
class PluginManagerSM2 extends AbstractPluginManager
13+
{
14+
/**
15+
* Retrieve a registered instance
16+
*
17+
* After the plugin is retrieved from the service locator, inject the
18+
* controller in the plugin every time it is requested. This is required
19+
* because a controller can use a plugin and another controller can be
20+
* dispatched afterwards. If this second controller uses the same plugin
21+
* as the first controller, the reference to the controller inside the
22+
* plugin is lost.
23+
*
24+
* @param string $name
25+
* @return DispatchableInterface
26+
*/
27+
public function get($name, $options = [], $usePeeringServiceManagers = true)
28+
{
29+
$options = is_array($options) && empty($options) ? null : $options;
30+
$plugin = parent::get($name, $options);
31+
$this->injectController($plugin);
32+
33+
return $plugin;
34+
}
35+
}

src/Controller/PluginManagerSM3.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-mvc for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\Mvc\Controller;
9+
10+
use Zend\Stdlib\DispatchableInterface;
11+
12+
class PluginManagerSM3 extends AbstractPluginManager
13+
{
14+
/**
15+
* Retrieve a registered instance
16+
*
17+
* After the plugin is retrieved from the service locator, inject the
18+
* controller in the plugin every time it is requested. This is required
19+
* because a controller can use a plugin and another controller can be
20+
* dispatched afterwards. If this second controller uses the same plugin
21+
* as the first controller, the reference to the controller inside the
22+
* plugin is lost.
23+
*
24+
* @param string $name
25+
* @param null|array $options
26+
* @return DispatchableInterface
27+
*/
28+
public function get($name, array $options = null)
29+
{
30+
$plugin = parent::get($name, $options);
31+
$this->injectController($plugin);
32+
33+
return $plugin;
34+
}
35+
}

src/autoload.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-mvc for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\Mvc;
9+
10+
use Zend\ServiceManager\PluginManagerInterface;
11+
12+
if (class_exists(PluginManagerInterface::class)) {
13+
class_alias(Controller\PluginManagerSM3::class, Controller\PluginManager::class, true);
14+
} else {
15+
class_alias(Controller\PluginManagerSM2::class, Controller\PluginManager::class, true);
16+
}

0 commit comments

Comments
 (0)