Skip to content

Commit 3ec940f

Browse files
committed
Optimize code
1 parent 2c7bdef commit 3ec940f

File tree

12 files changed

+108
-87
lines changed

12 files changed

+108
-87
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,52 @@
11
name: CI
22

3-
on: [ push, pull_request ]
3+
on:
4+
[push, pull_request]
45

56
jobs:
67
build:
78
runs-on: ubuntu-latest
89

9-
continue-on-error: ${{ matrix.env.experimental == true }}
1010
strategy:
1111
fail-fast: false
1212
matrix:
13-
env:
14-
- { php: 8.2 }
15-
- { php: 8.3 }
16-
17-
env: ${{ matrix.env }}
13+
php: [8.2, 8.3]
1814

1915
steps:
20-
- name: Checkout
16+
- name: Checkout code
2117
uses: actions/checkout@v4
2218

2319
- name: Setup PHP
2420
uses: shivammathur/setup-php@v2
2521
with:
26-
php-version: ${{ matrix.env.php }}
22+
php-version: ${{ matrix.php }}
2723
tools: composer:2
2824
extensions: pdo, sqlite3
2925

30-
- name: Validate composer.json and composer.lock
31-
run: composer validate
26+
- name: Validate composer files
27+
run: composer validate --strict
3228

33-
- name: Cache dependencies
29+
- name: Cache Composer dependencies
3430
uses: actions/cache@v4
3531
with:
3632
path: ~/.composer/cache
37-
key: dependencies-composer-${{ matrix.env.php }}-${{ hashFiles('composer.json') }}
33+
key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }}
3834

3935
- name: Install dependencies
40-
run: composer install
36+
run: composer install --prefer-dist --no-interaction
4137

42-
- name: Check coding style
38+
- name: Check coding style (optional)
4339
run: composer lint
40+
continue-on-error: true # In case you want to not fail build if lint fails
4441

4542
- name: Run Unit Tests
4643
run: |
47-
echo "Running unit tests with $(which php)";
48-
.Build/bin/phpunit --colors Tests/Unit/;
44+
echo "Running unit tests with $(which php)"
45+
if [ -f .Build/bin/phpunit ]; then
46+
.Build/bin/phpunit --colors=always Tests/Unit/
47+
elif [ -f vendor/bin/phpunit ]; then
48+
vendor/bin/phpunit --colors=always Tests/Unit/
49+
else
50+
echo "phpunit binary not found!"
51+
exit 1
52+
fi

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16
1+
22

Classes/Controller/SearchController.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Subugoe\Find\Utility\ArrayUtility;
3535
use Subugoe\Find\Utility\FrontendUtility;
3636
use TYPO3\CMS\Core\Page\AssetCollector;
37+
use TYPO3\CMS\Core\PageTitle\PageTitleProviderInterface;
3738
use TYPO3\CMS\Core\Utility\ArrayUtility as CoreArrayUtility;
3839
use TYPO3\CMS\Core\Utility\GeneralUtility;
3940
use TYPO3\CMS\Extbase\Http\ForwardResponse;
@@ -44,9 +45,9 @@ class SearchController extends ActionController
4445
{
4546
protected array $requestArguments = [];
4647

47-
protected ?object $searchProvider = null;
48+
protected ?ServiceProviderInterface $searchProvider = null;
4849

49-
public function __construct(private readonly AssetCollector $assetCollector, private readonly LoggerInterface $logger) {}
50+
public function __construct(private readonly AssetCollector $assetCollector, private readonly LoggerInterface $logger, private readonly PageTitleProviderInterface $pageTitleProvider) {}
5051

5152
/**
5253
* @throws NoSuchArgumentException|\JsonException
@@ -93,8 +94,7 @@ public function indexAction(): ResponseInterface
9394
$this->searchProvider->getRequestArguments()
9495
);
9596

96-
GeneralUtility::makeInstance(AssetCollector::class)
97-
->addInlineJavaScript('underlyingQueryVar', $underlyingQueryScriptTagContent, ['type' => 'text/javascript'], ['priority' => true]);
97+
$this->assetCollector->addInlineJavaScript('underlyingQueryVar', $underlyingQueryScriptTagContent, ['type' => 'text/javascript'], ['priority' => true]);
9898

9999
$this->addStandardAssignments();
100100
$defaultQuery = $this->searchProvider->getDefaultQuery();
@@ -149,13 +149,10 @@ protected function addStandardAssignments(): void
149149
$this->request->getAttribute('currentContentObject')->data['uid']
150150
);
151151
$this->searchProvider->setConfigurationValue('prefixID', 'tx_find_find');
152-
$this->searchProvider->setConfigurationValue('pageTitle', $GLOBALS['TSFE']->page['title']);
152+
$this->searchProvider->setConfigurationValue('pageTitle', $this->pageTitleProvider->getTitle());
153153
}
154154

155-
/**
156-
* @param string $activeConnection
157-
*/
158-
protected function initializeConnection($activeConnection): void
155+
protected function initializeConnection(string $activeConnection): void
159156
{
160157
$connectionConfiguration = $this->settings['connections'][$activeConnection];
161158

Classes/Service/AbstractServiceProvider.php

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

Classes/Service/SolrServiceProvider.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*
2727
* This copyright notice MUST APPEAR in all copies of the script!
2828
* ************************************************************* */
29+
30+
use Psr\Log\LoggerInterface;
2931
use Solarium\Client;
3032
use Solarium\Component\Highlighting\Field;
3133
use Solarium\Core\Client\Adapter\Curl;
@@ -40,9 +42,9 @@
4042
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
4143

4244
/**
43-
* Service provider for solr.
45+
* Service provider for Solr.
4446
*/
45-
class SolrServiceProvider extends AbstractServiceProvider
47+
class SolrServiceProvider implements ServiceProviderInterface
4648
{
4749
protected ?string $action = null;
4850

@@ -54,6 +56,20 @@ class SolrServiceProvider extends AbstractServiceProvider
5456

5557
protected Query $query;
5658

59+
protected array $requestArguments = [];
60+
61+
public function __construct(protected string $connectionName, protected array $settings, protected LoggerInterface $logger) {}
62+
63+
public function getRequestArguments(): array
64+
{
65+
return $this->requestArguments;
66+
}
67+
68+
public function setRequestArguments(array $requestArguments): void
69+
{
70+
$this->requestArguments = $requestArguments;
71+
}
72+
5773
public function connect(): void
5874
{
5975
$currentConnectionSettings = $this->settings['connections'][$this->connectionName]['options'];
@@ -525,7 +541,7 @@ protected function addSortOrdersToTemplate(array $arguments): void
525541
ksort($this->settings['sort']);
526542
foreach ($this->settings['sort'] as $sortOptionIndex => $sortOption) {
527543
if (array_key_exists('id', $sortOption) && array_key_exists('sortCriteria', $sortOption)) {
528-
$localisationKey = 'LLL:' . $this->settings['languageRootPath'] . 'locallang-form.xml:input.sort-' . $sortOption['id'];
544+
$localisationKey = 'LLL:' . $this->settings['languageRootPath'] . 'locallang-form.xlf:input.sort-' . $sortOption['id'];
529545
$localisedLabel = LocalizationUtility::translate(
530546
$localisationKey,
531547
$this->getControllerExtensionKey()

Classes/Utility/ArrayUtility.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright notice
77
*
88
* (c) 2015 Ingo Pfennigstorf <[email protected]>
9-
* Goettingen State Library
9+
* Göttingen State Library
1010
*
1111
* All rights reserved
1212
*
@@ -30,20 +30,23 @@
3030
class ArrayUtility
3131
{
3232
/**
33-
* Removes all values from $array whose
34-
* * keys begin with __
35-
* * values are an empty string.
36-
*
37-
* Specifically aimed at the __hmac and __referrer keys introduced by Fluid
38-
* forms as well as the text submitted by empty search form fields.
33+
* Recursively cleans an arguments array:
34+
* - Removes keys beginning with "__"
35+
* - Removes elements that are empty strings
36+
* Call this on $_GET, $_POST, or Extbase argument arrays before use.
3937
*/
4038
public static function cleanArgumentsArray(array $array): array
4139
{
42-
foreach ($array as $key => &$value) {
40+
foreach ($array as $key => $value) {
4341
if (str_starts_with($key, '__') || $value === '') {
4442
unset($array[$key]);
4543
} elseif (is_array($value)) {
46-
self::cleanArgumentsArray($value);
44+
$cleaned = self::cleanArgumentsArray($value);
45+
if ($cleaned === []) {
46+
unset($array[$key]);
47+
} else {
48+
$array[$key] = $cleaned;
49+
}
4750
}
4851
}
4952

Classes/Utility/FrontendUtility.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright notice
77
*
88
* (c) 2015 Ingo Pfennigstorf <[email protected]>
9-
* Goettingen State Library
9+
* Göttingen State Library
1010
*
1111
* All rights reserved
1212
*
@@ -28,41 +28,49 @@
2828
* ************************************************************* */
2929

3030
/**
31-
* Utility for JavaScripts, Views, ...
31+
* Utility for JavaScript and view helpers in the frontend.
3232
*/
3333
class FrontendUtility
3434
{
3535
/**
36-
* Stores information about the active query in the »underlyingQuery« JavaScript variable.
36+
* Generates a JS assignment for the active query and its paging/facet data as `underlyingQuery` variable.
3737
*
38-
* @param int|null $position of the record in the result list
39-
* @param array $arguments overrides $this->requestArguments if set
38+
* @param mixed $query Query parameter(s) (string or array, depending on your usage)
39+
* @param array $settings Complete plugin or extension settings
40+
* @param int|null $position Position in result list (1-based, null if not applicable)
41+
* @param array $arguments Arguments array (request arguments or override)
42+
* @return string JS assignment or empty string
4043
*
4144
* @throws \JsonException
4245
*/
43-
public static function addQueryInformationAsJavaScript($query, array $settings, ?int $position = null, array $arguments = []): string
44-
{
45-
if ($settings['paging']['detailPagePaging']) {
46-
if (array_key_exists('underlyingQuery', $arguments)) {
46+
public static function addQueryInformationAsJavaScript(
47+
$query,
48+
array $settings,
49+
?int $position = null,
50+
array $arguments = []
51+
): string {
52+
if (!empty($settings['paging']['detailPagePaging'])) {
53+
// If the arguments contain an 'underlyingQuery' sub-array, use it
54+
if (array_key_exists('underlyingQuery', $arguments) && is_array($arguments['underlyingQuery'])) {
4755
$arguments = $arguments['underlyingQuery'];
4856
}
4957

5058
$underlyingQuery = ['q' => $query];
51-
if (array_key_exists('facet', $arguments)) {
59+
if (!empty($arguments['facet'])) {
5260
$underlyingQuery['facet'] = $arguments['facet'];
5361
}
5462

5563
if ($position !== null) {
5664
$underlyingQuery['position'] = $position;
5765
}
5866

59-
if (array_key_exists('count', $arguments)) {
67+
if (isset($arguments['count'])) {
6068
$underlyingQuery['count'] = $arguments['count'];
61-
} elseif (array_key_exists('count', $settings)) {
69+
} elseif (isset($settings['count'])) {
6270
$underlyingQuery['count'] = $settings['count'];
6371
}
6472

65-
if (array_key_exists('sort', $arguments)) {
73+
if (isset($arguments['sort'])) {
6674
$underlyingQuery['sort'] = $arguments['sort'];
6775
}
6876

@@ -72,8 +80,22 @@ public static function addQueryInformationAsJavaScript($query, array $settings,
7280
return '';
7381
}
7482

83+
/**
84+
* Calculates index values for detail navigation based on a position key.
85+
*
86+
* @param array $underlyingQueryInfo Array including at least a 'position' key (1-based).
87+
* @return array Index info: positionIndex, previousIndex, nextIndex, resultIndexOffset
88+
*/
7589
public static function getIndexes(array $underlyingQueryInfo): array
7690
{
77-
return ['positionIndex' => $underlyingQueryInfo['position'] - 1, 'previousIndex' => max([$underlyingQueryInfo['position'] - 2, 0]), 'nextIndex' => $underlyingQueryInfo['position'], 'resultIndexOffset' => (0 === $underlyingQueryInfo['position'] - 1) ? 0 : 1];
91+
// Default to position=1 if missing
92+
$position = (int)($underlyingQueryInfo['position'] ?? 1);
93+
94+
return [
95+
'positionIndex' => $position - 1,
96+
'previousIndex' => max($position - 2, 0),
97+
'nextIndex' => $position,
98+
'resultIndexOffset' => ($position - 1 === 0) ? 0 : 1,
99+
];
78100
}
79101
}

Configuration/TSconfig/ContentElementWizard.tsconfig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ mod.wizards.newContentElement.wizardItems.plugins {
22
elements {
33
find {
44
iconIdentifier = ext-find-ce-wizard
5-
title = LLL:EXT:find/Resources/Private/Language/locallang_be.xml:ce.title
6-
description = LLL:EXT:find/Resources/Private/Language/locallang_be.xml:ce.description
5+
title = LLL:EXT:find/Resources/Private/Language/locallang_be.xlf:ce.title
6+
description = LLL:EXT:find/Resources/Private/Language/locallang_be.xlf:ce.description
77
tt_content_defValues {
88
CType = list
99
list_type = find_find
1010
}
1111
}
1212
}
13+
show := addToList(find)
1314
}

0 commit comments

Comments
 (0)