Skip to content

Commit

Permalink
Add context option
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Oct 22, 2024
1 parent 3f73d78 commit 2e5bc30
Show file tree
Hide file tree
Showing 37 changed files with 242 additions and 74 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ parameters:
path: src/Attribute/RouteParamValue/PropertyValues.php

-
message: "#^Method Sofascore\\\\PurgatoryBundle\\\\Cache\\\\Configuration\\\\Subscriptions\\:\\:getIterator\\(\\) should return Traversable\\<int, array\\{routeName\\: string, routeParams\\?\\: array\\<string, array\\{type\\: string, values\\: array\\<int, mixed\\>, optional\\?\\: true\\}\\>, if\\?\\: string, actions\\?\\: non\\-empty\\-array\\<int, Sofascore\\\\PurgatoryBundle\\\\Listener\\\\Enum\\\\Action\\>\\}\\> but returns ArrayIterator\\<int, array\\{routeName\\: string, routeParams\\?\\: array\\<string, array\\{type\\: string, values\\: array\\<int, mixed\\>, optional\\?\\: bool\\}\\>, if\\?\\: string, actions\\?\\: non\\-empty\\-array\\<int, Sofascore\\\\PurgatoryBundle\\\\Listener\\\\Enum\\\\Action\\>\\}\\>\\.$#"
message: "#^Method Sofascore\\\\PurgatoryBundle\\\\Cache\\\\Configuration\\\\Subscriptions\\:\\:getIterator\\(\\) should return Traversable\\<int, array\\{routeName\\: string, routeParams\\?\\: array\\<string, array\\{type\\: string, values\\: array\\<int, mixed\\>, optional\\?\\: true\\}\\>, if\\?\\: string, actions\\?\\: non\\-empty\\-array\\<int, Sofascore\\\\PurgatoryBundle\\\\Listener\\\\Enum\\\\Action\\>, context\\?\\: array\\<string, bool\\|float\\|int\\|string\\|null\\>\\}\\> but returns ArrayIterator\\<int, array\\{routeName\\: string, routeParams\\?\\: array\\<string, array\\{type\\: string, values\\: array\\<int, mixed\\>, optional\\?\\: bool\\}\\>, if\\?\\: string, actions\\?\\: non\\-empty\\-array\\<int, Sofascore\\\\PurgatoryBundle\\\\Listener\\\\Enum\\\\Action\\>, context\\?\\: array\\<string, bool\\|float\\|int\\|string\\|null\\>\\}\\>\\.$#"
count: 1
path: src/Cache/Configuration/Subscriptions.php

Expand Down
2 changes: 2 additions & 0 deletions src/Attribute/PurgeOn.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class PurgeOn
* @param ?non-empty-array<string, string|non-empty-list<string>|ValuesInterface> $routeParams
* @param string|non-empty-list<string>|null $route
* @param value-of<Action>|non-empty-list<value-of<Action>|Action>|Action|null $actions
* @param array<string, ?scalar> $context
*/
public function __construct(
public readonly string $class,
Expand All @@ -38,6 +39,7 @@ public function __construct(
string|Expression|null $if = null,
string|array|null $route = null,
string|array|Action|null $actions = null,
public readonly array $context = [],
) {
$this->target = \is_array($target) || \is_string($target) ? new ForProperties($target) : $target;
$this->routeParams = null !== $routeParams ? self::normalizeRouteParams($routeParams) : null;
Expand Down
2 changes: 2 additions & 0 deletions src/Cache/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class Configuration implements \Countable
* routeParams?: array<string, array{type: string, values: list<mixed>, optional?: true}>,
* if?: string,
* actions?: non-empty-list<Action>,
* context?: array<string, ?scalar>,
* }>> $configuration
*/
public function __construct(
Expand Down Expand Up @@ -58,6 +59,7 @@ public function count(): int
* routeParams?: array<string, array{type: string, values: list<mixed>, optional?: true}>,
* if?: string,
* actions?: non-empty-list<Action>,
* context?: array<string, ?scalar>,
* }>>
*/
public function toArray(): array
Expand Down
4 changes: 4 additions & 0 deletions src/Cache/Configuration/ConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function load(): Configuration
$config['actions'] = $subscription->actions;
}

if ($subscription->context) {
$config['context'] = $subscription->context;
}

$configuration[$key][] = $config;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Cache/Configuration/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* routeParams?: array<string, array{type: string, values: list<mixed>, optional?: true}>,
* if?: string,
* actions?: non-empty-list<Action>,
* context?: array<string, ?scalar>,
* }>
*/
final class Subscriptions implements \IteratorAggregate, \Countable
Expand All @@ -23,6 +24,7 @@ final class Subscriptions implements \IteratorAggregate, \Countable
* routeParams?: array<string, array{type: string, values: list<mixed>, optional?: true}>,
* if?: string,
* actions?: non-empty-list<Action>,
* context?: array<string, ?scalar>,
* }> $subscriptions
*/
public function __construct(
Expand Down Expand Up @@ -55,6 +57,7 @@ public function key(): string
* routeParams?: array<string, array{type: string, values: list<mixed>, optional?: true}>,
* if?: string,
* actions?: non-empty-list<Action>,
* context?: array<string, ?scalar>,
* }>
*/
public function toArray(): array
Expand Down
1 change: 1 addition & 0 deletions src/Cache/PropertyResolver/AssociationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class: $associationClass,
route: $routeMetadata->route,
actions: $routeMetadata->purgeOn->actions,
if: $if,
context: $routeMetadata->purgeOn->context,
);

return true;
Expand Down
1 change: 1 addition & 0 deletions src/Cache/PropertyResolver/EmbeddableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class: $routeMetadata->purgeOn->class,
route: $routeMetadata->route,
actions: $routeMetadata->purgeOn->actions,
if: $routeMetadata->purgeOn->if,
context: $routeMetadata->purgeOn->context,
);
}

Expand Down
1 change: 1 addition & 0 deletions src/Cache/PropertyResolver/PropertyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class: $routeMetadata->purgeOn->class,
route: $routeMetadata->route,
actions: $routeMetadata->purgeOn->actions,
if: $routeMetadata->purgeOn->if,
context: $routeMetadata->purgeOn->context,
);

return true;
Expand Down
6 changes: 5 additions & 1 deletion src/Cache/RouteMetadata/YamlMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
final class YamlMetadataProvider implements RouteMetadataProviderInterface
{
private const ALLOWED_KEYS = ['class', 'target', 'route_params', 'if', 'actions'];
private const ALLOWED_KEYS = ['class', 'target', 'route_params', 'if', 'actions', 'context'];
private ?YamlParser $yamlParser = null;

/**
Expand Down Expand Up @@ -87,6 +87,7 @@ private function provideFromFile(array $configuration, RouteCollection $routeCol
* route_params?: ?non-empty-array<string, string|non-empty-list<string>|TaggedValue>,
* if?: ?string,
* actions?: value-of<Action>|non-empty-list<value-of<Action>|Action>|Action|null,
* context?: array<string, ?scalar>,
* } $purgeOn
*/
foreach ($purgeOns as $purgeOn) {
Expand All @@ -109,6 +110,7 @@ private function provideFromFile(array $configuration, RouteCollection $routeCol
* route_params?: ?non-empty-array<string, string|non-empty-list<string>|TaggedValue>,
* if?: ?string,
* actions?: value-of<Action>|non-empty-list<value-of<Action>|Action>|Action|null,
* context?: array<string, ?scalar>,
* } $purgeOn
*/
private function validate(array $purgeOn, string $routeName): void
Expand All @@ -130,6 +132,7 @@ private function validate(array $purgeOn, string $routeName): void
* route_params?: ?non-empty-array<string, string|non-empty-list<string>|TaggedValue>,
* if?: ?string,
* actions?: value-of<Action>|non-empty-list<value-of<Action>|Action>|Action|null,
* context?: array<string, ?scalar>,
* } $purgeOn
*/
private function buildPurgeOn(array $purgeOn): PurgeOn
Expand All @@ -140,6 +143,7 @@ class: $purgeOn['class'],
routeParams: isset($purgeOn['route_params']) ? array_map($this->buildRouteParam(...), $purgeOn['route_params']) : null,
if: $purgeOn['if'] ?? null,
actions: $purgeOn['actions'] ?? null,
context: $purgeOn['context'] ?? [],
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Cache/Subscription/PurgeSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class PurgeSubscription
* @param class-string $class
* @param array<string, ValuesInterface> $routeParams
* @param ?non-empty-list<Action> $actions
* @param array<string, ?scalar> $context
*/
public function __construct(
public readonly string $class,
Expand All @@ -23,7 +24,8 @@ public function __construct(
public readonly string $routeName,
public readonly Route $route,
public readonly ?array $actions,
public readonly ?Expression $if = null,
public readonly ?Expression $if,
public readonly array $context,
) {
}
}
1 change: 1 addition & 0 deletions src/Cache/Subscription/PurgeSubscriptionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class: $purgeOn->class,
route: $routeMetadata->route,
actions: $purgeOn->actions,
if: $purgeOn->if,
context: $routeMetadata->purgeOn->context,
);

continue;
Expand Down
17 changes: 17 additions & 0 deletions src/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* routeParams?: array<string, array{type: string, values: list<mixed>, optional?: true}>,
* if?: string,
* actions?: non-empty-list<Action>,
* context?: array<string, ?scalar>,
* }>>
*/
private function findSubscriptions(string $subscription, bool $withProperties): array
Expand All @@ -246,6 +247,7 @@ private function findSubscriptions(string $subscription, bool $withProperties):
* routeParams?: array<string, array{type: string, values: list<mixed>, optional?: true}>,
* if?: string,
* actions?: non-empty-list<Action>,
* context?: array<string, ?scalar>,
* }>>
*/
private function findSubscriptionsForRoute(string $routeName): array
Expand All @@ -267,6 +269,7 @@ private function findSubscriptionsForRoute(string $routeName): array
* routeParams?: array<string, array{type: string, values: list<mixed>, optional?: true}>,
* if?: string,
* actions?: non-empty-list<Action>,
* context?: array<string, ?scalar>,
* }>> $configuration
*/
private function display(SymfonyStyle $io, array $configuration): void
Expand All @@ -286,6 +289,7 @@ private function display(SymfonyStyle $io, array $configuration): void
['Route Params', isset($subscription['routeParams']) ? $this->formatRouteParams($subscription['routeParams']) : 'NONE'],
['Condition', $subscription['if'] ?? 'NONE'],
['Actions', isset($subscription['actions']) ? $this->formatActions($subscription['actions']) : 'ANY'],
['Context', isset($subscription['context']) ? $this->formatContext($subscription['context']) : 'NONE'],
],
);
}
Expand Down Expand Up @@ -335,6 +339,19 @@ private function formatActions(array $actions): string
);
}

/**
* @param array<string, ?scalar> $context
*/
private function formatContext(array $context): string
{
$values = [];
foreach ($context as $key => $value) {
$values[] = \sprintf('%s: %s', $key, var_export($value, true));
}

return implode(\PHP_EOL, $values);
}

private function configuration(): Configuration
{
return $this->configuration ??= $this->configurationLoader->load();
Expand Down
15 changes: 14 additions & 1 deletion src/Listener/EntityChangeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use Sofascore\PurgatoryBundle\Listener\Enum\Action;
use Sofascore\PurgatoryBundle\Purger\PurgeRequest;
use Sofascore\PurgatoryBundle\Purger\PurgerInterface;
use Sofascore\PurgatoryBundle\RouteProvider\PurgeRoute;
use Sofascore\PurgatoryBundle\RouteProvider\RouteProviderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class EntityChangeListener
Expand Down Expand Up @@ -91,11 +93,22 @@ private function handleChanges(LifecycleEventArgs $eventArgs, Action $action): v
referenceType: UrlGeneratorInterface::ABSOLUTE_URL,
);

$this->queuedPurgeRequests[$url] ??= new PurgeRequest(
$this->queuedPurgeRequests[$this->generateHash(
url: $url,
route: $route,
)] ??= new PurgeRequest(
url: $url,
route: $route,
);
}
}
}

private function generateHash(string $url, PurgeRoute $route): string
{
$context = $route->context;
ksort($context);

return ContainerBuilder::hash([$url, $context]);
}
}
1 change: 1 addition & 0 deletions src/RouteProvider/AbstractEntityRouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private function processValidSubscriptions(Subscriptions $subscriptions, array $
yield new PurgeRoute(
name: $subscription['routeName'],
params: $routeParams,
context: $subscription['context'] ?? [],
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/RouteProvider/PurgeRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ final class PurgeRoute
{
/**
* @param array<string, ?scalar> $params
* @param array<string, ?scalar> $context
*/
public function __construct(
public readonly string $name,
public readonly array $params,
public readonly array $context = [],
) {
}
}
2 changes: 2 additions & 0 deletions templates/profiler.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<th scope="col" class="key">#</th>
<th scope="col">URLs</th>
<th scope="col">Routes</th>
<th scope="col">Context</th>
<th scope="col">Time</th>
</tr>
</thead>
Expand All @@ -70,6 +71,7 @@
<th scope="row">{{ key + 1 }}</th>
<td>{{ purge.requests|map(request => request.url)|join('\n')|nl2br }}</td>
<td>{{ purge.requests|map(request => request.route.name)|join('\n')|nl2br }}</td>
<td>{{ purge.requests|map(request => request.route.context|json_encode(constant('JSON_FORCE_OBJECT')))|join('\n')|nl2br }}</td>
<td>{{ '%.2f'|format(purge.time * 1000) }} <span class="unit">ms</span></td>
</tr>
{% endfor %}
Expand Down
20 changes: 20 additions & 0 deletions tests/Application/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sofascore\PurgatoryBundle\Tests\Functional\TestApplication\Entity\Measurements;
use Sofascore\PurgatoryBundle\Tests\Functional\TestApplication\Entity\Person;
use Sofascore\PurgatoryBundle\Tests\Functional\TestApplication\Entity\Plane;
use Sofascore\PurgatoryBundle\Tests\Functional\TestApplication\Entity\Plant;
use Sofascore\PurgatoryBundle\Tests\Functional\TestApplication\Entity\Ship;
use Sofascore\PurgatoryBundle\Tests\Functional\TestApplication\Enum\Country;
use Symfony\Component\PropertyAccess\PropertyPath;
Expand Down Expand Up @@ -842,4 +843,23 @@ public function testOldValuesWithMissingRouteParamsAreNotPurged(): void
static fn (string $url): bool => str_starts_with($url, '/for-owner-and-veterinarian'),
));
}

/**
* @see PlantController::detailsAction
*/
public function testPurgeOnWithContext(): void
{
$plant = new Plant();
$plant->name = 'Plant Ash';

$this->entityManager->persist($plant);
$this->entityManager->flush();

self::assertUrlIsPurged('/plant/'.$plant->id);

$purgeRequests = self::getPurger()->getPurgedRequests();

self::assertCount(1, $purgeRequests);
self::assertSame(['qux' => true, 'corge' => 2], $purgeRequests[0]->route->context);
}
}
3 changes: 3 additions & 0 deletions tests/Cache/Configuration/CachedConfigurationLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class: 'Foo',
route: new Route('/foo'),
actions: [Action::Update],
if: null,
context: [],
),
new PurgeSubscription(
class: 'Foo',
Expand All @@ -58,6 +59,7 @@ class: 'Foo',
route: new Route('/foo'),
actions: [Action::Create],
if: null,
context: ['qux' => true, 'corge' => 2],
),
]);

Expand Down Expand Up @@ -92,6 +94,7 @@ class: 'Foo',
],
],
'actions' => [Action::Create],
'context' => ['qux' => true, 'corge' => 2],
],
],
], $configuration->toArray());
Expand Down
Loading

0 comments on commit 2e5bc30

Please sign in to comment.