-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Refine LiveSearch search demand
The search demand implementation used by the LiveSearch is refined to be more flexible. Instead of a hard-coded list of properties, the constructor now receives a list of `DemandProperty` instances that accept everything that is backed by the `DemandPropertyName` enum, allowing to pass only the required properties. The `SearchDemand` class is immutable by design, it's API must not allow modification by 3rd parties. However, there is an internal `MutableSearchDemand` acting as a helper that allows to add / override properties. A `SearchDemand` object may be constructed from a request object, where it's payload must contain properties from the aforementioned `DemandPropertyName` enum. Resolves: #99941 Releases: main Change-Id: I880855943a67237fa765cd3da94333e8aaa3a2a2 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/77842 Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de> Tested-by: core-ci <typo3@b13.com> Tested-by: Georg Ringer <georg.ringer@gmail.com> Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de> Reviewed-by: Georg Ringer <georg.ringer@gmail.com> Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de> Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
- Loading branch information
1 parent
26ca687
commit 0a9b218
Showing
14 changed files
with
390 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
typo3/sysext/backend/Classes/Search/LiveSearch/SearchDemand.php
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
typo3/sysext/backend/Classes/Search/LiveSearch/SearchDemand/DemandProperty.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
namespace TYPO3\CMS\Backend\Search\LiveSearch\SearchDemand; | ||
|
||
final class DemandProperty | ||
{ | ||
public function __construct( | ||
private readonly DemandPropertyName $name, | ||
private readonly mixed $value, | ||
) { | ||
} | ||
|
||
public function getName(): DemandPropertyName | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getValue(): mixed | ||
{ | ||
return $this->value; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
typo3/sysext/backend/Classes/Search/LiveSearch/SearchDemand/DemandPropertyName.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
namespace TYPO3\CMS\Backend\Search\LiveSearch\SearchDemand; | ||
|
||
enum DemandPropertyName | ||
{ | ||
case query; | ||
case limit; | ||
case offset; | ||
case searchProviders; | ||
} |
36 changes: 36 additions & 0 deletions
36
typo3/sysext/backend/Classes/Search/LiveSearch/SearchDemand/MutableSearchDemand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
namespace TYPO3\CMS\Backend\Search\LiveSearch\SearchDemand; | ||
|
||
/** | ||
* @internal for internal use only, no public API | ||
*/ | ||
final class MutableSearchDemand extends SearchDemand | ||
{ | ||
public function setProperty(DemandPropertyName $name, mixed $value): self | ||
{ | ||
$this->demandProperties[$name->name] = new DemandProperty($name, $value); | ||
|
||
return $this; | ||
} | ||
|
||
public function freeze(): SearchDemand | ||
{ | ||
return new SearchDemand($this->demandProperties); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
typo3/sysext/backend/Classes/Search/LiveSearch/SearchDemand/SearchDemand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
namespace TYPO3\CMS\Backend\Search\LiveSearch\SearchDemand; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use TYPO3\CMS\Backend\Search\LiveSearch\SearchProviderInterface; | ||
|
||
/** | ||
* Holds necessary data to query data from a search provider | ||
* | ||
* @internal may change in further iterations, do not rely on it | ||
*/ | ||
class SearchDemand | ||
{ | ||
public const DEFAULT_LIMIT = 50; | ||
|
||
/** | ||
* @var DemandProperty[] $demandProperties | ||
*/ | ||
protected array $demandProperties = []; | ||
|
||
/** | ||
* @param DemandProperty[] $demandProperties | ||
*/ | ||
final public function __construct(array $demandProperties = []) | ||
{ | ||
$this->demandProperties = array_reduce($demandProperties, static function (array $result, DemandProperty $item) { | ||
$result[$item->getName()->name] = $item; | ||
return $result; | ||
}, []); | ||
} | ||
|
||
public function getProperty(DemandPropertyName $demandPropertyName): ?DemandProperty | ||
{ | ||
return $this->demandProperties[$demandPropertyName->name] ?? null; | ||
} | ||
|
||
/** | ||
* @return DemandProperty[] | ||
*/ | ||
public function getProperties(): array | ||
{ | ||
return $this->demandProperties; | ||
} | ||
|
||
public function getQuery(): string | ||
{ | ||
return $this->getProperty(DemandPropertyName::query)?->getValue() ?? ''; | ||
} | ||
|
||
public function getLimit(): int | ||
{ | ||
return (int)($this->getProperty(DemandPropertyName::limit)?->getValue() ?? self::DEFAULT_LIMIT); | ||
} | ||
|
||
public function getOffset(): int | ||
{ | ||
return (int)($this->getProperty(DemandPropertyName::offset)?->getValue() ?? 0); | ||
} | ||
|
||
/** | ||
* @return class-string<SearchProviderInterface>[] | ||
*/ | ||
public function getSearchProviders(): array | ||
{ | ||
return $this->getProperty(DemandPropertyName::searchProviders)?->getValue() ?? []; | ||
} | ||
|
||
public static function fromRequest(ServerRequestInterface $request): static | ||
{ | ||
$demandProperties = []; | ||
foreach (DemandPropertyName::cases() as $demandProperty) { | ||
$demandPropertyName = $demandProperty->name; | ||
$valueFromRequest = $request->getParsedBody()[$demandPropertyName] ?? $request->getQueryParams()[$demandPropertyName] ?? null; | ||
if ($valueFromRequest !== null) { | ||
$demandProperties[] = new DemandProperty($demandProperty, $valueFromRequest); | ||
} | ||
} | ||
|
||
return new static($demandProperties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.