Skip to content

Commit 188eb66

Browse files
authored
Refactor AbstractJsStrings and related classes to use constructor injection and Autowiring (#5075)
1 parent b59c619 commit 188eb66

File tree

10 files changed

+61
-96
lines changed

10 files changed

+61
-96
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
namespace VuFind\View\Helper\Root;
3131

32-
use Laminas\View\Helper\AbstractHelper;
33-
3432
/**
3533
* AbstractJsStrings helper for passing transformed text to Javascript
3634
*
@@ -40,15 +38,8 @@
4038
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
4139
* @link https://vufind.org/wiki/development Wiki
4240
*/
43-
abstract class AbstractJsStrings extends AbstractHelper
41+
abstract class AbstractJsStrings
4442
{
45-
/**
46-
* Variable name to store values
47-
*
48-
* @var string
49-
*/
50-
protected $varName;
51-
5243
/**
5344
* Strings to convey (key = js key, value = value to map)
5445
*
@@ -61,9 +52,18 @@ abstract class AbstractJsStrings extends AbstractHelper
6152
*
6253
* @param string $varName Variable name to store values
6354
*/
64-
public function __construct($varName = 'vufindString')
55+
public function __construct(protected string $varName = 'vufindString')
56+
{
57+
}
58+
59+
/**
60+
* Make helper invokable.
61+
*
62+
* @return static
63+
*/
64+
public function __invoke(): static
6565
{
66-
$this->varName = $varName;
66+
return $this;
6767
}
6868

6969
/**

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,17 @@
4141
*/
4242
class JsIcons extends AbstractJsStrings
4343
{
44-
/**
45-
* Icon helper
46-
*
47-
* @var Icon
48-
*/
49-
protected $iconHelper;
50-
5144
/**
5245
* Constructor
5346
*
5447
* @param Icon $iconHelper Icon helper
5548
* @param string $varName Variable name to store icons
5649
*/
5750
public function __construct(
58-
Icon $iconHelper,
59-
$varName = 'vufindIconString'
51+
protected Icon $iconHelper,
52+
string $varName = 'vufindIconString'
6053
) {
6154
parent::__construct($varName);
62-
$this->iconHelper = $iconHelper;
6355
}
6456

6557
/**

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,6 @@
4040
*/
4141
class JsTranslations extends AbstractJsStrings
4242
{
43-
/**
44-
* Translate helper
45-
*
46-
* @var Translate
47-
*/
48-
protected $translate;
49-
50-
/**
51-
* Translate + escape helper
52-
*
53-
* @var TransEsc
54-
*/
55-
protected $transEsc;
56-
5743
/**
5844
* Constructor
5945
*
@@ -62,13 +48,11 @@ class JsTranslations extends AbstractJsStrings
6248
* @param string $varName Variable name to store translations
6349
*/
6450
public function __construct(
65-
Translate $translate,
66-
TransEsc $transEsc,
67-
$varName = 'vufindString'
51+
protected Translate $translate,
52+
protected TransEsc $transEsc,
53+
string $varName = 'vufindString'
6854
) {
6955
parent::__construct($varName);
70-
$this->translate = $translate;
71-
$this->transEsc = $transEsc;
7256
}
7357

7458
/**

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929

3030
namespace VuFind\View\Helper\Root;
3131

32-
use Laminas\View\Helper\AbstractHelper;
32+
use Laminas\View\Helper\EscapeHtml;
33+
use VuFind\ServiceManager\Factory\Autowire;
3334

3435
/**
3536
* Translate + escape view helper
@@ -40,8 +41,22 @@
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 TransEsc extends AbstractHelper
44+
class TransEsc
4445
{
46+
/**
47+
* Constructor
48+
*
49+
* @param Translate $translate Translate view helper
50+
* @param EscapeHtml $escapeHtml EscapeHtml view helper
51+
*/
52+
public function __construct(
53+
#[Autowire(container: 'ViewHelperManager')]
54+
protected Translate $translate,
55+
#[Autowire(container: 'ViewHelperManager')]
56+
protected EscapeHtml $escapeHtml
57+
) {
58+
}
59+
4560
/**
4661
* Translate and escape a string
4762
*
@@ -64,8 +79,7 @@ public function __invoke(
6479
$useIcuFormatter = false,
6580
$fallbackDomains = []
6681
) {
67-
$escaper = $this->getView()->plugin('escapeHtml');
68-
$translator = $this->getView()->plugin('translate');
69-
return $escaper($translator($str, $tokens, $default, $useIcuFormatter, $fallbackDomains));
82+
$translated = ($this->translate)($str, $tokens, $default, $useIcuFormatter, $fallbackDomains);
83+
return ($this->escapeHtml)($translated);
7084
}
7185
}

module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/FlashmessagesTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,7 @@ protected function getViewHelpers()
278278
$translator = $this->getMockTranslator($translations);
279279
$translate = new Translate();
280280
$translate->setTranslator($translator);
281-
$transEsc = new TransEsc();
282-
$transEsc->setView(
283-
$this->getPhpRenderer(
284-
[
285-
'escapeHtml' => new EscapeHtml(),
286-
'translate' => $translate,
287-
]
288-
)
289-
);
281+
$transEsc = new TransEsc($translate, new EscapeHtml());
290282
return compact('transEsc', 'translate');
291283
}
292284
}

module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/JsTranslationsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ class JsTranslationsTest extends \PHPUnit\Framework\TestCase
5757
public function testHelper()
5858
{
5959
$view = $this->getPhpRenderer($this->getViewHelpers());
60-
$transEsc = new TransEsc();
61-
$transEsc->setView($view);
62-
$helper = new JsTranslations($view->plugin('translate'), $transEsc);
63-
$helper->setView($view);
60+
$translate = $view->plugin('translate');
61+
$escapeHtml = new \Laminas\View\Helper\EscapeHtml();
62+
$transEsc = new TransEsc($translate, $escapeHtml);
63+
$helper = new JsTranslations($translate, $transEsc);
6464

6565
// Normal addStrings:
6666
$helper->addStrings(['1key' => 'key1']);

module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/MethodTimedBlocksTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,7 @@ protected function getViewHelpers(array $timedBlocks, bool $blocked)
134134
$translator = $this->getMockTranslator($translations);
135135
$translate = new Translate();
136136
$translate->setTranslator($translator);
137-
$transEsc = new TransEsc();
138-
$transEsc->setView(
139-
$this->getPhpRenderer(
140-
[
141-
'escapeHtml' => new EscapeHtml(),
142-
'translate' => $translate,
143-
]
144-
)
145-
);
137+
$transEsc = new TransEsc($translate, new EscapeHtml());
146138

147139
$connection = $this->createMock(Connection::class);
148140
$connection->method('getMethodTimedBlocks')->willReturn($timedBlocks);

module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/PermissionTest.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
namespace VuFindTest\View\Helper\Root;
3232

33+
use VuFind\Role\PermissionDeniedManager;
34+
use VuFind\Role\PermissionManager;
3335
use VuFind\View\Helper\Root\Permission;
3436

3537
/**
@@ -84,7 +86,7 @@ class PermissionTest extends \PHPUnit\Framework\TestCase
8486
*/
8587
public function testMessageDisplay()
8688
{
87-
$mockPmdMessage = $this->getMockPmd(
89+
$mockPdmMessage = $this->getMockPdm(
8890
[
8991
'deniedTemplateBehavior' => [
9092
'action' => 'showMessage',
@@ -94,7 +96,7 @@ public function testMessageDisplay()
9496
]
9597
);
9698

97-
$helper = new Permission($this->getMockPm(false), $mockPmdMessage);
99+
$helper = new Permission($this->getMockPm(false), $mockPdmMessage);
98100
$helper->setView($this->getMockView());
99101

100102
$displayBlock = $helper->getAlternateContent('permissionDeniedMessage');
@@ -111,7 +113,7 @@ public function testTemplateDisplay()
111113
$this->expectException(\Laminas\View\Exception\RuntimeException::class);
112114

113115
// Template does not exist, expect an exception, though
114-
$mockPmd = $this->getMockPmd(
116+
$mockPdm = $this->getMockPdm(
115117
[
116118
'deniedTemplateBehavior' => [
117119
'action' => 'showTemplate',
@@ -121,7 +123,7 @@ public function testTemplateDisplay()
121123
]
122124
);
123125

124-
$helper = new Permission($this->getMockPm(false), $mockPmd);
126+
$helper = new Permission($this->getMockPm(false), $mockPdm);
125127
$helper->setView($this->getMockView());
126128

127129
$helper->getAlternateContent('permissionDeniedTemplate');
@@ -134,7 +136,7 @@ public function testTemplateDisplay()
134136
*/
135137
public function testExistingTemplateDisplay()
136138
{
137-
$mockPmd = $this->getMockPmd(
139+
$mockPdm = $this->getMockPdm(
138140
[
139141
'deniedTemplateBehavior' => [
140142
'action' => 'showTemplate',
@@ -144,7 +146,7 @@ public function testExistingTemplateDisplay()
144146
]
145147
);
146148

147-
$helper = new Permission($this->getMockPm(false), $mockPmd);
149+
$helper = new Permission($this->getMockPm(false), $mockPdm);
148150
$helper->setView($this->getMockView());
149151

150152
$this->assertSame(
@@ -158,43 +160,33 @@ public function testExistingTemplateDisplay()
158160
*
159161
* @param array $config Config containing DeniedTemplateBehavior to return
160162
*
161-
* @return \VuFind\Role\PermissionDeniedManager
163+
* @return PermissionDeniedManager
162164
*/
163-
protected function getMockPmd($config = false)
165+
protected function getMockPdm($config = false): PermissionDeniedManager
164166
{
165-
$mockPmd = $this->getMockBuilder(\VuFind\Role\PermissionDeniedManager::class)
167+
$mockPdm = $this->getMockBuilder(PermissionDeniedManager::class)
166168
->setConstructorArgs([$this->permissionDeniedConfig])
167169
->getMock();
168-
$mockPmd->method('getDeniedTemplateBehavior')->willReturn($config['deniedTemplateBehavior']);
169-
return $mockPmd;
170+
$mockPdm->method('getDeniedTemplateBehavior')->willReturn($config['deniedTemplateBehavior']);
171+
return $mockPdm;
170172
}
171173

172174
/**
173175
* Get mock permission manager
174176
*
175177
* @param array $isAuthorized isAuthorized value to return
176178
*
177-
* @return \VuFind\Role\PermissionManager
179+
* @return PermissionManager
178180
*/
179181
protected function getMockPm($isAuthorized = false)
180182
{
181-
$mockPm = $this->createMock(\VuFind\Role\PermissionManager::class);
183+
$mockPm = $this->createMock(PermissionManager::class);
182184
$mockPm->method('isAuthorized')->willReturn($isAuthorized);
183185
$mockPm->method('permissionRuleExists')->willReturn(true);
184186

185187
return $mockPm;
186188
}
187189

188-
/**
189-
* Get mock context helper.
190-
*
191-
* @return \VuFind\View\Helper\Root\Context
192-
*/
193-
protected function getMockContext()
194-
{
195-
return $this->createMock(\VuFind\View\Helper\Root\Context::class);
196-
}
197-
198190
/**
199191
* Return a view object populated for these test cases.
200192
*
@@ -204,12 +196,11 @@ protected function getMockView()
204196
{
205197
$escapehtml = new \Laminas\View\Helper\EscapeHtml();
206198
$translate = new \VuFind\View\Helper\Root\Translate();
207-
$transEsc = new \VuFind\View\Helper\Root\TransEsc();
199+
$transEsc = new \VuFind\View\Helper\Root\TransEsc($translate, $escapehtml);
208200
$context = new \VuFind\View\Helper\Root\Context();
209201
$realView = $this->getPhpRenderer(
210202
compact('translate', 'transEsc', 'context', 'escapehtml')
211203
);
212-
$transEsc->setView($realView);
213204
return $realView;
214205
}
215206
}

module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ protected function getViewHelpers(ContainerInterface $container, SchemaOrg $sche
8888
return $this->createMock($service);
8989
});
9090
$record->setDbServiceManager($serviceManager);
91+
$translate = new \VuFind\View\Helper\Root\Translate();
9192
return [
9293
'auth' => new \VuFind\View\Helper\Root\Auth(
9394
$this->createMock(\VuFind\Auth\Manager::class),
@@ -118,8 +119,8 @@ protected function getViewHelpers(ContainerInterface $container, SchemaOrg $sche
118119
new \VuFind\Search\Options\PluginManager($container)
119120
),
120121
'searchTabs' => $this->createMock(\VuFind\View\Helper\Root\SearchTabs::class),
121-
'transEsc' => new \VuFind\View\Helper\Root\TransEsc(),
122-
'translate' => new \VuFind\View\Helper\Root\Translate(),
122+
'transEsc' => new \VuFind\View\Helper\Root\TransEsc($translate, new \Laminas\View\Helper\EscapeHtml()),
123+
'translate' => $translate,
123124
'usertags' => new \VuFind\View\Helper\Root\UserTags(),
124125
];
125126
}

themes/root/theme.config.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
'VuFind\View\Helper\Root\SystemEmail' => 'VuFind\View\Helper\Root\SystemEmailFactory',
9595
'VuFind\View\Helper\Root\Tabs' => 'Laminas\ServiceManager\Factory\InvokableFactory',
9696
'VuFind\View\Helper\Root\ThemeConfig' => 'VuFind\View\Helper\Root\ThemeConfigFactory',
97-
'VuFind\View\Helper\Root\TransEsc' => 'Laminas\ServiceManager\Factory\InvokableFactory',
9897
'VuFind\View\Helper\Root\TransEscAttr' => 'Laminas\ServiceManager\Factory\InvokableFactory',
9998
'VuFind\View\Helper\Root\TransEscWithPrefix' => 'Laminas\ServiceManager\Factory\InvokableFactory',
10099
'VuFind\View\Helper\Root\Translate' => 'Laminas\ServiceManager\Factory\InvokableFactory',

0 commit comments

Comments
 (0)