From d5a5b50b587700fcf870cc7512469e641c168c5e Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 4 Sep 2012 12:39:58 -0400
Subject: [PATCH] Created plugin manager for autocomplete handlers.

---
 module/VuFind/config/module.config.php        | 22 +++++++
 .../Autocomplete/AutocompleteInterface.php    | 12 ++++
 .../VuFind/src/VuFind/Autocomplete/None.php   | 27 +++++----
 .../VuFind/Autocomplete/OCLCIdentities.php    | 30 +++++-----
 .../src/VuFind/Autocomplete/PluginFactory.php | 48 +++++++++++++++
 .../{Factory.php => PluginManager.php}        | 60 ++++++-------------
 .../VuFind/src/VuFind/Autocomplete/Solr.php   | 16 +++--
 .../src/VuFind/Autocomplete/SolrAuth.php      |  8 +--
 .../VuFind/src/VuFind/Autocomplete/SolrCN.php | 14 +++--
 .../src/VuFind/Autocomplete/SolrReserves.php  |  9 +--
 module/VuFind/src/VuFind/Autocomplete/Tag.php | 27 +++++----
 module/VuFind/src/VuFind/Bootstrap.php        |  5 ++
 .../src/VuFind/Controller/AjaxController.php  |  4 +-
 .../VuFind/Controller/SearchController.php    |  4 +-
 14 files changed, 179 insertions(+), 107 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/Autocomplete/PluginFactory.php
 rename module/VuFind/src/VuFind/Autocomplete/{Factory.php => PluginManager.php} (65%)

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 7b6bd58ca79..5455e0ddf04 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -76,6 +76,28 @@ $config = array(
             'sip' => 'Sip2',
         ),
     ),
+    'autocomplete_handler_manager' => array(
+        'abstract_factories' => array('VuFind\Autocomplete\PluginFactory'),
+        'invokables' => array(
+            'none' => 'VuFind\Autocomplete\None',
+            'oclcidentities' => 'VuFind\Autocomplete\OCLCIdentities',
+            'solr' => 'VuFind\Autocomplete\Solr',
+            'solrauth' => 'VuFind\Autocomplete\SolrAuth',
+            'solrcn' => 'VuFind\Autocomplete\SolrCN',
+            'solrreserves' => 'VuFind\Autocomplete\SolrReserves',
+            'tag' => 'VuFind\Autocomplete\Tag',
+        ),
+        'aliases' => array(
+            // for legacy 1.x compatibility
+            'noautocomplete' => 'None',
+            'oclcidentitiesautocomplete' => 'OCLCIdentities',
+            'solrautocomplete' => 'Solr',
+            'solrauthautocomplete' => 'SolrAuth',
+            'solrcnautocomplete' => 'SolrCN',
+            'solrreservesautocomplete' => 'SolrReserves',
+            'tagautocomplete' => 'Tag',
+        ),
+    ),
     'controllers' => array(
         'invokables' => array(
             'admin' => 'VuFind\Controller\AdminController',
diff --git a/module/VuFind/src/VuFind/Autocomplete/AutocompleteInterface.php b/module/VuFind/src/VuFind/Autocomplete/AutocompleteInterface.php
index d5069abfd94..96b18a39464 100644
--- a/module/VuFind/src/VuFind/Autocomplete/AutocompleteInterface.php
+++ b/module/VuFind/src/VuFind/Autocomplete/AutocompleteInterface.php
@@ -53,4 +53,16 @@ interface AutocompleteInterface
      * @return array        The suggestions for the provided query
      */
     public function getSuggestions($query);
+
+    /**
+     * setConfig
+     *
+     * Set parameters that affect the behavior of the autocomplete handler.
+     * These values normally come from the search configuration file.
+     *
+     * @param string $params Parameters to set
+     *
+     * @return void
+     */
+    public function setConfig($params);
 }
diff --git a/module/VuFind/src/VuFind/Autocomplete/None.php b/module/VuFind/src/VuFind/Autocomplete/None.php
index 485df18eb88..dbee88328fd 100644
--- a/module/VuFind/src/VuFind/Autocomplete/None.php
+++ b/module/VuFind/src/VuFind/Autocomplete/None.php
@@ -41,18 +41,6 @@ namespace VuFind\Autocomplete;
  */
 class None implements AutocompleteInterface
 {
-    /**
-     * Constructor
-     *
-     * Establishes base settings for making autocomplete suggestions.
-     *
-     * @param string $params Additional settings from searches.ini.
-     */
-    public function __construct($params)
-    {
-        // No parameters
-    }
-
     /**
      * getSuggestions
      *
@@ -68,4 +56,19 @@ class None implements AutocompleteInterface
         // No suggestions
         return array();
     }
+
+    /**
+     * setConfig
+     *
+     * Set parameters that affect the behavior of the autocomplete handler.
+     * These values normally come from the search configuration file.
+     *
+     * @param string $params Parameters to set
+     *
+     * @return void
+     */
+    public function setConfig($params)
+    {
+        // Ignore all parameters
+    }
 }
diff --git a/module/VuFind/src/VuFind/Autocomplete/OCLCIdentities.php b/module/VuFind/src/VuFind/Autocomplete/OCLCIdentities.php
index 13c5e8ea634..74eff8e736d 100644
--- a/module/VuFind/src/VuFind/Autocomplete/OCLCIdentities.php
+++ b/module/VuFind/src/VuFind/Autocomplete/OCLCIdentities.php
@@ -41,20 +41,7 @@ namespace VuFind\Autocomplete;
  */
 class OCLCIdentities implements AutocompleteInterface
 {
-    protected $url;
-
-    /**
-     * Constructor
-     *
-     * Establishes base settings for making autocomplete suggestions.
-     *
-     * @param string $params Additional settings from searches.ini.
-     */
-    public function __construct($params)
-    {
-        // For now, incoming parameters are ignored and a hard-coded URL is used:
-        $this->url = 'http://worldcat.org/identities/AutoSuggest';
-    }
+    protected $url = 'http://worldcat.org/identities/AutoSuggest';
 
     /**
      * getSuggestions
@@ -89,4 +76,19 @@ class OCLCIdentities implements AutocompleteInterface
         // Send back results:
         return array_unique($results);
     }
+
+    /**
+     * setConfig
+     *
+     * Set parameters that affect the behavior of the autocomplete handler.
+     * These values normally come from the search configuration file.
+     *
+     * @param string $params Parameters to set
+     *
+     * @return void
+     */
+    public function setConfig($params)
+    {
+        // For now, incoming parameters are ignored.
+    }
 }
diff --git a/module/VuFind/src/VuFind/Autocomplete/PluginFactory.php b/module/VuFind/src/VuFind/Autocomplete/PluginFactory.php
new file mode 100644
index 00000000000..cb26dcfea95
--- /dev/null
+++ b/module/VuFind/src/VuFind/Autocomplete/PluginFactory.php
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Autocomplete handler plugin factory
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2010.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind2
+ * @package  Session_Handlers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/creating_a_session_handler Wiki
+ */
+namespace VuFind\Autocomplete;
+
+/**
+ * Autocomplete handler plugin factory
+ *
+ * @category VuFind2
+ * @package  Session_Handlers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/creating_a_session_handler Wiki
+ */
+class PluginFactory extends \VuFind\ServiceManager\AbstractPluginFactory
+{
+    /**
+     * Constructor
+     */
+    public function __construct()
+    {
+        $this->defaultNamespace = 'VuFind\Autocomplete';
+    }
+}
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/Autocomplete/Factory.php b/module/VuFind/src/VuFind/Autocomplete/PluginManager.php
similarity index 65%
rename from module/VuFind/src/VuFind/Autocomplete/Factory.php
rename to module/VuFind/src/VuFind/Autocomplete/PluginManager.php
index 13c8034e29d..dff684fc82f 100644
--- a/module/VuFind/src/VuFind/Autocomplete/Factory.php
+++ b/module/VuFind/src/VuFind/Autocomplete/PluginManager.php
@@ -1,6 +1,6 @@
 <?php
 /**
- * Code for generating Autocomplete objects
+ * Autocomplete handler plugin manager
  *
  * PHP version 5
  *
@@ -20,60 +20,34 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * @category VuFind2
- * @package  Autocomplete
+ * @package  Session_Handlers
  * @author   Demian Katz <demian.katz@villanova.edu>
- * @author   Chris Hallberg <challber@villanova.edu>
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     http://vufind.org/wiki/autocomplete Wiki
+ * @link     http://vufind.org/wiki/creating_a_session_handler Wiki
  */
 namespace VuFind\Autocomplete;
 use VuFind\Config\Reader as ConfigReader, VuFind\Search\Options as SearchOptions;
 
 /**
- * Code for generating Autocomplete objects
- *
- * This is a factory class to build autocomplete modules for use in searches.
+ * Autocomplete handler plugin manager
  *
  * @category VuFind2
- * @package  Autocomplete
+ * @package  Session_Handlers
  * @author   Demian Katz <demian.katz@villanova.edu>
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     http://vufind.org/wiki/autocomplete Wiki
+ * @link     http://vufind.org/wiki/creating_a_session_handler Wiki
  */
-class Factory
+class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
 {
     /**
-     * initRecommendation
-     *
-     * This constructs an autocomplete plug-in object.
-     *
-     * @param string $module The name of the autocomplete module to build
-     * @param string $params Configuration string to send to the constructor
+     * Return the name of the base class or interface that plug-ins must conform
+     * to.
      *
-     * @return mixed         The $module object on success, false otherwise
+     * @return string
      */
-    public static function initAutocomplete($module, $params)
+    protected function getExpectedInterface()
     {
-        // backward compatibility with VuFind 1.x names:
-        switch ($module) {
-        case 'NoAutocomplete':
-            $module = 'None';
-            break;
-        default:
-            $module = str_replace('Autocomplete', '', $module);
-            break;
-        }
-
-        // Try to load the appropriate class, if any:
-        if (!empty($module)) {
-            $module = 'VuFind\Autocomplete\\' . $module;
-            if (class_exists($module)) {
-                $auto = new $module($params);
-                return $auto;
-            }
-        }
-
-        return false;
+        return 'VuFind\Autocomplete\AutocompleteInterface';
     }
 
     /**
@@ -91,9 +65,8 @@ class Factory
      *
      * @return array
      */
-    public static function getSuggestions($request, $typeParam = 'type',
-        $queryParam = 'q'
-    ) {
+    public function getSuggestions($request, $typeParam = 'type', $queryParam = 'q')
+    {
         // Process incoming parameters:
         $type = $request->get($typeParam, '');
         $query = $request->get($queryParam, '');
@@ -120,10 +93,11 @@ class Factory
                 $module .= ':'; // force colon to avoid warning in explode below
             }
             list($name, $params) = explode(':', $module, 2);
-            $handler = self::initAutocomplete($name, $params);
+            $handler = $this->get($name);
+            $handler->setConfig($params);
         }
 
         return (isset($handler) && is_object($handler))
             ? array_values($handler->getSuggestions($query)) : array();
     }
-}
+}
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/Autocomplete/Solr.php b/module/VuFind/src/VuFind/Autocomplete/Solr.php
index 8464fc4ed4c..cc989178fbf 100644
--- a/module/VuFind/src/VuFind/Autocomplete/Solr.php
+++ b/module/VuFind/src/VuFind/Autocomplete/Solr.php
@@ -48,14 +48,18 @@ class Solr implements AutocompleteInterface
     protected $filters;
     protected $searchObject;
 
+
     /**
-     * Constructor
+     * setConfig
+     *
+     * Set parameters that affect the behavior of the autocomplete handler.
+     * These values normally come from the search configuration file.
      *
-     * Establishes base settings for making autocomplete suggestions.
+     * @param string $params Parameters to set
      *
-     * @param string $params Additional settings from searches.ini.
+     * @return void
      */
-    public function __construct($params)
+    public function setConfig($params)
     {
         // Save the basic parameters:
         $params = explode(':', $params);
@@ -125,6 +129,10 @@ class Solr implements AutocompleteInterface
      */
     public function getSuggestions($query)
     {
+        if (!is_object($this->searchObject)) {
+            throw new \Exception('Please set configuration first.');
+        }
+
         try {
             $this->searchObject->getParams()->setBasicSearch(
                 $this->mungeQuery($query), $this->handler
diff --git a/module/VuFind/src/VuFind/Autocomplete/SolrAuth.php b/module/VuFind/src/VuFind/Autocomplete/SolrAuth.php
index afb6f75b00c..daa42eb352b 100644
--- a/module/VuFind/src/VuFind/Autocomplete/SolrAuth.php
+++ b/module/VuFind/src/VuFind/Autocomplete/SolrAuth.php
@@ -43,16 +43,10 @@ class SolrAuth extends Solr
 {
     /**
      * Constructor
-     *
-     * Establishes base settings for making autocomplete suggestions.
-     *
-     * @param string $params Additional settings from searches.ini.
      */
-    public function __construct($params)
+    public function __construct()
     {
-        // Use a different default field; otherwise, behave the same as the parent:
         $this->defaultDisplayField = 'heading';
-        parent::__construct($params);
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Autocomplete/SolrCN.php b/module/VuFind/src/VuFind/Autocomplete/SolrCN.php
index 0ec54cf6869..aa8c3de625a 100644
--- a/module/VuFind/src/VuFind/Autocomplete/SolrCN.php
+++ b/module/VuFind/src/VuFind/Autocomplete/SolrCN.php
@@ -42,15 +42,19 @@ namespace VuFind\Autocomplete;
 class SolrCN extends Solr
 {
     /**
-     * Constructor
+     * setConfig
      *
-     * Establishes base settings for making autocomplete suggestions.
+     * Set parameters that affect the behavior of the autocomplete handler.
+     * These values normally come from the search configuration file.
      *
-     * @param string $params Additional settings from searches.ini.
+     * @param string $params Parameters to set
+     *
+     * @return void
      */
-    public function __construct($params)
+    public function setConfig($params)
     {
-        parent::__construct('CallNumber');
+        // Ignore incoming configuration and force CallNumber settings.
+        parent::setConfig('CallNumber');
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Autocomplete/SolrReserves.php b/module/VuFind/src/VuFind/Autocomplete/SolrReserves.php
index caf15fdff8c..e6a91ec5a2c 100644
--- a/module/VuFind/src/VuFind/Autocomplete/SolrReserves.php
+++ b/module/VuFind/src/VuFind/Autocomplete/SolrReserves.php
@@ -43,17 +43,10 @@ class SolrReserves extends Solr
 {
     /**
      * Constructor
-     *
-     * Establishes base settings for making autocomplete suggestions.
-     *
-     * @param string $params Additional settings from searches.ini.
      */
-    public function __construct($params)
+    public function __construct()
     {
-        // Use a different default field; otherwise, behave the same as the parent:
         $this->defaultDisplayField = 'course';
-
-        parent::__construct($params);
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Autocomplete/Tag.php b/module/VuFind/src/VuFind/Autocomplete/Tag.php
index 9fcac8f7094..5debdda2930 100644
--- a/module/VuFind/src/VuFind/Autocomplete/Tag.php
+++ b/module/VuFind/src/VuFind/Autocomplete/Tag.php
@@ -41,18 +41,6 @@ use VuFind\Db\Table\Tags as TagsTable;
  */
 class Tag implements AutocompleteInterface
 {
-    /**
-     * Constructor
-     *
-     * Establishes base settings for making autocomplete suggestions.
-     *
-     * @param string $params Additional settings from searches.ini.
-     */
-    public function __construct($params)
-    {
-        // No parameters
-    }
-
     /**
      * getSuggestions
      *
@@ -75,4 +63,19 @@ class Tag implements AutocompleteInterface
         }
         return $tagList;
     }
+
+    /**
+     * setConfig
+     *
+     * Set parameters that affect the behavior of the autocomplete handler.
+     * These values normally come from the search configuration file.
+     *
+     * @param string $params Parameters to set
+     *
+     * @return void
+     */
+    public function setConfig($params)
+    {
+        // Ignore all parameters
+    }
 }
diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php
index 11199887649..030688273f5 100644
--- a/module/VuFind/src/VuFind/Bootstrap.php
+++ b/module/VuFind/src/VuFind/Bootstrap.php
@@ -93,6 +93,11 @@ class Bootstrap
                 new ServiceManagerConfig($config['auth_handler_manager'])
             )
         );
+        $serviceManager->setService(
+            'AutocompleteHandlerManager', new \VuFind\Autocomplete\PluginManager(
+                new ServiceManagerConfig($config['autocomplete_handler_manager'])
+            )
+        );
         $serviceManager->setService(
             'SessionHandlerManager', new \VuFind\Session\PluginManager(
                 new ServiceManagerConfig($config['session_handler_manager'])
diff --git a/module/VuFind/src/VuFind/Controller/AjaxController.php b/module/VuFind/src/VuFind/Controller/AjaxController.php
index 47bd5546193..7fe6223f5f8 100644
--- a/module/VuFind/src/VuFind/Controller/AjaxController.php
+++ b/module/VuFind/src/VuFind/Controller/AjaxController.php
@@ -917,8 +917,10 @@ class AjaxController extends AbstractBase
     public function getACSuggestions()
     {
         $query = $this->getRequest()->getQuery();
+        $autocompleteManager = $this->getServiceLocator()
+            ->get('AutocompleteHandlerManager');
         return $this->output(
-            \VuFind\Autocomplete\Factory::getSuggestions($query), self::STATUS_OK
+            $autocompleteManager->getSuggestions($query), self::STATUS_OK
         );
     }
 
diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php
index 7139ae03b7c..2b64abfbfe3 100644
--- a/module/VuFind/src/VuFind/Controller/SearchController.php
+++ b/module/VuFind/src/VuFind/Controller/SearchController.php
@@ -559,7 +559,9 @@ class SearchController extends AbstractSearch
 
         // Get suggestions and make sure they are an array (we don't want to JSON
         // encode them into an object):
-        $suggestions = \VuFind\Autocomplete\Factory::getSuggestions(
+        $autocompleteManager = $this->getServiceLocator()
+            ->get('AutocompleteHandlerManager');
+        $suggestions = $autocompleteManager->getSuggestions(
             $query, 'type', 'lookfor'
         );
 
-- 
GitLab