From 5c38ae8bcc811a0912bf26f78d9e4ef41ec50796 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 1 Nov 2017 12:08:59 -0400
Subject: [PATCH] Adjust interface of abstract plugin factories.

---
 .../VuFind/Search/Options/PluginFactory.php   | 18 +++++++-------
 .../src/VuFind/Search/Params/Factory.php      |  2 +-
 .../VuFind/Search/Params/PluginFactory.php    | 24 +++++++++----------
 .../src/VuFind/Search/Results/Factory.php     | 10 ++++----
 .../VuFind/Search/Results/PluginFactory.php   | 24 ++++++++++---------
 5 files changed, 40 insertions(+), 38 deletions(-)

diff --git a/module/VuFind/src/VuFind/Search/Options/PluginFactory.php b/module/VuFind/src/VuFind/Search/Options/PluginFactory.php
index 04f547f177b..08d98c1bf39 100644
--- a/module/VuFind/src/VuFind/Search/Options/PluginFactory.php
+++ b/module/VuFind/src/VuFind/Search/Options/PluginFactory.php
@@ -27,7 +27,7 @@
  */
 namespace VuFind\Search\Options;
 
-use Zend\ServiceManager\ServiceLocatorInterface;
+use Interop\Container\ContainerInterface;
 
 /**
  * Search options plugin factory
@@ -52,18 +52,20 @@ class PluginFactory extends \VuFind\ServiceManager\AbstractPluginFactory
     /**
      * Create a service for the specified name.
      *
-     * @param ServiceLocatorInterface $serviceLocator Service locator
-     * @param string                  $name           Name of service
-     * @param string                  $requestedName  Unfiltered name of service
+     * @param ContainerInterface $container     Service container
+     * @param string             $requestedName Name of service
+     * @param array              $options       Options (unused)
      *
      * @return object
+     *
+     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
      */
-    public function createServiceWithName(ServiceLocatorInterface $serviceLocator,
-        $name, $requestedName
+    public function __invoke(ContainerInterface $container, $requestedName,
+        array $options = null
     ) {
-        $class = $this->getClassName($name, $requestedName);
+        $class = $this->getClassName($requestedName);
         return new $class(
-            $serviceLocator->getServiceLocator()->get('VuFind\Config')
+            $container->getServiceLocator()->get('VuFind\Config')
         );
     }
 }
diff --git a/module/VuFind/src/VuFind/Search/Params/Factory.php b/module/VuFind/src/VuFind/Search/Params/Factory.php
index ea75523e898..55b4e3ae758 100644
--- a/module/VuFind/src/VuFind/Search/Params/Factory.php
+++ b/module/VuFind/src/VuFind/Search/Params/Factory.php
@@ -53,6 +53,6 @@ class Factory
     {
         $factory = new PluginFactory();
         $helper = $sm->getServiceLocator()->get('VuFind\HierarchicalFacetHelper');
-        return $factory->createServiceWithName($sm, 'solr', 'Solr', [$helper]);
+        return $factory($sm, 'Solr', [$helper]);
     }
 }
diff --git a/module/VuFind/src/VuFind/Search/Params/PluginFactory.php b/module/VuFind/src/VuFind/Search/Params/PluginFactory.php
index 7df0608b777..168a7d5741d 100644
--- a/module/VuFind/src/VuFind/Search/Params/PluginFactory.php
+++ b/module/VuFind/src/VuFind/Search/Params/PluginFactory.php
@@ -27,7 +27,7 @@
  */
 namespace VuFind\Search\Params;
 
-use Zend\ServiceManager\ServiceLocatorInterface;
+use Interop\Container\ContainerInterface;
 
 /**
  * Search params plugin factory
@@ -52,22 +52,22 @@ class PluginFactory extends \VuFind\ServiceManager\AbstractPluginFactory
     /**
      * Create a service for the specified name.
      *
-     * @param ServiceLocatorInterface $serviceLocator Service locator
-     * @param string                  $name           Name of service
-     * @param string                  $requestedName  Unfiltered name of service
-     * @param array                   $extraParams    Extra constructor parameters
-     * (to follow the Options object and config loader)
+     * @param ContainerInterface $container     Service container
+     * @param string             $requestedName Name of service
+     * @param array              $extras        Extra options
      *
      * @return object
+     *
+     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
      */
-    public function createServiceWithName(ServiceLocatorInterface $serviceLocator,
-        $name, $requestedName, array $extraParams = []
+    public function __invoke(ContainerInterface $container, $requestedName,
+        array $extras = null
     ) {
-        $options = $serviceLocator->getServiceLocator()
+        $options = $container->getServiceLocator()
             ->get('VuFind\SearchOptionsPluginManager')->get($requestedName);
-        $class = $this->getClassName($name, $requestedName);
-        $configLoader = $serviceLocator->getServiceLocator()->get('VuFind\Config');
+        $class = $this->getClassName($requestedName);
+        $configLoader = $container->getServiceLocator()->get('VuFind\Config');
         // Clone the options instance in case caller modifies it:
-        return new $class(clone $options, $configLoader, ...$extraParams);
+        return new $class(clone $options, $configLoader, ...($extras ?: []));
     }
 }
diff --git a/module/VuFind/src/VuFind/Search/Results/Factory.php b/module/VuFind/src/VuFind/Search/Results/Factory.php
index 2091189c832..0dad692de6a 100644
--- a/module/VuFind/src/VuFind/Search/Results/Factory.php
+++ b/module/VuFind/src/VuFind/Search/Results/Factory.php
@@ -53,8 +53,8 @@ class Factory
     {
         $factory = new PluginFactory();
         $tm = $sm->getServiceLocator()->get('VuFind\DbTablePluginManager');
-        $obj = $factory->createServiceWithName(
-            $sm, 'favorites', 'Favorites',
+        $obj = $factory(
+            $sm, 'Favorites',
             [$tm->get('Resource'), $tm->get('UserList')]
         );
         $init = new \ZfcRbac\Initializer\AuthorizationServiceInitializer();
@@ -72,7 +72,7 @@ class Factory
     public static function getSolr(ServiceManager $sm)
     {
         $factory = new PluginFactory();
-        $solr = $factory->createServiceWithName($sm, 'solr', 'Solr');
+        $solr = $factory($sm, 'Solr');
         $config = $sm->getServiceLocator()
             ->get('VuFind\Config')->get('config');
         $spellConfig = isset($config->Spelling)
@@ -94,8 +94,6 @@ class Factory
     {
         $factory = new PluginFactory();
         $tm = $sm->getServiceLocator()->get('VuFind\DbTablePluginManager');
-        return $factory->createServiceWithName(
-            $sm, 'tags', 'Tags', [$tm->get('Tags')]
-        );
+        return $factory($sm, 'Tags', [$tm->get('Tags')]);
     }
 }
diff --git a/module/VuFind/src/VuFind/Search/Results/PluginFactory.php b/module/VuFind/src/VuFind/Search/Results/PluginFactory.php
index 79d27a75975..f12b0d56bbc 100644
--- a/module/VuFind/src/VuFind/Search/Results/PluginFactory.php
+++ b/module/VuFind/src/VuFind/Search/Results/PluginFactory.php
@@ -27,7 +27,7 @@
  */
 namespace VuFind\Search\Results;
 
-use Zend\ServiceManager\ServiceLocatorInterface;
+use Interop\Container\ContainerInterface;
 
 /**
  * Search results plugin factory
@@ -52,24 +52,26 @@ class PluginFactory extends \VuFind\ServiceManager\AbstractPluginFactory
     /**
      * Create a service for the specified name.
      *
-     * @param ServiceLocatorInterface $serviceLocator Service locator
-     * @param string                  $name           Name of service
-     * @param string                  $requestedName  Unfiltered name of service
-     * @param array                   $extraParams    Extra constructor parameters
-     * (to follow the Params, Search and RecordLoader objects)
+     * @param ContainerInterface $container     Service container
+     * @param string             $requestedName Name of service
+     * @param array              $extras        Extra options
      *
      * @return object
+     *
+     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
      */
-    public function createServiceWithName(ServiceLocatorInterface $serviceLocator,
-        $name, $requestedName, array $extraParams = []
+    public function __invoke(ContainerInterface $container, $requestedName,
+        array $extras = null
     ) {
-        $params = $serviceLocator->getServiceLocator()
+        $params = $container->getServiceLocator()
             ->get('VuFind\SearchParamsPluginManager')->get($requestedName);
         $searchService = $serviceLocator->getServiceLocator()
             ->get('VuFind\Search');
         $recordLoader = $serviceLocator->getServiceLocator()
             ->get('VuFind\RecordLoader');
-        $class = $this->getClassName($name, $requestedName);
-        return new $class($params, $searchService, $recordLoader, ...$extraParams);
+        $class = $this->getClassName($requestedName);
+        return new $class(
+            $params, $searchService, $recordLoader, ...($extras ?: [])
+        );
     }
 }
-- 
GitLab