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 @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- `#[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.
- `#[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`) is resolved as BEAR.Resource resolves a link, under 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 to every `Surrogate-Key` it sets, so invalidating the key on the server and at the CDN clears the whole application. 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.
Expand Down
2 changes: 1 addition & 1 deletion docs/llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Article extends ResourceObject
```

- The purge reaches every variant of the collection that differs only in default-valued parameters (see Purging query variants): with `onGet(?int $authorId = null, int $page = 1)`, purging `app://self/articles` drops `?page=2` and `?author_id=7`. A list keyed by a required parameter, such as one author's articles, is another group: name it with `#[Purge]` on the write.
- The href expands as a `#[Purge]` template does, from the body the write returns, then from its query. An href that does not expand to a resource URI, such as a path-only one, and a `#[Link]` of another rel purge nothing.
- The href expands as a `#[Purge]` template does, from the body the write returns, then from its query. An href that expands to a path (`/articles`) is resolved as BEAR.Resource resolves a link, under the context scheme (`#[ContextScheme]`), so the purge and the link crawl name the same resource; an href that does not expand to a resource URI and a `#[Link]` of another rel purge nothing.
- Which side declares the relation: when the list knows its rows and their number is bounded, declare `#[DependsOn]` on the list; when the row knows its list and the rows are unbounded, declare `#[Link(rel: 'collection')]` on the row. The collection link keeps the list to its own key.
- A write that moves a row from one list to another names both lists with `#[Purge]`. The old value comes from the body the write returns, under a name other than the argument's, as the body is read before the query:

Expand Down
6 changes: 5 additions & 1 deletion src/RefreshAnnotatedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use BEAR\Resource\ResourceInterface;
use BEAR\Resource\ResourceObject;
use BEAR\Resource\Uri;
use BEAR\Resource\UriFactory;
use Ray\Aop\MethodInvocation;
use ReflectionMethod;

Expand All @@ -31,6 +32,7 @@ public function __construct(
private ResourceInterface $resource,
private CacheIdentityFactory $identityFactory,
private DependsOnTemplates $templates,
private UriFactory $uriFactory,
) {
}

Expand Down Expand Up @@ -123,6 +125,8 @@ private function uris(ResourceObject $ro, string $template, array $query): array
}

/**
* Resolved as BEAR\Resource\Linker resolves a link, so a path-only href takes the context scheme (#[ContextScheme])
*
* @param list<string> $uris
*
* @return list<Uri>
Expand All @@ -132,7 +136,7 @@ private function resourceUris(array $uris): array
$resourceUris = [];
foreach ($uris as $uri) {
try {
$resourceUris[] = new Uri($uri);
$resourceUris[] = ($this->uriFactory)($uri);
} catch (UriException) {
continue;
}
Expand Down
24 changes: 19 additions & 5 deletions tests/CollectionLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@

use BEAR\Resource\ResourceInterface;
use BEAR\Resource\Uri;
use FakeVendor\HelloWorld\Resource\App\Collection\Comment;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Ray\Di\Injector;

/** A write to a row purges the collection its onGet() links with #[Link(rel: 'collection')] */
class CollectionLinkTest extends TestCase
{
private Injector $injector;
private ResourceInterface $resource;
private QueryRepositoryInterface $repository;

protected function setUp(): void
{
$injector = new Injector(ModuleFactory::getInstance('FakeVendor\HelloWorld'), __DIR__ . '/tmp');
$this->resource = $injector->getInstance(ResourceInterface::class);
$this->repository = $injector->getInstance(QueryRepositoryInterface::class);
$this->injector = new Injector(ModuleFactory::getInstance('FakeVendor\HelloWorld'), __DIR__ . '/tmp');
$this->resource = $this->injector->getInstance(ResourceInterface::class);
$this->repository = $this->injector->getInstance(QueryRepositoryInterface::class);
$this->repository->purge(new Uri('app://self/collection/posts'));
$this->repository->purge(new Uri('app://self/collection/author-posts?author_id=7'));
$this->repository->purge(new Uri('app://self/collection/author-posts?author_id=8'));
Expand Down Expand Up @@ -63,8 +65,8 @@ public function testOnlyTheCollectionIsPurged(): void
$this->assertNotNull($this->repository->get(new Uri($authorPosts)));
}

/** An href that is not a resource URI, as a path-only one, is skipped after the write has run */
public function testHrefThatIsNotAResourceUriIsSkipped(): void
/** An href that expands to a path names the list at that path; an href that is not a resource URI is skipped */
public function testHrefExpandingToAPathIsResolvedAndAnUnusableOneSkipped(): void
{
$posts = 'app://self/collection/posts';
$this->resource->get($posts);
Expand All @@ -75,6 +77,18 @@ public function testHrefThatIsNotAResourceUriIsSkipped(): void
$this->assertNull($this->repository->get(new Uri($posts)));
}

/** A row taken from the injector has no URI, and its path-only href still purges the list */
public function testPathOnlyHrefOfARowWithoutUriPurgesTheList(): void
{
$posts = 'app://self/collection/posts';
$this->resource->get($posts);

$comment = $this->injector->getInstance(Comment::class);
$comment->onPut(1);

$this->assertNull($this->repository->get(new Uri($posts)));
}

/** A write that moves a row to another list purges the old list with the value its body carries */
public function testMovePurgesTheOldListFromTheBody(): void
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Fake/fake-app/src/Resource/App/Collection/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class Comment extends ResourceObject
{
#[Link(rel: 'collection', href: '/collection/posts')]
#[Link(rel: 'collection', href: 'app://self/collection/posts')]
#[Link(rel: 'collection', href: '{/kind}/posts')]
#[Link(rel: 'collection', href: '')]
public function onGet(int $id): static
{
$this->body = ['id' => $id];
Expand All @@ -20,7 +20,7 @@ public function onGet(int $id): static

public function onPut(int $id): static
{
$this->body = ['id' => $id];
$this->body = ['id' => $id, 'kind' => 'collection'];

return $this;
}
Expand Down
Loading