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
4 changes: 2 additions & 2 deletions src/CacheDependencyCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function collect(ResourceObject $ro): void
{
$dependencies = $this->begin(new TagSet());
$this->declared($ro);
foreach ($this->dependsOnTags->requests($ro) as $request) {
foreach (EmbeddedRequests::of($ro) as $request) {
$this->materialize($request);
$child = $request->resourceObject;
$this->depends($ro, $child, $dependencies->read($child));
Expand All @@ -51,7 +51,7 @@ public function collect(ResourceObject $ro): void
public function materials(ResourceObject $ro, Dependencies $dependencies): void
{
$this->declared($ro);
foreach ($this->dependsOnTags->requests($ro) as $request) {
foreach (EmbeddedRequests::of($ro) as $request) {
$child = $request->resourceObject;
$this->depends($ro, $child, $dependencies->embedded($child));
}
Expand Down
1 change: 1 addition & 0 deletions src/CacheableModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function configure(): void
$this->bind(ConditionalRequest::class);
$this->bind(ConditionalResponseInterface::class)->to(ConditionalResponse::class);
$this->bind(DeclaredCacheControl::class)->in(Scope::SINGLETON);
$this->bind(UriTemplates::class);
$this->bind(RefreshTemplateValidator::class)->in(Scope::SINGLETON);
$this->bind(CacheableClassMatcher::class);
$this->install(new StorageExpiryModule(60, 60 * 60, 60 * 60 * 24));
Expand Down
19 changes: 3 additions & 16 deletions src/CommandInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
use BEAR\Resource\InvokerInterface;
use BEAR\Resource\Request;
use BEAR\Resource\ResourceObject;
use BEAR\Resource\Uri;
use Override;
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;
use ReflectionClass;

use function array_column;
use function array_merge;
use function method_exists;

/**
Expand Down Expand Up @@ -74,30 +71,20 @@ private function purgeAndRefresh(MethodInvocation $invocation, ResourceObject $r
{
$stored = $this->isStored($invocation);
$getQuery = $sent === null ? null : $this->getQuery($invocation, $sent, $stored);
$declared = $this->command->declared($invocation, $ro);
$targets = $this->command->declared($invocation, $ro);
$own = $sent === null || $getQuery === null ? [] : $this->commandQuery->ownUris($sent, $getQuery);
$failure = $this->purge([...$own, ...array_merge(...array_column($declared, 0))]);
$failure = $this->purge([...$own, ...$targets->purge]);
if ($stored && $sent !== null && $getQuery !== null) {
$this->rebuild($invocation, $sent, $getQuery);
}

$this->refreshDeclared($declared);
$this->command->refresh($targets->refresh);

if ($failure !== null) {
throw $failure;
}
}

/** @param list<array{list<Uri>, bool}> $declared each declaration's URIs, and whether it is a #[Refresh] */
private function refreshDeclared(array $declared): void
{
foreach ($declared as [$named, $refresh]) {
if ($refresh) {
$this->command->refresh($named);
}
}
}

/** The resource the write was sent to, cloned with its URI before the handler runs; none without a URI or an onGet() */
private function sent(ResourceObject $target): ResourceObject|null
{
Expand Down
25 changes: 25 additions & 0 deletions src/CommandTargets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace BEAR\QueryRepository;

use BEAR\Resource\Uri;

/**
* The URIs a write method's #[Purge], #[Refresh] and #[Link(rel: 'collection')] declarations target
*
* @internal
*/
final readonly class CommandTargets
{
/**
* @param list<Uri> $purge the #[Purge]/#[Refresh] URIs in declaration order, then the #[Link(rel: 'collection')] URIs
* @param list<Uri> $refresh the #[Refresh] URIs to read again after every purge has run
*/
public function __construct(
public array $purge,
public array $refresh,
) {
}
}
2 changes: 1 addition & 1 deletion src/Dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function ofResult(ResourceObject $ro, Closure $embedded): TagSet|null
private function passedUp(ResourceObject $ro, Closure $embedded): TagSet
{
$tags = new TagSet();
foreach ($this->dependsOnTags->requests($ro) as $request) {
foreach (EmbeddedRequests::of($ro) as $request) {
if ($embedded($request->resourceObject)) {
$tags = $tags->merge($this->cacheTags->tags($request->resourceObject));
}
Expand Down
12 changes: 6 additions & 6 deletions src/DependsOnExpansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
public function __construct(
private UriTagInterface $uriTag,
private DependsOnTemplates $templates,
private UriTemplates $uriTemplates,
private string $source,
private array $top,
private array $arguments,
Expand All @@ -57,8 +57,8 @@ public function __construct(
public function tags(DependsOn $dependsOn, array $rows): array
{
$template = $dependsOn->uri;
$variables = $this->templates->variables($template);
$exploded = $this->templates->explodedQueryNames($template);
$variables = $this->uriTemplates->variables($template);
$exploded = $this->uriTemplates->explodedQueryNames($template);
$fromRows = array_map(fn (array $row): array => $this->expandable($variables, $exploded, $row), $rows);
$carried = $dependsOn->key === null ? [] : array_merge(...$fromRows);
$outside = array_diff_key($this->top, $carried);
Expand All @@ -67,7 +67,7 @@ public function tags(DependsOn $dependsOn, array $rows): array
if ($rows === []) {
$named = $dependsOn->key === null && $this->missing($variables, $fallback, $outside + $given) === null;

return $named ? $this->templates->tags($this->uriTag, $template, $fallback) : [];
return $named ? $this->uriTemplates->tags($this->uriTag, $template, $fallback) : [];
}

$tags = [];
Expand All @@ -82,11 +82,11 @@ public function tags(DependsOn $dependsOn, array $rows): array
}

$expanded = true;
$tags = [...$tags, ...$this->templates->tags($this->uriTag, $template, $values)];
$tags = [...$tags, ...$this->uriTemplates->tags($this->uriTag, $template, $values)];
}

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

return $tags;
Expand Down
21 changes: 2 additions & 19 deletions src/DependsOnTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace BEAR\QueryRepository;

use BEAR\RepositoryModule\Annotation\DependsOn;
use BEAR\Resource\AbstractRequest;
use BEAR\Resource\ResourceObject;
use Generator;

use function array_keys;
use function is_array;
Expand All @@ -25,25 +23,10 @@ public function __construct(
private DependsOnRows $rows,
private CacheIdentityFactory $identityFactory,
private DependsOnTemplates $templates,
private UriTemplates $uriTemplates,
) {
}

/**
* Enumerate only immediate values of the array-cast body, in order and with duplicates.
* Do not execute requests, descend into children, or consume Traversable bodies.
*
* @return Generator<AbstractRequest>
*/
public function requests(ResourceObject $ro): Generator
{
/** @var mixed $body */
foreach ((array) $ro->body as $body) {
if ($body instanceof AbstractRequest) {
yield $body;
}
}
}

/** @return TagSet|null what onGet() declares; null when it declares nothing */
public function declared(ResourceObject $ro): TagSet|null
{
Expand Down Expand Up @@ -79,7 +62,7 @@ private function fromResult(ResourceObject $ro, array $declarations): array
}

$source = sprintf('%s onGet()', (string) $ro->uri);
$expansion = new DependsOnExpansion($this->uriTag, $this->templates, $source, is_array($ro->body) ? $ro->body : [], $arguments, $onGet->names(), array_keys($onGet->bound()));
$expansion = new DependsOnExpansion($this->uriTag, $this->uriTemplates, $source, is_array($ro->body) ? $ro->body : [], $arguments, $onGet->names(), array_keys($onGet->bound()));
$tags = [];
foreach ($declarations as $dependsOn) {
$tags = [...$tags, ...$expansion->tags($dependsOn, $this->rows->of($ro, $dependsOn->key))];
Expand Down
131 changes: 1 addition & 130 deletions src/DependsOnTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,13 @@

use BEAR\RepositoryModule\Annotation\DependsOn;
use BEAR\Resource\ResourceObject;
use BEAR\Resource\Uri;
use ReflectionClass;
use Stringable;

use function array_is_list;
use function array_unique;
use function array_values;
use function explode;
use function is_array;
use function preg_match_all;
use function preg_replace;
use function serialize;
use function sprintf;
use function str_ends_with;
use function strpos;
use function substr;
use function uri_template;

use const PREG_OFFSET_CAPTURE;
use const PREG_SET_ORDER;

/**
* The URI templates #[DependsOn] declares, and the values #[DependsOn], #[Purge] and #[Refresh] expand theirs with
* The #[DependsOn] declarations on a resource's onGet(), cached per class
*
* @internal
*/
Expand All @@ -54,117 +38,4 @@ public function ofOnGet(ResourceObject $ro): array

return $this->onGet[$ro::class] = array_values($declarations);
}

/**
* @param array<array-key, mixed> $values
*
* @return list<string> the tag of each URI the template expands to, one per element of a list given to an exploded query variable
*/
public function tags(UriTagInterface $uriTag, string $template, array $values): array
{
$tags = [];
foreach ($this->variableSets($template, $values) as $variables) {
$tag = $this->tag($uriTag, $template, $variables);
if ($tag !== null) {
$tags[] = $tag;
}
}

return $tags;
}

/**
* One set per element of each list given to an exploded query variable (`{?id*}`, `?id={ids*}`), the product when there are
* several. A null element names no URI and is skipped: the bare URI's group tag reaches every variant.
*
* @param array<array-key, mixed> $values
*
* @return list<array<array-key, mixed>>
*/
public function variableSets(string $template, array $values): array
{
$sets = [$values];
foreach ($this->explodedQueryNames($template) as $name) {
if (! isset($values[$name]) || ! is_array($values[$name]) || ! array_is_list($values[$name])) {
continue;
}

$expanded = [];
foreach ($sets as $set) {
/** @psalm-suppress MixedAssignment */
foreach ($values[$name] as $value) {
if ($value !== null) {
$expanded[] = [$name => $value] + $set;
}
}
}

$sets = $expanded;
}

return $sets;
}

/**
* @param array<array-key, mixed> $values each variable of the template holds a scalar, a Stringable or null: {@see DependsOnExpansion}
*
* @return string|null the tag of the URI the template expands to; null when a variable is null, which names no URI
*/
public function tag(UriTagInterface $uriTag, string $template, array $values): string|null
{
$variables = [];
foreach ($this->variables($template) as $variable) {
/** @var scalar|Stringable|null $value */
$value = $values[$variable];
if ($value === null) {
return null;
}

$variables[$variable] = $value instanceof Stringable ? (string) $value : $value;
}

return $uriTag(new Uri(uri_template($template, $variables)));
}

/** @return list<string> the RFC 6570 variable names in the template */
public function variables(string $template): array
{
preg_match_all('/\{[+#.\/;?&]?([^}]+)\}/', $template, $matches);
$variables = [];
foreach ($matches[1] as $expression) {
foreach (explode(',', $expression) as $spec) {
$variables[] = (string) preg_replace('/(\*|:\d+)$/', '', $spec);
}
}

return $variables;
}

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

/** @return list<string> the query variables a list is given to one element at a time (`{?id*}`, `?id={ids*}`) */
public function explodedQueryNames(string $template): array
{
preg_match_all('/\{([+#.\/;?&]?)([^}]+)\}/', $template, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
$queryStart = strpos($template, '?');
$names = [];
foreach ($matches as $match) {
$operator = $match[1][0];
$inQuery = $operator === '?' || $operator === '&' || ($operator === '' && $queryStart !== false && $match[0][1] > $queryStart);
if (! $inQuery) {
continue;
}

foreach (explode(',', $match[2][0]) as $varSpec) {
if (str_ends_with($varSpec, '*')) {
$names[] = substr($varSpec, 0, -1);
}
}
}

return array_values(array_unique($names));
}
}
1 change: 0 additions & 1 deletion src/DonutCacheModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ protected function configure(): void

private function installAopClassModule(): void
{
$this->bind(DonutRepository::class)->in(Scope::SINGLETON);
$this->bind(DonutRendererInterface::class)->to(DonutRenderer::class);
$this->bindPriorityInterceptor(
$this->matcher->annotatedWith(CacheableResponse::class),
Expand Down
Loading
Loading