From 199191cc2370f8ff854882fa4c5bb9b15dc4399e Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 6 Sep 2013 13:13:43 -0400 Subject: [PATCH] Changed VuFind\Search\Memory from static class to service. Resolves VUFIND-883. --- module/VuFind/config/module.config.php | 1 + .../src/VuFind/Controller/AbstractBase.php | 10 +++++ .../src/VuFind/Controller/AbstractSearch.php | 4 +- .../VuFind/Controller/CombinedController.php | 6 +-- .../Controller/Plugin/ResultScroller.php | 4 +- .../VuFind/Controller/SearchController.php | 5 +-- module/VuFind/src/VuFind/Search/Memory.php | 43 +++++++++++++------ .../View/Helper/Root/GetLastSearchLink.php | 19 +++++++- themes/root/theme.config.php | 6 ++- 9 files changed, 72 insertions(+), 26 deletions(-) diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index e517dadf44c..33bb85ad003 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -321,6 +321,7 @@ $config = array( 'invokables' => array( 'VuFind\SessionManager' => 'Zend\Session\SessionManager', 'VuFind\Search' => 'VuFindSearch\Service', + 'VuFind\Search\Memory' => 'VuFind\Search\Memory', ), 'initializers' => array( array('VuFind\ServiceManager\Initializer', 'initInstance'), diff --git a/module/VuFind/src/VuFind/Controller/AbstractBase.php b/module/VuFind/src/VuFind/Controller/AbstractBase.php index 426b5420107..1d9f972e915 100644 --- a/module/VuFind/src/VuFind/Controller/AbstractBase.php +++ b/module/VuFind/src/VuFind/Controller/AbstractBase.php @@ -379,4 +379,14 @@ class AbstractBase extends AbstractActionController { $this->getServiceLocator()->get('VuFind\SessionManager')->writeClose(); } + + /** + * Get the search memory + * + * @return \VuFind\Search\Memory + */ + public function getSearchMemory() + { + return $this->getServiceLocator()->get('VuFind\Search\Memory'); + } } diff --git a/module/VuFind/src/VuFind/Controller/AbstractSearch.php b/module/VuFind/src/VuFind/Controller/AbstractSearch.php index 02f64ba6a86..4264c1655ed 100644 --- a/module/VuFind/src/VuFind/Controller/AbstractSearch.php +++ b/module/VuFind/src/VuFind/Controller/AbstractSearch.php @@ -26,7 +26,7 @@ * @link http://www.vufind.org Main Page */ namespace VuFind\Controller; -use VuFind\Search\Memory, Zend\Stdlib\Parameters; +use Zend\Stdlib\Parameters; /** * VuFind Search Controller @@ -177,7 +177,7 @@ class AbstractSearch extends AbstractBase $searchUrl = $this->url()->fromRoute( $results->getOptions()->getSearchAction() ) . $results->getUrlQuery()->getParams(false); - Memory::rememberSearch($searchUrl); + $this->getSearchMemory()->rememberSearch($searchUrl); } } diff --git a/module/VuFind/src/VuFind/Controller/CombinedController.php b/module/VuFind/src/VuFind/Controller/CombinedController.php index 0651f12d884..fa38c0b9e2a 100644 --- a/module/VuFind/src/VuFind/Controller/CombinedController.php +++ b/module/VuFind/src/VuFind/Controller/CombinedController.php @@ -26,7 +26,7 @@ * @link http://vufind.org Main Site */ namespace VuFind\Controller; -use VuFind\Search\Memory, Zend\Stdlib\Parameters; +use Zend\Stdlib\Parameters; /** * Redirects the user to the appropriate default VuFind action. @@ -68,7 +68,7 @@ class CombinedController extends AbstractSearch $this->writeSession(); // avoid session write timing bug // Turn off search memory -- not relevant in this context: - Memory::disable(); + $this->getSearchMemory()->disable(); // Validate configuration: $searchClassId = $this->params()->fromQuery('id'); @@ -121,7 +121,7 @@ class CombinedController extends AbstractSearch // Remember the current URL, then disable memory so multi-search results // don't overwrite it: $this->rememberSearch($results); - Memory::disable(); + $this->getSearchMemory()->disable(); // Gather combined results: $combinedResults = array(); diff --git a/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php b/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php index c676b2c1aa8..64e609343e6 100644 --- a/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php +++ b/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php @@ -26,7 +26,7 @@ * @link http://vufind.org/wiki/vufind2:developer_manual Wiki */ namespace VuFind\Controller\Plugin; -use VuFind\Search\Memory, Zend\Mvc\Controller\Plugin\AbstractPlugin, +use Zend\Mvc\Controller\Plugin\AbstractPlugin, Zend\Session\Container as SessionContainer; /** @@ -368,7 +368,7 @@ class ResultScroller extends AbstractPlugin $baseUrl = $this->getController()->url()->fromRoute( $search->getOptions()->getSearchAction() ); - Memory::rememberSearch( + $this->getController()->getSearchMemory()->rememberSearch( $baseUrl . $search->getUrlQuery()->getParams(false) ); } diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php index 301689d951f..ff5a531468a 100644 --- a/module/VuFind/src/VuFind/Controller/SearchController.php +++ b/module/VuFind/src/VuFind/Controller/SearchController.php @@ -27,8 +27,7 @@ */ namespace VuFind\Controller; -use VuFind\Exception\Mail as MailException, VuFind\Search\Memory, - VuFind\Solr\Utils as SolrUtils; +use VuFind\Exception\Mail as MailException, VuFind\Solr\Utils as SolrUtils; /** * Redirects the user to the appropriate default VuFind action. @@ -265,7 +264,7 @@ class SearchController extends AbstractSearch $current->delete(); // We don't want to remember the last search after a purge: - Memory::forgetSearch(); + $this->getSearchMemory()->forgetSearch(); } else { // Otherwise add to the list $unsaved[] = $minSO->deminify($this->getResultsManager()); diff --git a/module/VuFind/src/VuFind/Search/Memory.php b/module/VuFind/src/VuFind/Search/Memory.php index fe04d8749f9..55f2769453c 100644 --- a/module/VuFind/src/VuFind/Search/Memory.php +++ b/module/VuFind/src/VuFind/Search/Memory.php @@ -26,7 +26,7 @@ * @link http://www.vufind.org Main Page */ namespace VuFind\Search; -use Zend\Session\Container as SessionContainer; +use Zend\Session\Container; /** * Wrapper class to handle search memory @@ -44,7 +44,25 @@ class Memory * * @var bool */ - protected static $active = true; + protected $active = true; + + /** + * Session container + * + * @var Container + */ + protected $session; + + /** + * Constructor + * + * @param Container $session Session container for storing URLs (optional) + */ + public function __construct($session = null) + { + $this->session = (null === $session) + ? new Container('Search') : $session; + } /** * Stop updating the URL in memory -- used in combined search to prevent @@ -54,7 +72,7 @@ class Memory */ public function disable() { - self::$active = false; + $this->active = false; } /** @@ -62,10 +80,9 @@ class Memory * * @return void */ - public static function forgetSearch() + public function forgetSearch() { - $session = new SessionContainer('Search'); - unset($session->last); + unset($this->session->last); } /** @@ -75,19 +92,18 @@ class Memory * * @return void */ - public static function rememberSearch($url) + public function rememberSearch($url) { // Do nothing if disabled. - if (!self::$active) { + if (!$this->active) { return; } // Only remember URL if string is non-empty... otherwise clear the memory. if (strlen(trim($url)) > 0) { - $session = new SessionContainer('Search'); - $session->last = $url; + $this->session->last = $url; } else { - self::forgetSearch(); + $this->forgetSearch(); } } @@ -97,9 +113,8 @@ class Memory * * @return string|null */ - public static function retrieve() + public function retrieve() { - $session = new SessionContainer('Search'); - return isset($session->last) ? $session->last : null; + return isset($this->session->last) ? $this->session->last : null; } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/View/Helper/Root/GetLastSearchLink.php b/module/VuFind/src/VuFind/View/Helper/Root/GetLastSearchLink.php index 9295b6e2414..8bc3eed1b82 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/GetLastSearchLink.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/GetLastSearchLink.php @@ -39,6 +39,23 @@ use VuFind\Search\Memory, Zend\View\Helper\AbstractHelper; */ class GetLastSearchLink extends AbstractHelper { + /** + * Search memory + * + * @var Memory + */ + protected $memory; + + /** + * Constructor + * + * @param Memory $memory Search memory + */ + public function __construct(Memory $memory) + { + $this->memory = $memory; + } + /** * If a previous search is recorded in the session, return a link to it; * otherwise, return a blank string. @@ -51,7 +68,7 @@ class GetLastSearchLink extends AbstractHelper */ public function __invoke($link, $prefix = '', $suffix = '') { - $last = Memory::retrieve(); + $last = $this->memory->retrieve(); if (!empty($last)) { $escaper = $this->getView()->plugin('escapeHtml'); return $prefix . '<a href="' . $escaper($last) . '">' . $link . '</a>' diff --git a/themes/root/theme.config.php b/themes/root/theme.config.php index 4e0670b1baa..f7625e87084 100644 --- a/themes/root/theme.config.php +++ b/themes/root/theme.config.php @@ -66,6 +66,11 @@ return array( ? $config->GoogleAnalytics->apiKey : false; return new \VuFind\View\Helper\Root\GoogleAnalytics($key); }, + 'getlastsearchlink' => function ($sm) { + return new \VuFind\View\Helper\Root\GetLastSearchLink( + $sm->getServiceLocator()->get('VuFind\Search\Memory') + ); + }, 'historylabel' => function ($sm) { $config = $sm->getServiceLocator()->get('VuFind\Config')->get('config'); $config = isset($config->SearchHistoryLabels) @@ -160,7 +165,6 @@ return array( 'browse' => 'VuFind\View\Helper\Root\Browse', 'context' => 'VuFind\View\Helper\Root\Context', 'currentpath' => 'VuFind\View\Helper\Root\CurrentPath', - 'getlastsearchlink' => 'VuFind\View\Helper\Root\GetLastSearchLink', 'highlight' => 'VuFind\View\Helper\Root\Highlight', 'jqueryvalidation' => 'VuFind\View\Helper\Root\JqueryValidation', 'printms' => 'VuFind\View\Helper\Root\Printms', -- GitLab