From ed3f25859ecc48d8d4c0d5cbf0ddf7492e777552 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Thu, 14 Dec 2017 08:10:07 -0500 Subject: [PATCH] Factored out Suggester class for cleaner design. - Eliminates deprecation warnings about getServiceLocator(). --- module/VuFind/config/module.config.php | 1 + .../src/VuFind/Autocomplete/PluginManager.php | 63 -------- .../src/VuFind/Autocomplete/Suggester.php | 140 ++++++++++++++++++ .../VuFind/Autocomplete/SuggesterFactory.php | 68 +++++++++ .../src/VuFind/Controller/AjaxController.php | 7 +- .../VuFind/Controller/SearchController.php | 7 +- 6 files changed, 213 insertions(+), 73 deletions(-) create mode 100644 module/VuFind/src/VuFind/Autocomplete/Suggester.php create mode 100644 module/VuFind/src/VuFind/Autocomplete/SuggesterFactory.php diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 962b601ba04..29884fd60de 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -274,6 +274,7 @@ $config = [ 'VuFind\AuthManager' => 'VuFind\Auth\Factory::getManager', 'VuFind\AuthPluginManager' => 'VuFind\Service\Factory::getAuthPluginManager', 'VuFind\AutocompletePluginManager' => 'VuFind\Service\Factory::getAutocompletePluginManager', + 'VuFind\Autocomplete\Suggester' => 'VuFind\Autocomplete\SuggesterFactory', 'VuFind\CacheManager' => 'VuFind\Service\Factory::getCacheManager', 'VuFind\Cart' => 'VuFind\Service\Factory::getCart', 'VuFind\ChannelProviderPluginManager' => 'VuFind\Service\Factory::getChannelProviderPluginManager', diff --git a/module/VuFind/src/VuFind/Autocomplete/PluginManager.php b/module/VuFind/src/VuFind/Autocomplete/PluginManager.php index 089f4b6f70f..1b9acbafcdf 100644 --- a/module/VuFind/src/VuFind/Autocomplete/PluginManager.php +++ b/module/VuFind/src/VuFind/Autocomplete/PluginManager.php @@ -48,67 +48,4 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager { return 'VuFind\Autocomplete\AutocompleteInterface'; } - - /** - * This returns an array of suggestions based on current request parameters. - * This logic is present in the factory class so that it can be easily shared - * by multiple AJAX handlers. - * - * @param \Zend\Stdlib\Parameters $request The user request - * @param string $typeParam Request parameter containing search - * type - * @param string $queryParam Request parameter containing query - * string - * - * @return array - */ - public function getSuggestions($request, $typeParam = 'type', $queryParam = 'q') - { - // Process incoming parameters: - $type = $request->get($typeParam, ''); - $query = $request->get($queryParam, ''); - $searcher = $request->get('searcher', 'Solr'); - $hiddenFilters = $request->get('hiddenFilters', []); - - // If we're using a combined search box, we need to override the searcher - // and type settings. - if (substr($type, 0, 7) == 'VuFind:') { - list(, $tmp) = explode(':', $type, 2); - list($searcher, $type) = explode('|', $tmp, 2); - } - - // get Autocomplete_Type config - $options = $this->getServiceLocator() - ->get('VuFind\SearchOptionsPluginManager')->get($searcher); - $config = $this->getServiceLocator()->get('VuFind\Config') - ->get($options->getSearchIni()); - $types = isset($config->Autocomplete_Types) ? - $config->Autocomplete_Types->toArray() : []; - - // Figure out which handler to use: - if (!empty($type) && isset($types[$type])) { - $module = $types[$type]; - } elseif (isset($config->Autocomplete->default_handler)) { - $module = $config->Autocomplete->default_handler; - } else { - $module = false; - } - - // Get suggestions: - if ($module) { - if (strpos($module, ':') === false) { - $module .= ':'; // force colon to avoid warning in explode below - } - list($name, $params) = explode(':', $module, 2); - $handler = $this->get($name); - $handler->setConfig($params); - } - - if (is_callable([$handler, 'addFilters'])) { - $handler->addFilters($hiddenFilters); - } - - return (isset($handler) && is_object($handler)) - ? array_values($handler->getSuggestions($query)) : []; - } } diff --git a/module/VuFind/src/VuFind/Autocomplete/Suggester.php b/module/VuFind/src/VuFind/Autocomplete/Suggester.php new file mode 100644 index 00000000000..36eee4621f2 --- /dev/null +++ b/module/VuFind/src/VuFind/Autocomplete/Suggester.php @@ -0,0 +1,140 @@ +<?php +/** + * Autocomplete handler plugin manager + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package Autocomplete + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:plugins:autosuggesters Wiki + */ +namespace VuFind\Autocomplete; + +use VuFind\Config\PluginManager as ConfigManager; +use VuFind\Search\Options\PluginManager as OptionsManager; + +/** + * Autocomplete handler plugin manager + * + * @category VuFind + * @package Autocomplete + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:plugins:autosuggesters Wiki + */ +class Suggester +{ + /** + * Autocomplete plugin manager. + * + * @var PluginManager + */ + protected $pluginManager = null; + + /** + * Search options plugin manager. + * + * @var OptionsManager + */ + protected $optionsManager = null; + + /** + * Configuration manager. + * + * @var ConfigManager + */ + protected $configManager = null; + + /** + * Constructor + * + * @param PluginManager $pm Autocomplete plugin manager + * @param ConfigManager $cm Config manager + * @param OptionsManager $om Options manager + */ + public function __construct(PluginManager $pm, ConfigManager $cm, + OptionsManager $om + ) { + $this->pluginManager = $pm; + $this->configManager = $cm; + $this->optionsManager = $om; + } + + /** + * This returns an array of suggestions based on current request parameters. + * This logic is present in the factory class so that it can be easily shared + * by multiple AJAX handlers. + * + * @param \Zend\Stdlib\Parameters $request The user request + * @param string $typeParam Request parameter containing search + * type + * @param string $queryParam Request parameter containing query + * string + * + * @return array + */ + public function getSuggestions($request, $typeParam = 'type', $queryParam = 'q') + { + // Process incoming parameters: + $type = $request->get($typeParam, ''); + $query = $request->get($queryParam, ''); + $searcher = $request->get('searcher', 'Solr'); + $hiddenFilters = $request->get('hiddenFilters', []); + + // If we're using a combined search box, we need to override the searcher + // and type settings. + if (substr($type, 0, 7) == 'VuFind:') { + list(, $tmp) = explode(':', $type, 2); + list($searcher, $type) = explode('|', $tmp, 2); + } + + // get Autocomplete_Type config + $options = $this->optionsManager->get($searcher); + $config = $this->configManager->get($options->getSearchIni()); + $types = isset($config->Autocomplete_Types) ? + $config->Autocomplete_Types->toArray() : []; + + // Figure out which handler to use: + if (!empty($type) && isset($types[$type])) { + $module = $types[$type]; + } elseif (isset($config->Autocomplete->default_handler)) { + $module = $config->Autocomplete->default_handler; + } else { + $module = false; + } + + // Get suggestions: + if ($module) { + if (strpos($module, ':') === false) { + $module .= ':'; // force colon to avoid warning in explode below + } + list($name, $params) = explode(':', $module, 2); + $handler = $this->pluginManager->get($name); + $handler->setConfig($params); + } + + if (is_callable([$handler, 'addFilters'])) { + $handler->addFilters($hiddenFilters); + } + + return (isset($handler) && is_object($handler)) + ? array_values($handler->getSuggestions($query)) : []; + } +} diff --git a/module/VuFind/src/VuFind/Autocomplete/SuggesterFactory.php b/module/VuFind/src/VuFind/Autocomplete/SuggesterFactory.php new file mode 100644 index 00000000000..67cd2b964c4 --- /dev/null +++ b/module/VuFind/src/VuFind/Autocomplete/SuggesterFactory.php @@ -0,0 +1,68 @@ +<?php +/** + * Factory for autocomplete suggester. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2017. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package Autocomplete + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development Wiki + */ +namespace VuFind\Autocomplete; + +use Interop\Container\ContainerInterface; + +/** + * Factory for autocomplete suggester. + * + * @category VuFind + * @package Autocomplete + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development Wiki + */ +class SuggesterFactory implements \Zend\ServiceManager\Factory\FactoryInterface +{ + /** + * Create an object + * + * @param ContainerInterface $container Service manager + * @param string $requestedName Service being created + * @param null|array $options Extra options (optional) + * + * @return object + * + * @throws ServiceNotFoundException if unable to resolve the service. + * @throws ServiceNotCreatedException if an exception is raised when + * creating a service. + * @throws ContainerException if any other error occurs + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function __invoke(ContainerInterface $container, $requestedName, + array $options = null + ) { + return new $requestedName( + $container->get('VuFind\AutocompletePluginManager'), + $container->get('VuFind\Config'), + $container->get('VuFind\SearchOptionsPluginManager') + ); + } +} diff --git a/module/VuFind/src/VuFind/Controller/AjaxController.php b/module/VuFind/src/VuFind/Controller/AjaxController.php index 901eee60cc5..af24596602a 100644 --- a/module/VuFind/src/VuFind/Controller/AjaxController.php +++ b/module/VuFind/src/VuFind/Controller/AjaxController.php @@ -922,11 +922,8 @@ class AjaxController extends AbstractBase { $this->disableSessionWrites(); // avoid session write timing bug $query = $this->getRequest()->getQuery(); - $autocompleteManager = $this->serviceLocator - ->get('VuFind\AutocompletePluginManager'); - return $this->output( - $autocompleteManager->getSuggestions($query), self::STATUS_OK - ); + $suggester = $this->serviceLocator->get('VuFind\Autocomplete\Suggester'); + return $this->output($suggester->getSuggestions($query), self::STATUS_OK); } /** diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php index 5b00a26d5ff..bbfb12cca1f 100644 --- a/module/VuFind/src/VuFind/Controller/SearchController.php +++ b/module/VuFind/src/VuFind/Controller/SearchController.php @@ -660,11 +660,8 @@ class SearchController extends AbstractSearch // Get suggestions and make sure they are an array (we don't want to JSON // encode them into an object): - $autocompleteManager = $this->serviceLocator - ->get('VuFind\AutocompletePluginManager'); - $suggestions = $autocompleteManager->getSuggestions( - $query, 'type', 'lookfor' - ); + $suggester = $this->serviceLocator->get('VuFind\Autocomplete\Suggester'); + $suggestions = $suggester->getSuggestions($query, 'type', 'lookfor'); // Send the JSON response: $response = $this->getResponse(); -- GitLab