From bcda02a2e610e941e74443b9f8a9e5564ad93ffc Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Tue, 17 Dec 2019 15:28:03 -0500 Subject: [PATCH] Make UrlQueryHelper more extensible. (#1523) --- module/VuFind/config/module.config.php | 1 + .../VuFind/Controller/SearchController.php | 2 +- .../VuFind/src/VuFind/Search/Base/Results.php | 34 ++++++++++++++++++- .../Search/Factory/UrlQueryHelperFactory.php | 9 ++++- .../VuFind/Search/Results/ResultsFactory.php | 7 +++- .../VuFind/src/VuFindTest/Unit/TestCase.php | 5 +++ 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 4e976ef563d..fc4f6273914 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -411,6 +411,7 @@ $config = [ 'VuFind\Search\History' => 'VuFind\Search\HistoryFactory', 'VuFind\Search\Memory' => 'VuFind\Search\MemoryFactory', 'VuFind\Search\FacetCache\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory', + 'VuFind\Search\Factory\UrlQueryHelperFactory' => 'Zend\ServiceManager\Factory\InvokableFactory', 'VuFind\Search\Options\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory', 'VuFind\Search\Params\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory', 'VuFind\Search\Results\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory', diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php index cddf5e7da4c..b3cf701f282 100644 --- a/module/VuFind/src/VuFind/Controller/SearchController.php +++ b/module/VuFind/src/VuFind/Controller/SearchController.php @@ -74,7 +74,7 @@ class SearchController extends AbstractSolrSearch // Retrieve and manipulate the parameters: $searchHelper = $this->getViewRenderer()->plugin('searchMemory'); $params = $searchHelper->getLastSearchParams($searchClassId); - $factory = new UrlQueryHelperFactory(); + $factory = $this->serviceLocator->get(UrlQueryHelperFactory::class); $initialParams = $factory->fromParams($params); if ($removeAllFilters) { diff --git a/module/VuFind/src/VuFind/Search/Base/Results.php b/module/VuFind/src/VuFind/Search/Base/Results.php index 6b99ea66048..032870307d0 100644 --- a/module/VuFind/src/VuFind/Search/Base/Results.php +++ b/module/VuFind/src/VuFind/Search/Base/Results.php @@ -151,6 +151,13 @@ abstract class Results */ protected $recordLoader; + /** + * URL query helper factory + * + * @var UrlQueryHelperFactory + */ + protected $urlQueryHelperFactory = null; + /** * Constructor * @@ -231,7 +238,7 @@ abstract class Results { // Set up URL helper: if (!isset($this->helpers['urlQuery'])) { - $factory = new UrlQueryHelperFactory(); + $factory = $this->getUrlQueryHelperFactory(); $this->helpers['urlQuery'] = $factory->fromParams( $this->getParams(), $this->getUrlQueryHelperOptions() ); @@ -595,6 +602,31 @@ abstract class Results ); } + /** + * Get URL query helper factory + * + * @return UrlQueryHelperFactory + */ + protected function getUrlQueryHelperFactory() + { + if (null === $this->urlQueryHelperFactory) { + $this->urlQueryHelperFactory = new UrlQueryHelperFactory(); + } + return $this->urlQueryHelperFactory; + } + + /** + * Set URL query helper factory + * + * @param UrlQueryHelperFactory $factory UrlQueryHelperFactory object + * + * @return void + */ + public function setUrlQueryHelperFactory(UrlQueryHelperFactory $factory) + { + $this->urlQueryHelperFactory = $factory; + } + /** * Get complete facet counts for several index fields * diff --git a/module/VuFind/src/VuFind/Search/Factory/UrlQueryHelperFactory.php b/module/VuFind/src/VuFind/Search/Factory/UrlQueryHelperFactory.php index edb9aaf2efc..21bf2fd0559 100644 --- a/module/VuFind/src/VuFind/Search/Factory/UrlQueryHelperFactory.php +++ b/module/VuFind/src/VuFind/Search/Factory/UrlQueryHelperFactory.php @@ -41,6 +41,13 @@ use VuFind\Search\UrlQueryHelper; */ class UrlQueryHelperFactory { + /** + * Name of class built by factory. + * + * @var string + */ + protected $helperClass = UrlQueryHelper::class; + /** * Extract default settings from the search parameters. * @@ -158,7 +165,7 @@ class UrlQueryHelperFactory public function fromParams(Params $params, array $config = []) { $finalConfig = $this->addDefaultsToConfig($params, $config); - return new UrlQueryHelper( + return new $this->helperClass( $this->getUrlParams($params, $finalConfig), $params->getQuery(), $finalConfig diff --git a/module/VuFind/src/VuFind/Search/Results/ResultsFactory.php b/module/VuFind/src/VuFind/Search/Results/ResultsFactory.php index cc7bf07726f..cddcfeb5797 100644 --- a/module/VuFind/src/VuFind/Search/Results/ResultsFactory.php +++ b/module/VuFind/src/VuFind/Search/Results/ResultsFactory.php @@ -28,6 +28,7 @@ namespace VuFind\Search\Results; use Interop\Container\ContainerInterface; +use VuFind\Search\Factory\UrlQueryHelperFactory; use Zend\ServiceManager\Factory\FactoryInterface; /** @@ -64,8 +65,12 @@ class ResultsFactory implements FactoryInterface ->get($paramsService); $searchService = $container->get(\VuFindSearch\Service::class); $recordLoader = $container->get(\VuFind\Record\Loader::class); - return new $requestedName( + $results = new $requestedName( $params, $searchService, $recordLoader, ...($options ?: []) ); + $results->setUrlQueryHelperFactory( + $container->get(UrlQueryHelperFactory::class) + ); + return $results; } } diff --git a/module/VuFind/src/VuFindTest/Unit/TestCase.php b/module/VuFind/src/VuFindTest/Unit/TestCase.php index eed2055534b..4ceead7268a 100644 --- a/module/VuFind/src/VuFindTest/Unit/TestCase.php +++ b/module/VuFind/src/VuFindTest/Unit/TestCase.php @@ -28,6 +28,8 @@ */ namespace VuFindTest\Unit; +use VuFind\Search\Factory\UrlQueryHelperFactory; + /** * Abstract base class for PHPUnit test cases. * @@ -215,6 +217,9 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase \Zend\Mvc\I18n\Translator::class, $factory->createService($this->serviceManager) ); + $this->serviceManager->setService( + UrlQueryHelperFactory::class, new UrlQueryHelperFactory() + ); } return $this->serviceManager; } -- GitLab