Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:
branches:
- "main"
- "1"
- "2"
- "release/**"
- "feature/**"
push:
branches:
- "main"
- "1"
- "2"

jobs:
ci:
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v2
with:
# disable auto detection of JS tests (remove if any JS tests are added)
js: false
17 changes: 0 additions & 17 deletions .vscode/settings.json

This file was deleted.

7 changes: 7 additions & 0 deletions _config/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
Name: discoverer-service
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Discoverer\Service\SearchService:
constructor:
indexPrefix: '`SS_ENVIRONMENT_TYPE`'
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
"Querying"
],
"require": {
"php": "^8.1",
"silverstripe/framework": "^5.2"
"php": "^8.3",
"silverstripe/framework": "^6"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"slevomat/coding-standard": "^8.8"
"phpunit/phpunit": "^11.3",
"slevomat/coding-standard": "~8.18.0"
},
"autoload": {
"psr-4": {
Expand All @@ -44,5 +44,10 @@
},
"scripts": {
"phpcs": "phpcs --standard=phpcs.xml --extensions=php --encoding=utf-8"
},
"extra": {
"branch-alias": {
"dev-main": "3.0.x-dev"
}
}
}
4 changes: 2 additions & 2 deletions docs/detailed-result-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ simple methods available that you can access anywhere.
* `getRecords()`: A `PaginatedList` of `Record` objects that were returned by the search service based on your `Query`.
* `getFacets`: An `ArrayList` of `Facet` objects that were returned by the search service based on your `Query`.

The `Results` class is also a `ViewableData` object, so these methods can be access in your template with `$isSuccess`,
The `Results` class is also a `ModelData` object, so these methods can be access in your template with `$isSuccess`,
`$Records`, and `$Facets`.

## `Record` class

The `Record` class is a simple `ViewableData` class, and the purpose of it is to provide you with access to the fields
The `Record` class is a simple `ModelData` class, and the purpose of it is to provide you with access to the fields
that are returned by your search service, and to make those fields available to you in (EG) Silverstripe templates.

**Note:** A `Record` is not assumed to have a linked `DataObject`. The recommendation would be for you to index all of
Expand Down
4 changes: 4 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<file>src</file>
<file>tests</file>

<!-- Don't sniff test classes, because they include PHP attributes that cause Slevomat to throw errors -->
<exclude-pattern>./tests/*</exclude-pattern>
<!-- Don't sniff third party libraries -->
<exclude-pattern>./vendor/*</exclude-pattern>
<exclude-pattern>*/thirdparty/*</exclude-pattern>
Expand Down Expand Up @@ -146,6 +148,8 @@
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification"/>
<!-- Leave the order up to us to decide -->
<exclude name="SlevomatCodingStandard.Classes.ClassStructure.IncorrectGroupOrder"/>
</rule>

<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
Expand Down
31 changes: 16 additions & 15 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/silverstripe/framework/tests/bootstrap.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
>
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
<source>
<include>
<directory suffix=".php">tests</directory>
</include>
</source>
</phpunit>
58 changes: 38 additions & 20 deletions src/Analytics/AnalyticsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@

namespace SilverStripe\Discoverer\Analytics;

use JsonSerializable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\View\ViewableData;
use SilverStripe\Model\ModelData;

class AnalyticsData extends ViewableData
class AnalyticsData extends ModelData implements JsonSerializable
{

use Injectable;

private ?string $engineName = null;
private ?string $indexName = null;

private ?string $queryString = null;

private mixed $documentId = null;

private mixed $requestId = null;

public function getEngineName(): ?string
public function getIndexName(): ?string
{
return $this->engineName;
return $this->indexName;
}

public function setEngineName(?string $engineName): AnalyticsData
public function setIndexName(?string $indexName): AnalyticsData
{
$this->engineName = $engineName;
$this->indexName = $indexName;

return $this;
}
Expand Down Expand Up @@ -66,17 +67,42 @@ public function setRequestId(mixed $requestId): AnalyticsData
return $this;
}

public function forTemplate(): ?string
public function forTemplate(): string
{
$data = $this->getDataArray();

if (!$data) {
return '';
}

$query = [
'_searchAnalytics' => base64_encode(json_encode($data)),
];

return http_build_query($query);
}

public function jsonSerialize(): array
{
return [
'indexName' => $this->getIndexName(),
'queryString' => $this->getQueryString(),
'documentId' => $this->getDocumentId(),
'requestId' => $this->getRequestId(),
];
}

private function getDataArray(): array
{
$data = [];

$engineName = $this->getEngineName();
$indexName = $this->getIndexName();
$query = $this->getQueryString();
$documentId = $this->getDocumentId();
$requestId = $this->getRequestId();

if ($engineName) {
$data['engineName'] = $engineName;
if ($indexName) {
$data['indexName'] = $indexName;
}

if ($query) {
Expand All @@ -91,15 +117,7 @@ public function forTemplate(): ?string
$data['requestId'] = $requestId;
}

if (!$data) {
return null;
}

$query = [
'_searchAnalytics' => base64_encode(json_encode($data)),
];

return http_build_query($query);
return $data;
}

}
4 changes: 2 additions & 2 deletions src/Analytics/AnalyticsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AnalyticsMiddleware implements HTTPMiddleware
use Configurable;
use Injectable;

public const ENV_ANALYTICS_ENABLED = 'SEARCH_ANALYTICS_ENABLED';
public const string ENV_ANALYTICS_ENABLED = 'SEARCH_ANALYTICS_ENABLED';

private ?LoggerInterface $logger = null;

Expand Down Expand Up @@ -58,7 +58,7 @@ public function process(HTTPRequest $request, callable $delegate): mixed
$analyticsData->setQueryString($data['queryString'] ?? null);
$analyticsData->setRequestId($data['requestId'] ?? null);
$analyticsData->setDocumentId($data['documentId'] ?? null);
$analyticsData->setEngineName($data['engineName'] ?? null);
$analyticsData->setIndexName($data['indexName'] ?? null);

$service = SearchService::create();
$service->processAnalytics($analyticsData);
Expand Down
16 changes: 8 additions & 8 deletions src/Query/Facet/Facet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class Facet

use Injectable;

public const TYPE_VALUE = 'VALUE';
public const TYPE_RANGE = 'RANGE';
public const string TYPE_VALUE = 'VALUE';
public const string TYPE_RANGE = 'RANGE';

public const TYPES = [
public const array TYPES = [
self::TYPE_VALUE,
self::TYPE_RANGE,
];
Expand All @@ -43,7 +43,7 @@ public function getLimit(): ?int
return $this->limit;
}

public function setLimit(?int $limit): self
public function setLimit(?int $limit): static
{
$this->limit = $limit;

Expand All @@ -55,7 +55,7 @@ public function getName(): ?string
return $this->name;
}

public function setName(?string $name): self
public function setName(?string $name): static
{
$this->name = $name;

Expand All @@ -67,7 +67,7 @@ public function getFieldName(): ?string
return $this->fieldName;
}

public function setFieldName(?string $fieldName): self
public function setFieldName(?string $fieldName): static
{
$this->fieldName = $fieldName;

Expand All @@ -83,7 +83,7 @@ public function addRange(
string|int|float|null $from = null,
string|int|float|null $to = null,
?string $name = null
): self {
): static {
// If a range is added, then we'll update the type
$this->type = self::TYPE_RANGE;
$range = FacetRange::create($from, $to, $name);
Expand All @@ -101,7 +101,7 @@ public function getType(): ?string
/**
* @throws Exception
*/
public function setType(?string $type): self
public function setType(?string $type): static
{
if (!in_array($type, self::TYPES, true)) {
throw new Exception(sprintf('Invalid type "%s" provided', $type));
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Facet/FacetCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getFacets(): array
return $this->facets;
}

public function addFacet(Facet $facet): self
public function addFacet(Facet $facet): static
{
$this->facets[] = $facet;

Expand Down
6 changes: 3 additions & 3 deletions src/Query/Facet/FacetRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getFrom(): float|int|string|null
return $this->from;
}

public function setFrom(float|int|string|null $from): self
public function setFrom(float|int|string|null $from): static
{
$this->from = $from;

Expand All @@ -33,7 +33,7 @@ public function getTo(): float|int|string|null
return $this->to;
}

public function setTo(float|int|string|null $to): self
public function setTo(float|int|string|null $to): static
{
$this->to = $to;

Expand All @@ -45,7 +45,7 @@ public function getName(): ?string
return $this->name;
}

public function setName(?string $name): self
public function setName(?string $name): static
{
$this->name = $name;

Expand Down
Loading