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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `#[Link(rel: 'collection', href: ...)]` on a row's `onGet()` names the list it belongs to: a write to the row purges the list's URI and the variants that differ only in default-valued parameters, so the list needs no `#[DependsOn]` for its rows. A path-only href (`/posts`) takes the context scheme (`#[ContextScheme]`).
- The `purge_skipped` log entry (fields `uri`, the write's URI without its query, and `reason`, `unmatched_query`), recorded when a write on a resource without a cache attribute cannot address its own key because its query lacks a required `onGet()` parameter.
- `AppSurrogateKeyModule($key)` adds the key to every entry this package stores and, as the first key, to the `Surrogate-Key` of each response it stores or writes as a donut, so invalidating the key on the server and at the CDN clears the whole application; a CDN that ignores the keys past a length limit (Fastly: 16,384 bytes) still reads it. Install it before or after `QueryRepositoryModule`.
- `#[DependsOn(uri: ..., key: ...)]`: `key` names the rows in the body `onGet()` returns, so `#[DependsOn(uri: 'app://self/article?id={id}', key: 'articles')]` with `$this->body['articles'] = [['id' => 1, ...], ['id' => 2, ...]]` depends on `?id=1` and `?id=2`; a row that lacks the variable is skipped, an empty list names no URI, and a variable no row holds is read from the body's top level, then from the arguments. A body or row list that can be read only once, such as a `Generator`, is left in the body as an `ArrayIterator` with every row, under the keys it yielded unless a key repeats, as with `yield from`, which leaves a list.
- `#[DependsOn(uri: ..., key: ...)]`: `key` names the rows in the body `onGet()` returns, so `#[DependsOn(uri: 'app://self/article?id={id}', key: 'articles')]` with `$this->body['articles'] = [['id' => 1, ...], ['id' => 2, ...]]` depends on `?id=1` and `?id=2`; a row that lacks the variable is skipped, an empty list names no URI, a variable no row holds is read from the body's top level, then from the arguments, and rows that hold none of the template's variables throw `DependsOnNotExpandable`. A body or row list that can be read only once, such as a `Generator`, is left in the body as an `ArrayIterator` with every row, under the keys it yielded unless a key repeats, as with `yield from`, which leaves a list.

### Changed
- `ResourceDonut`, `ResourceBodyEvaluator`, `RefreshAnnotatedCommand` and the default log classes `Log\HostRuntime`, `Log\ProcessSession`, `Log\SafeSemanticLogger` and `Log\ShutdownFlush` are `@internal`: depend on the interfaces they implement and the modules that bind them. `RefreshAnnotatedCommand`'s constructor and `ResourceDonut::create()`/`refresh()` take new arguments, so code that builds them with `new` has to take them from the injector instead.
Expand Down
4 changes: 2 additions & 2 deletions docs/llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ The list is tagged with `app://self/article?id=1` and `?id=2`, and a write to ei

- `key` names the top-level key of the body `onGet()` returns that holds the rows; rows nested deeper are not read. A list - an array list, or a `Traversable` read with `foreach` - names one URI per row; one array or object names one URI. An array row is read as it is, an object row as `(array)`, so its public properties. An empty list names none. A list that can be read only once - a `Generator`, or an `Iterator` that is neither an `IteratorAggregate` nor an `ArrayIterator` - is read once and put back in the body as an `ArrayIterator` with every row, under the keys it yielded; when a key repeats, as `yield from` two lists gives, the rows are a list.
- Without `key`, the body itself holds the rows: a body that is a list of rows names one URI per row, and a body that is one array or object names one URI from its keys or public properties.
- With `key`, a variable any row holds is read from the rows alone. One no row holds is read from the body's top level, then from the `onGet()` arguments. Without `key`, every variable is read that way, as `#[Link]` expands its `href`: `$this->body['id'] = 5` with `app://self/article?id={id}` names `?id=5`, and a list of values under an exploded query variable names one URI per element, as in `#[Purge]`: `['id' => [1, 2, 3, 5]]` with `app://self/item{?id*}`, or `['ids' => [1, 2, 3, 5]]` with `app://self/item?id={ids*}`.
- With `key`, a variable any row holds is read from the rows alone. One no row holds is read from the body's top level, then from the `onGet()` arguments, as `{channelId}` in `app://self/article?ulid={channelId}-{id}`. Rows that hold none of the template's variables fail the declaration: with `app://self/item?id={id}`, a column named `article_id` is not filled from an argument named `id`. Without `key`, every variable is read that way, as `#[Link]` expands its `href`: `$this->body['id'] = 5` with `app://self/article?id={id}` names `?id=5`, and a list of values under an exploded query variable names one URI per element, as in `#[Purge]`: `['id' => [1, 2, 3, 5]]` with `app://self/item{?id*}`, or `['ids' => [1, 2, 3, 5]]` with `app://self/item?id={ids*}`.
- A value that cannot expand - an array, or an object that is not `Stringable`, other than a list of values under an exploded query variable - counts as absent, so the next source is read: `$this->body['user'] = ['id' => $user, 'name' => ...]` with `app://self/item?id={user}` and `onGet(string $user)` expands from the argument.
- A row that lacks a variable is skipped, as a guide row put among article rows; the declaration fails only when no row has it. With `key`, an empty list names no URI. Without `key` and with no row - a `null` body, as a 404 without one has, or an empty list - a template the top level and the arguments fill names its URI, and one they do not names none.
- The arguments are read back from the request query, under the parameter name or its snake_case form, then from the parameter default. A parameter bound by `#[ResourceParam]`, on the parameter or on the method, takes its value from another resource whatever the query holds, so a declaration that uses it fails unless the body holds the value under that name; the resource it is read from is a dependency without a declaration (see Cache Dependencies). A value another provider binds (a web context attribute) is not in the query either: with a default, the URI silently expands from the default instead of the bound value; without one, the declaration fails. A query value is the string the request sent, so an `int` argument expands as written and a `BackedEnum` default fails as not scalar.
Expand All @@ -193,7 +193,7 @@ The list is tagged with `app://self/article?id=1` and `?id=2`, and a write to ei
- A URI also tagged by hand with `UriTagInterface::fromAssoc()` is tagged once.
- `#[DependsOn]` only adds the tag. The URI is invalidated by the named resource's own write, or by a `#[Purge]`/`#[Refresh]` that names it.

A declaration that cannot expand fails the GET with `BEAR\QueryRepository\Exception\DependsOnNotExpandable`, and nothing is stored. Its `reason` is `no '<variable>'` (no such argument), `'<variable>' is not an onGet() parameter` (nor a key of the row or the body), `'<variable>' is bound by #[ResourceParam]` (and not a key of the row or the body) or `'<variable>' is <type>` (neither scalar, `Stringable` nor `null`; a list only under an exploded query variable). Only a declaration on `onGet()` is read; static analysis reports one on a class (PHPStan `attribute.target`, Psalm `InvalidAttribute`).
A declaration that cannot expand fails the GET with `BEAR\QueryRepository\Exception\DependsOnNotExpandable`, and nothing is stored. Its `reason` is `no '<variable>'` (no such argument), `'<variable>' is not an onGet() parameter` (nor a key of the row or the body), `'<variable>' is bound by #[ResourceParam]` (and not a key of the row or the body), `'<variable>' is <type>` (neither scalar, `Stringable` nor `null`; a list only under an exploded query variable) or `no row under '<key>' holds '<variable>'` (with `key`, no row holds any variable of the template; several are joined with `or`). Only a declaration on `onGet()` is read; static analysis reports one on a class (PHPStan `attribute.target`, Psalm `InvalidAttribute`).

A row added to a list is caught by the write that adds it, not by the row tags: that write must invalidate the list's own URI, or a URI the list's `onGet()` declares. An embedded list that ran with a body gives the page its own URI tag whether or not it is cached, so a write that invalidates the list's URI reaches the page too.

Expand Down
2 changes: 1 addition & 1 deletion src-annotation/DependsOn.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Tags the stored resource with each URI `uri` expands to, so a write to that URI invalidates it and every page that embeds it
*
* On onGet(), `key` names the top-level key of the body it returns that holds the rows, or the body itself without `key`, and each row names one URI; a variable
* no row holds is read from the body's top level, then the onGet() arguments.
* no row holds is read from the body's top level, then the onGet() arguments. With `key`, rows that hold none of the variables throw DependsOnNotExpandable.
*
* @see https://bearsunday.github.io/manuals/1.0/en/cache.html#dependson
*/
Expand Down
45 changes: 43 additions & 2 deletions src/DependsOnExpansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use function array_key_exists;
use function array_map;
use function array_merge;
use function array_unique;
use function get_debug_type;
use function implode;
use function in_array;
use function is_array;
use function is_scalar;
Expand Down Expand Up @@ -48,7 +50,8 @@ public function __construct(
/**
* Each variable is read from the row, then the body's top level, then the arguments, past a value that cannot expand; with
* `key`, one any row holds is read from the rows alone: {@see DependsOn}. A row that lacks a variable is skipped, and the
* declaration fails when every row does. Without a row, the top level and the arguments name a URI, or none when they cannot.
* declaration fails when every row does, or, with `key`, when its rows hold none of its variables. Without a row, the top
* level and the arguments name a URI, or none when they cannot.
*
* @param list<array<array-key, mixed>> $rows
*
Expand All @@ -70,6 +73,8 @@ public function tags(DependsOn $dependsOn, array $rows): array
return $named ? $this->uriTemplates->tags($this->uriTag, $template, $fallback) : [];
}

$this->throwUnlessRowsHoldAVariable($dependsOn, $variables, $carried, $rows);

$tags = [];
$missing = '';
$expanded = false;
Expand All @@ -86,12 +91,48 @@ public function tags(DependsOn $dependsOn, array $rows): array
}

if (! $expanded) {
throw new DependsOnNotExpandable($this->source, sprintf("#[DependsOn(uri: '%s')]", $template), $missing);
throw new DependsOnNotExpandable($this->source, $this->declaration($template), $missing);
}

return $tags;
}

/**
* @param list<string> $variables
* @param array<string, mixed> $carried the values the rows hold that can expand
* @param list<array<array-key, mixed>> $rows
*/
private function throwUnlessRowsHoldAVariable(DependsOn $dependsOn, array $variables, array $carried, array $rows): void
{
if ($dependsOn->key === null || $variables === [] || $carried !== []) {
return;
}

throw new DependsOnNotExpandable($this->source, $this->declaration($dependsOn->uri), $this->notInRows($dependsOn->key, $variables, $rows));
}

/**
* @param list<string> $variables
* @param list<array<array-key, mixed>> $rows
*/
private function notInRows(string $key, array $variables, array $rows): string
{
foreach ($variables as $variable) {
foreach ($rows as $row) {
if (array_key_exists($variable, $row)) {
return sprintf("'%s' is %s", $variable, get_debug_type($row[$variable]));
}
}
}

return sprintf("no row under '%s' holds '%s'", $key, implode("' or '", array_unique($variables)));
}

private function declaration(string $template): string
{
return sprintf("#[DependsOn(uri: '%s')]", $template);
}

/**
* @param list<string> $variables
* @param list<string> $exploded the variables a list is given to one element at a time
Expand Down
5 changes: 3 additions & 2 deletions src/Exception/DependsOnNotExpandable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
*
* `source` is `<uri> onGet()`, the stored resource's URI. `reason` is one of `no '<variable>'` (no such parameter value),
* `'<variable>' is not an onGet() parameter` (nor a key of the row or the body), `'<variable>' is bound by #[ResourceParam]` (not a
* key of the row or the body, and its value comes from another resource, not the query) and `'<variable>' is <type>` (neither
* scalar, Stringable nor null; a list only under an exploded query variable).
* key of the row or the body, and its value comes from another resource, not the query), `'<variable>' is <type>` (neither
* scalar, Stringable nor null; a list only under an exploded query variable) and `no row under '<key>' holds '<variable>'` (with
* `key`, no row holds any variable of the template; several are joined with `or`).
* Nothing is stored. A dependency the declaration cannot express is tagged by hand with UriTagInterface::fromAssoc().
*/
final class DependsOnNotExpandable extends LogicException
Expand Down
2 changes: 1 addition & 1 deletion tests/CACHE_DEPENDENCY_TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ invalidateTags([uriTag('page://self/html/blog-posting')]) → BlogPosting invali

### Row Dependencies (`#[DependsOn]`)

A declaration on `onGet()` expands once per row under its `key`, from the rows alone for a variable any row holds, and otherwise from the body's top level, then the call's arguments; without `key`, from the top level, as `#[Link]` does, then the arguments, and a list under an exploded query variable names one URI per element. A value that cannot expand is read past. A write to a declared URI invalidates the resource and every page that embeds it.
A declaration on `onGet()` expands once per row under its `key`, from the rows alone for a variable any row holds, and otherwise from the body's top level, then the call's arguments; rows that hold none of its variables fail it. Without `key`, it expands from the top level, as `#[Link]` does, then the arguments, and a list under an exploded query variable names one URI per element. A value that cannot expand is read past. A write to a declared URI invalidates the resource and every page that embeds it.

```text
RowList body['articles'] = [['id' => 1, ...], ['id' => 2, ...]], #[DependsOn('item?id={id}', key: 'articles')] → tags item?id=1, 2
Expand Down
8 changes: 6 additions & 2 deletions tests/DependsOnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ public function testAVariableTheRowLacksIsReadFromTheTopLevelThenTheArguments():
{
$uri = 'app://self/rows/row-fallback?channelId=c2';
$keys = $this->keys($this->resource->get($uri));
$this->assertSame(1, array_count_values($keys)[$this->tag('app://self/rows/taxonomy?ulid=c1')] ?? 0);
$this->assertContains($this->tag('app://self/rows/article?ulid=c1-1'), $keys);
$this->assertContains($this->tag('app://self/rows/article?ulid=c1-2'), $keys);
$this->assertContains($this->tag('app://self/rows/article?ulid=c2-1'), $keys);
$this->assertContains($this->tag('app://self/rows/article?ulid=c2-2'), $keys);

Expand Down Expand Up @@ -496,7 +497,10 @@ public static function notExpandableProvider(): array
return [
'variable not an onGet() parameter' => ['app://self/rows/not-parameter?channelId=c1', 'app://self/rows/not-parameter?channelId=c1 onGet()', "'ulid' is not an onGet() parameter"],
'variable whose only value is an array' => ['app://self/rows/nested-only', 'app://self/rows/nested-only onGet()', "'user' is array"],
'variable in neither the rows, the top level nor the arguments' => ['app://self/rows/row-missing', 'app://self/rows/row-missing onGet()', "'ulid' is not an onGet() parameter"],
'variable no row holds, nor the top level or the arguments' => ['app://self/rows/row-missing', 'app://self/rows/row-missing onGet()', "no row under 'articles' holds 'ulid'"],
'variable no row holds, beside an argument of the same name' => ['app://self/rows/renamed-column?id=7', 'app://self/rows/renamed-column?id=7 onGet()', "no row under 'articles' holds 'id'"],
'variable the rows hold only as an array, beside an argument of the same name' => ['app://self/rows/renamed-column?id=7&shape=nested', 'app://self/rows/renamed-column?id=7&shape=nested onGet()', "'id' is array"],
'variables no row holds, one of them an argument' => ['app://self/rows/row-missing-pair?channelId=c1', 'app://self/rows/row-missing-pair?channelId=c1 onGet()', "no row under 'articles' holds 'channelId' or 'ulid'"],
'variable bound by #[ResourceParam]' => ['app://self/rows/resource-param-bound', 'app://self/rows/resource-param-bound onGet()', "'channelId' is bound by #[ResourceParam]"],
'variable bound by #[ResourceParam] and also in the query' => ['app://self/rows/resource-param-bound?channelId=c1', 'app://self/rows/resource-param-bound?channelId=c1 onGet()', "'channelId' is bound by #[ResourceParam]"],
'variable bound by a method-level #[ResourceParam]' => ['app://self/rows/method-resource-param-bound', 'app://self/rows/method-resource-param-bound onGet()', "'channelId' is bound by #[ResourceParam]"],
Expand Down
22 changes: 22 additions & 0 deletions tests/Fake/fake-app/src/Resource/App/Rows/RenamedColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace FakeVendor\HelloWorld\Resource\App\Rows;

use BEAR\RepositoryModule\Annotation\Cacheable;
use BEAR\RepositoryModule\Annotation\DependsOn;
use BEAR\Resource\ResourceObject;

/** Rows that name the article ID `article_id`, or hold it as an array, beside an argument named as the template's variable */
#[Cacheable]
class RenamedColumn extends ResourceObject
{
#[DependsOn(uri: 'app://self/rows/item?id={id}', key: 'articles')]
public function onGet(int $id, string $shape = 'renamed'): static
{
$this->body['articles'] = $shape === 'nested' ? [['id' => ['a' => 1]]] : [['article_id' => 1], ['article_id' => 2]];

return $this;
}
}
2 changes: 1 addition & 1 deletion tests/Fake/fake-app/src/Resource/App/Rows/RowFallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#[Cacheable]
class RowFallback extends ResourceObject
{
#[DependsOn(uri: 'app://self/rows/taxonomy?ulid={channelUlid}', key: 'articles')]
#[DependsOn(uri: 'app://self/rows/article?ulid={channelUlid}-{id}', key: 'articles')]
#[DependsOn(uri: 'app://self/rows/article?ulid={channelId}-{id}', key: 'articles')]
public function onGet(string $channelId): static
{
Expand Down
22 changes: 22 additions & 0 deletions tests/Fake/fake-app/src/Resource/App/Rows/RowMissingPair.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace FakeVendor\HelloWorld\Resource\App\Rows;

use BEAR\RepositoryModule\Annotation\Cacheable;
use BEAR\RepositoryModule\Annotation\DependsOn;
use BEAR\Resource\ResourceObject;

/** Rows that hold neither variable of the template, one of which is an argument */
#[Cacheable]
class RowMissingPair extends ResourceObject
{
#[DependsOn(uri: 'app://self/rows/article?ulid={channelId}-{ulid}', key: 'articles')]
public function onGet(string $channelId): static
{
$this->body['articles'] = [['id' => 1], ['id' => 2]];

return $this;
}
}
Loading