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

Commit e4e8cd5

Browse files
committed
Provide migration path for validatePlugin
This patch modifies `AbstractPluginManager::validate()` to check for the existence of a `validatePlugin()` method. If found, it will: - Trigger an `E_USER_DEPRECATED` warning indicating that `validatePlugin()` is deprecated. - Call `validatePlugin()` and return. This provides a migration path from v2 plugin managers to v3. v2 plugin managers can continue defining validatePlugin(), but can avoid the warning on upgrade by one of the following paths: - Defining a `validate()` method prior to upgrade with the logic for validating a plugin instance, and modifying the `validatePlugin()` method to proxy to the new method. - After upgrade, renaming the `validatePlugin()` method to `validate()`. - After upgrade, removing the `validatePlugin()` method and defining the `$instanceOf` property to indicate the valid instance type (if only one is possible).
1 parent 5405c7d commit e4e8cd5

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/AbstractPluginManager.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ public function get($name, array $options = null)
128128
*/
129129
public function validate($instance)
130130
{
131+
if (method_exists($this, 'validatePlugin')) {
132+
trigger_error(sprintf(
133+
'%s::validatePlugin() has been deprecated as of 3.0; please define validate() instead',
134+
get_class($this)
135+
), E_USER_DEPRECATED);
136+
$this->validatePlugin($instance);
137+
return;
138+
}
139+
131140
if (empty($this->instanceOf) || $instance instanceof $this->instanceOf) {
132141
return;
133142
}

test/AbstractPluginManagerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,31 @@ public function testPluginManagersMayOptOutOfSupportingAutoInvokableServices()
306306
$this->setExpectedException(ServiceNotFoundException::class, TestAsset\NonAutoInvokablePluginManager::class);
307307
$pluginManager->get(TestAsset\InvokableObject::class);
308308
}
309+
310+
/**
311+
* @group migration
312+
*/
313+
public function testValidateWillFallBackToValidatePluginWhenDefinedAndEmitDeprecationNotice()
314+
{
315+
$assertionCalled = false;
316+
$instance = (object) [];
317+
$assertion = function ($plugin) use ($instance, &$assertionCalled) {
318+
$this->assertSame($instance, $plugin);
319+
$assertionCalled = true;
320+
};
321+
$pluginManager = new TestAsset\V2ValidationPluginManager(new ServiceManager());
322+
$pluginManager->assertion = $assertion;
323+
324+
$errorHandlerCalled = false;
325+
set_error_handler(function ($errno, $errmsg) use (&$errorHandlerCalled) {
326+
$this->assertEquals(E_USER_DEPRECATED, $errno);
327+
$this->assertContains('3.0', $errmsg);
328+
$errorHandlerCalled = true;
329+
}, E_USER_DEPRECATED);
330+
$pluginManager->validate($instance);
331+
restore_error_handler();
332+
333+
$this->assertTrue($assertionCalled, 'Assertion was not called by validatePlugin!');
334+
$this->assertTrue($errorHandlerCalled, 'Error handler was not triggered by validatePlugin!');
335+
}
309336
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
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
8+
*/
9+
10+
namespace ZendTest\ServiceManager\TestAsset;
11+
12+
use RuntimeException;
13+
use Zend\ServiceManager\AbstractPluginManager;
14+
15+
class V2ValidationPluginManager extends AbstractPluginManager
16+
{
17+
public $assertion;
18+
19+
public function validatePlugin($plugin)
20+
{
21+
if (! is_callable($this->assertion)) {
22+
throw new RuntimeException(sprintf(
23+
'%s requires a callable $assertion property; not currently set',
24+
__CLASS__
25+
));
26+
}
27+
28+
call_user_func($this->assertion, $plugin);
29+
}
30+
}

0 commit comments

Comments
 (0)