Skip to content

Commit ce9c6d6

Browse files
authored
Refactor SearchParams and SearchTabs view helpers to use autowiring (#5085)
1 parent 07e6689 commit ce9c6d6

File tree

6 files changed

+45
-232
lines changed

6 files changed

+45
-232
lines changed

module/VuFind/src/VuFind/View/Helper/Root/SearchParams.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
namespace VuFind\View\Helper\Root;
3131

3232
use VuFind\Search\Params\PluginManager;
33+
use VuFind\ServiceManager\Factory\Autowire;
3334

3435
/**
3536
* "Retrieve search params" view helper
@@ -40,23 +41,17 @@
4041
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
4142
* @link https://vufind.org/wiki/development Wiki
4243
*/
43-
class SearchParams extends \Laminas\View\Helper\AbstractHelper
44+
class SearchParams
4445
{
45-
/**
46-
* Search manager
47-
*
48-
* @var PluginManager
49-
*/
50-
protected $manager;
51-
5246
/**
5347
* Constructor
5448
*
5549
* @param PluginManager $manager Search manager
5650
*/
57-
public function __construct(PluginManager $manager)
58-
{
59-
$this->manager = $manager;
51+
public function __construct(
52+
#[Autowire]
53+
protected PluginManager $manager
54+
) {
6055
}
6156

6257
/**

module/VuFind/src/VuFind/View/Helper/Root/SearchParamsFactory.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

module/VuFind/src/VuFind/View/Helper/Root/SearchTabs.php

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131

3232
namespace VuFind\View\Helper\Root;
3333

34-
use Laminas\Http\Request;
35-
use Laminas\View\Helper\Url;
34+
use Psr\Log\LoggerAwareInterface;
35+
use VuFind\Log\LoggerAwareTrait;
3636
use VuFind\Search\Base\Results;
3737
use VuFind\Search\Results\PluginManager;
3838
use VuFind\Search\SearchTabsHelper;
3939
use VuFind\Search\UrlQueryHelper;
40+
use VuFind\ServiceManager\Factory\Autowire;
4041

4142
/**
4243
* "Search tabs" view helper
@@ -48,37 +49,9 @@
4849
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
4950
* @link https://vufind.org/wiki/development Wiki
5051
*/
51-
class SearchTabs extends \Laminas\View\Helper\AbstractHelper implements \Psr\Log\LoggerAwareInterface
52+
class SearchTabs implements LoggerAwareInterface
5253
{
53-
use \VuFind\Log\LoggerAwareTrait;
54-
55-
/**
56-
* Search manager
57-
*
58-
* @var PluginManager
59-
*/
60-
protected $results;
61-
62-
/**
63-
* Request
64-
*
65-
* @var Request
66-
*/
67-
protected $request;
68-
69-
/**
70-
* Url
71-
*
72-
* @var Url
73-
*/
74-
protected $url;
75-
76-
/**
77-
* Search tab helper
78-
*
79-
* @var SearchTabsHelper
80-
*/
81-
protected $helper;
54+
use LoggerAwareTrait;
8255

8356
/**
8457
* Cached hidden filter url params
@@ -97,18 +70,29 @@ class SearchTabs extends \Laminas\View\Helper\AbstractHelper implements \Psr\Log
9770
/**
9871
* Constructor
9972
*
100-
* @param PluginManager $results Search results plugin manager
101-
* @param Url $url URL helper
102-
* @param SearchTabsHelper $helper Search tabs helper
73+
* @param PluginManager $results Search results plugin manager
74+
* @param Url $url URL helper
75+
* @param SearchTabsHelper $helper Search tabs helper
76+
* @param SearchMemory $searchMemory Search memory view helper
10377
*/
10478
public function __construct(
105-
PluginManager $results,
106-
Url $url,
107-
SearchTabsHelper $helper
79+
protected PluginManager $results,
80+
#[Autowire(container: 'ViewHelperManager')]
81+
protected Url $url,
82+
protected SearchTabsHelper $helper,
83+
#[Autowire(container: 'ViewHelperManager')]
84+
protected SearchMemory $searchMemory
10885
) {
109-
$this->results = $results;
110-
$this->url = $url;
111-
$this->helper = $helper;
86+
}
87+
88+
/**
89+
* Invoke the helper.
90+
*
91+
* @return static
92+
*/
93+
public function __invoke(): static
94+
{
95+
return $this;
11296
}
11397

11498
/**
@@ -135,18 +119,15 @@ public function getTabConfig(
135119
$allSettings = $this->helper->getSettings();
136120
$retVal['showCounts'] = $allSettings['show_result_counts'] ?? false;
137121
foreach ($this->helper->getTabConfig() as $key => $label) {
138-
$permissionName = null;
139-
if (isset($allPermissions[$key])) {
140-
$permissionName = $allPermissions[$key];
141-
}
122+
$permissionName = $allPermissions[$key] ?? null;
142123
$class = $this->helper->extractClassName($key);
143124
$filters = isset($allFilters[$key]) ? (array)$allFilters[$key] : [];
144-
$selected = $class == $activeSearchClass && $this->helper->filtersMatch($class, $hiddenFilters, $filters);
125+
$selected = $class == $activeSearchClass
126+
&& $this->helper->filtersMatch($class, $hiddenFilters, $filters);
145127
try {
146128
if ($type == 'basic') {
147129
if (!isset($activeOptions)) {
148-
$activeOptions
149-
= $this->results->get($activeSearchClass)->getOptions();
130+
$activeOptions = $this->results->get($activeSearchClass)->getOptions();
150131
}
151132
$url = $this->remapBasicSearch(
152133
$activeOptions,
@@ -262,14 +243,12 @@ public function getCurrentHiddenFilterParams(
262243
return '';
263244
}
264245
if (!isset($this->cachedHiddenFilterParams[$searchClassId])) {
265-
$view = $this->getView();
266246
$hiddenFilters = $this->getHiddenFilters(
267247
$searchClassId,
268248
$ignoreHiddenFilterMemory
269249
);
270250
if (empty($hiddenFilters) && !$ignoreHiddenFilterMemory) {
271-
$hiddenFilters = $view->plugin('searchMemory')
272-
->getLastHiddenFilters($searchClassId);
251+
$hiddenFilters = $this->searchMemory->getLastHiddenFilters($searchClassId);
273252
if (empty($hiddenFilters)) {
274253
$hiddenFilters = $this->getHiddenFilters($searchClassId);
275254
}
@@ -323,15 +302,14 @@ protected function remapBasicSearch(
323302
foreach ($filters as $filter) {
324303
$params->addHiddenFilter($filter);
325304
}
326-
327305
// Find matching handler for new query (and use default if no match):
328306
$options = $results->getOptions();
329307
$targetHandler = $options->getHandlerForLabel(
330308
$activeOptions->getLabelForBasicHandler($handler)
331309
);
332310

333311
// Build new URL:
334-
$results->getParams()->setBasicSearch($query, $targetHandler);
312+
$params->setBasicSearch($query, $targetHandler);
335313
return ($this->url)($options->getSearchAction())
336314
. $results->getUrlQuery()->getParams(false);
337315
}
@@ -346,12 +324,9 @@ protected function remapBasicSearch(
346324
*/
347325
protected function getHomeTabUrl($class, $filters)
348326
{
349-
// If an advanced search is available, link there; otherwise, just go
350-
// to the search home:
351327
$results = $this->results->get($class);
352-
$url = ($this->url)($results->getOptions()->getSearchHomeAction())
328+
return ($this->url)($results->getOptions()->getSearchHomeAction())
353329
. $this->buildUrlHiddenFilters($results, $filters);
354-
return $url;
355330
}
356331

357332
/**

module/VuFind/src/VuFind/View/Helper/Root/SearchTabsFactory.php

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)