@@ -1137,6 +1137,8 @@ class ObserverPluginManager extends AbstractPluginManager
1137
1137
'log' => LogObserver::class,
1138
1138
];
1139
1139
1140
+ protected $shareByDefault = false;
1141
+
1140
1142
public function validatePlugin($instance)
1141
1143
{
1142
1144
if (! $plugin instanceof ObserverInterface) {
@@ -1156,12 +1158,16 @@ To prepare this for version 3, we need to do the following:
1156
1158
` factories ` and ` aliases ` .
1157
1159
- We need to implement a ` validate() ` method.
1158
1160
- We need to update the ` validatePlugin() ` method to proxy to ` validate() ` .
1161
+ - We need to add a ` $sharedByDefault ` property (if ` $shareByDefault ` is present)
1159
1162
1160
1163
Doing so, we get the following result:
1161
1164
1162
1165
``` php
1166
+ namespace MyNamespace;
1167
+
1163
1168
use RuntimeException;
1164
1169
use Zend\ServiceManager\AbstractPluginManager;
1170
+ use Zend\ServiceManager\Exception\InvalidServiceException;
1165
1171
use Zend\ServiceManager\Factory\InvokableFactory;
1166
1172
1167
1173
class ObserverPluginManager extends AbstractPluginManager
@@ -1176,14 +1182,21 @@ class ObserverPluginManager extends AbstractPluginManager
1176
1182
];
1177
1183
1178
1184
protected $factories = [
1179
- MailObserver::class => InvokableFactory::class,
1185
+ MailObserver::class => InvokableFactory::class,
1180
1186
LogObserver::class => InvokableFactory::class,
1187
+ // Legacy (v2) due to alias resolution
1188
+ 'mynamespacemailobserver' => InvokableFactory::class,
1189
+ 'mynamespacelogobserver' => InvokableFactory::class,
1181
1190
];
1182
1191
1192
+ protected $shareByDefault = false;
1193
+
1194
+ protected $sharedByDefault = false;
1195
+
1183
1196
public function validate($instance)
1184
1197
{
1185
1198
if (! $plugin instanceof $this->instanceOf) {
1186
- throw new RuntimeException (sprintf(
1199
+ throw new InvalidServiceException (sprintf(
1187
1200
'Invalid plugin "%s" created; not an instance of %s',
1188
1201
get_class($instance),
1189
1202
$this->instanceOf
@@ -1193,7 +1206,11 @@ class ObserverPluginManager extends AbstractPluginManager
1193
1206
1194
1207
public function validatePlugin($instance)
1195
1208
{
1196
- $this->validate($instance);
1209
+ try {
1210
+ $this->validate($instance);
1211
+ } catch (InvalidServiceException $e) {
1212
+ throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
1213
+ }
1197
1214
}
1198
1215
}
1199
1216
```
@@ -1209,16 +1226,71 @@ Things to note about the above:
1209
1226
- The aliases point to the fully qualified class name (FQCN) for the service
1210
1227
being generated, and these are mapped to ` InvokableFactory ` instances. This
1211
1228
means you can also fetch your plugins by their FQCN.
1229
+ - There are also factory entries for the canonicalized FQCN of each factory,
1230
+ which will be used in v2.
1231
+ - ` validatePlugin() ` continues to throw the old exception
1212
1232
1213
1233
The above will now work in both version 2 and version 3.
1214
1234
1235
+ ### Migration testing
1236
+
1237
+ To test your changes, create a new ` MigrationTest ` case that uses the
1238
+ ` CommonPluginManagerTrait ` . Override the ` getPluginManager() ` to return an
1239
+ instance of your plugin manager and override ` getV2InvalidPluginException() `
1240
+ to return the classname of the exception your ` validatePlugin() ` method throws:
1241
+
1242
+ ``` php
1243
+ use MyNamespace\ObserverPluginManager;
1244
+ use MyNamespace\Exception\RuntimeException;
1245
+ use PHPUnit_Framework_TestCase as TestCase;
1246
+ use Zend\ServiceManager\ServiceManager;
1247
+ use ZendTest\ServiceManager\TestAsset\V2v3PluginManager;
1248
+
1249
+ class MigrationTest extends TestCase
1250
+ {
1251
+ use CommonPluginManagerTrait;
1252
+
1253
+ protected function getPluginManager()
1254
+ {
1255
+ return new ObserverPluginManager(new ServiceManager());
1256
+ }
1257
+
1258
+ protected function getV2InvalidPluginException()
1259
+ {
1260
+ return RuntimeException::class;
1261
+ }
1262
+ }
1263
+ ```
1264
+
1265
+ This will check that:
1266
+
1267
+ - You have set the ` $instanceOf ` property
1268
+ - ` $shareByDefault ` and ` $sharedByDefault ` match, if present
1269
+ - That requesting an invalid plugin throws the right exception
1270
+ - That all your aliases resolve
1271
+
1272
+ Note that you will need to include ServiceManager's test directory in the ` autoload-dev ` section of your ` compoer.json `
1273
+ for this test to work:
1274
+
1275
+ ```
1276
+ "autoload-dev": {
1277
+ "psr-4": {
1278
+ "MyNamespaceTest\ObserverPluginManager\\": "test/",
1279
+ "ZendTest\\ServiceManager\\": "vendor/zendframework/zend-servicemanager/test/"
1280
+ }
1281
+ }
1282
+ ```
1283
+
1284
+ ### Post migration
1285
+
1215
1286
After you migrate to version 3, you can clean up your plugin manager:
1216
1287
1217
1288
- Remove the ` validatePlugin() ` method.
1218
1289
- If your ` validate() ` routine is only checking that the instance is of a single
1219
1290
type, and has no other logic, you can remove that implementation as well, as
1220
1291
the ` AbstractPluginManager ` already takes care of that when ` $instanceOf ` is
1221
1292
defined!
1293
+ - Remove the canonicalized FQCN entry for each factory
1222
1294
1223
1295
Performing these steps on the above, we get:
1224
1296
0 commit comments