Skip to content
Open
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
61 changes: 61 additions & 0 deletions core/components/minishop3/src/Services/GridConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ class GridConfigService
/** @var modX */
protected $modx;

/**
* Request-scoped memo for getGridConfig(): gridKey:includeHidden → column config.
*
* This cache is safe only because GridConfigService is registered in the
* MODX DI container as a request-scoped singleton (see ServiceRegistry):
* one instance per request, never shared across requests. The memo must
* never be persisted to a long-lived process cache or serialized — it
* holds no TTL and would leak stale data across requests. Mutation
* methods (saveGridConfig, addField, updateField, deleteField) invalidate
* the affected gridKey entries via invalidateGridConfigCache() so the next
* read re-loads from the database.
*
* @var array<string, array<int, array<string, mixed>>>
*/
private array $gridConfigCache = [];

/**
* @param modX $modx
*/
Expand All @@ -29,6 +45,22 @@ public function __construct(modX $modx)
* @return array
*/
public function getGridConfig(string $gridKey, bool $includeHidden = false): array
{
$cacheKey = $this->gridConfigCacheKey($gridKey, $includeHidden);
if (array_key_exists($cacheKey, $this->gridConfigCache)) {
return $this->gridConfigCache[$cacheKey];
}

$fields = $this->loadGridConfig($gridKey, $includeHidden);
$this->gridConfigCache[$cacheKey] = $fields;

return $fields;
}

/**
* @return list<array<string, mixed>>
*/
private function loadGridConfig(string $gridKey, bool $includeHidden): array
{
$query = $this->modx->newQuery(msGridField::class);
$where = ['grid_key' => $gridKey];
Expand Down Expand Up @@ -74,6 +106,27 @@ public function getGridConfig(string $gridKey, bool $includeHidden = false): arr
return $fields;
}

private function gridConfigCacheKey(string $gridKey, bool $includeHidden): string
{
return $gridKey . ':' . ($includeHidden ? '1' : '0');
}

/**
* Call at the start of any msGridField mutation so request-scoped cache stays consistent.
*/
private function beginGridMutation(string $gridKey): void
{
$this->invalidateGridConfigCache($gridKey);
}

private function invalidateGridConfigCache(string $gridKey): void
{
unset(
$this->gridConfigCache[$this->gridConfigCacheKey($gridKey, false)],
$this->gridConfigCache[$this->gridConfigCacheKey($gridKey, true)]
);
}

/**
* Get label considering lexicon_key
*
Expand Down Expand Up @@ -112,6 +165,8 @@ protected function resolveLabel(msGridField $field): string
*/
public function saveGridConfig(string $gridKey, array $fields): bool
{
$this->beginGridMutation($gridKey);

try {
// Get list of field names to keep
$fieldNamesToKeep = [];
Expand Down Expand Up @@ -253,6 +308,8 @@ public function saveGridConfig(string $gridKey, array $fields): bool
*/
public function deleteField(string $gridKey, string $fieldName): array
{
$this->beginGridMutation($gridKey);

try {
$field = $this->modx->getObject(msGridField::class, [
'grid_key' => $gridKey,
Expand Down Expand Up @@ -308,6 +365,8 @@ public function deleteField(string $gridKey, string $fieldName): array
*/
public function addField(string $gridKey, array $data): array
{
$this->beginGridMutation($gridKey);

try {
// Basic validation
if (empty($data['field_name'])) {
Expand Down Expand Up @@ -450,6 +509,8 @@ public function addField(string $gridKey, array $data): array
*/
public function updateField(string $gridKey, string $fieldName, array $data): array
{
$this->beginGridMutation($gridKey);

try {
// Find existing field
$field = $this->modx->getObject(msGridField::class, [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace MiniShop3\Tests\Unit\Services;

require_once __DIR__ . '/../../stubs/ModxStub.php';
require_once __DIR__ . '/../../stubs/GridConfigQueryStub.php';
require_once __DIR__ . '/../../stubs/GridConfigModxStub.php';

use MiniShop3\Services\GridConfigService;
use MiniShop3\Tests\Stubs\GridConfigModxStub;
use PHPUnit\Framework\TestCase;

final class GridConfigServiceMemoTest extends TestCase
{
public function testGetGridConfigMemoizesWithinSameInstance(): void
{
$modx = new GridConfigModxStub();
$service = new GridConfigService($modx);

$first = $service->getGridConfig('orders', true);
$second = $service->getGridConfig('orders', true);

self::assertSame($first, $second);
self::assertSame(1, $modx->getCollectionCalls);
self::assertSame([], $first);
}

public function testIncludeHiddenUsesSeparateCacheEntry(): void
{
$modx = new GridConfigModxStub();
$service = new GridConfigService($modx);

$service->getGridConfig('orders', false);
$service->getGridConfig('orders', true);

self::assertSame(2, $modx->getCollectionCalls);
}

public function testSaveGridConfigInvalidatesMemoForSameRequest(): void
{
$modx = new GridConfigModxStub();
$service = new GridConfigService($modx);

$service->getGridConfig('orders', true);
$service->saveGridConfig('orders', []);
$service->getGridConfig('orders', true);

// 1st read + saveGridConfig delete scan + 2nd read after cache invalidation
self::assertSame(3, $modx->getCollectionCalls);
}
}
53 changes: 53 additions & 0 deletions core/components/minishop3/tests/stubs/GridConfigModxStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace MiniShop3\Tests\Stubs;

use MODX\Revolution\modX;

/**
* modX stub that counts msGridField collection loads for GridConfigService tests.
*/
final class GridConfigModxStub extends modX
{
public int $getCollectionCalls = 0;

/** @var list<object> */
public array $gridFields = [];

/** @var object */
public object $lexicon;

public function __construct()
{
parent::__construct();
$this->lexicon = new class {
public function load(string $topic): void
{
}

public function __invoke(string $key): string
{
return $key;
}
};
}

public function newQuery($class = ''): GridConfigQueryStub
{
return new GridConfigQueryStub();
}

public function getCollection($className, $criteria = null): array
{
$this->getCollectionCalls++;

return $this->gridFields;
}

public function getObject($className, $criteria = null, $cacheFlag = true): ?object
{
return null;
}
}
26 changes: 26 additions & 0 deletions core/components/minishop3/tests/stubs/GridConfigQueryStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace MiniShop3\Tests\Stubs;

/**
* Fluent query stub — GridConfigService only chains where/sortby before getCollection.
*/
final class GridConfigQueryStub
{
public function where(array $criteria): self
{
return $this;
}

public function sortby(string $column, string $direction = 'ASC'): self
{
return $this;
}

public function limit(int $limit): self
{
return $this;
}
}