Skip to content

Commit 8dd01f4

Browse files
Data collection for profiler (#26)
Co-authored-by: Jacob Dreesen <[email protected]>
1 parent 6dc8a9c commit 8dd01f4

File tree

18 files changed

+839
-6
lines changed

18 files changed

+839
-6
lines changed

config/services.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
use Neusta\Pimcore\HttpCacheBundle\Cache\CacheTagChecker\ElementCacheTagChecker;
1313
use Neusta\Pimcore\HttpCacheBundle\Cache\CacheTagChecker\StaticCacheTagChecker;
1414
use Neusta\Pimcore\HttpCacheBundle\Cache\ResponseTagger;
15+
use Neusta\Pimcore\HttpCacheBundle\Cache\ResponseTagger\CacheTagCollectionResponseTagger;
1516
use Neusta\Pimcore\HttpCacheBundle\Cache\ResponseTagger\OnlyWhenActiveResponseTagger;
1617
use Neusta\Pimcore\HttpCacheBundle\Cache\ResponseTagger\RemoveDisabledTagsResponseTagger;
1718
use Neusta\Pimcore\HttpCacheBundle\CacheActivator;
19+
use Neusta\Pimcore\HttpCacheBundle\DataCollector;
1820
use Neusta\Pimcore\HttpCacheBundle\Element\ElementRepository;
1921
use Neusta\Pimcore\HttpCacheBundle\Element\InvalidateElementListener;
2022
use Neusta\Pimcore\HttpCacheBundle\Element\TagElementListener;
2123
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2224
use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg;
25+
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
2326
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
2427

2528
return static function (ContainerConfigurator $configurator) {
@@ -52,6 +55,10 @@
5255
->decorate('neusta_pimcore_http_cache.response_tagger', null, -100)
5356
->args([service('.inner'), service('neusta_pimcore_http_cache.cache_activator')]);
5457

58+
$services->set('.neusta_pimcore_http_cache.collect_tags_response_tagger', CacheTagCollectionResponseTagger::class)
59+
->decorate('neusta_pimcore_http_cache.response_tagger', null, 1)
60+
->args([service('.inner')]);
61+
5562
$services->alias(ResponseTagger::class, 'neusta_pimcore_http_cache.response_tagger');
5663

5764
$services->set('neusta_pimcore_http_cache.cache_tag_checker', StaticCacheTagChecker::class)
@@ -85,4 +92,13 @@
8592
$services->set('neusta_pimcore_http_cache.element.invalidate_listener', InvalidateElementListener::class)
8693
->arg('$cacheInvalidator', service('neusta_pimcore_http_cache.cache_invalidator'))
8794
->arg('$dispatcher', service('event_dispatcher'));
95+
96+
$services->set('neusta_pimcore_http_cache.data_collector', DataCollector::class)
97+
->arg('$cacheTagCollector', service('.neusta_pimcore_http_cache.collect_tags_response_tagger'))
98+
->arg('$configuration', param('neusta_pimcore_http_cache.config'))
99+
->tag('data_collector', [
100+
'template' => '@NeustaPimcoreHttpCache/profiler.html.twig',
101+
'id' => 'pimcore_http_cache',
102+
'priority' => 255,
103+
]);
88104
};

src/Cache/CacheTags.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
final class CacheTags implements \IteratorAggregate
1111
{
1212
/**
13-
* @var list<CacheTag>
13+
* @var array<string, CacheTag>
1414
*/
1515
private readonly array $tags;
1616

@@ -19,7 +19,12 @@ final class CacheTags implements \IteratorAggregate
1919
*/
2020
public function __construct(CacheTag ...$tags)
2121
{
22-
$this->tags = $tags;
22+
$indexedTags = [];
23+
foreach ($tags as $tag) {
24+
$indexedTags[$tag->toString()] = $tag;
25+
}
26+
27+
$this->tags = $indexedTags;
2328
}
2429

2530
public static function fromString(string $tag, ?CacheType $type = null): self
@@ -53,12 +58,12 @@ public static function fromElements(array $elements): self
5358
*/
5459
public function getIterator(): \ArrayIterator
5560
{
56-
return new \ArrayIterator($this->tags);
61+
return new \ArrayIterator(array_values($this->tags));
5762
}
5863

5964
public function with(CacheTag|self ...$tags): self
6065
{
61-
$newTags = $this->tags;
66+
$newTags = array_values($this->tags);
6267
foreach ($tags as $tag) {
6368
if ($tag instanceof self) {
6469
$newTags = [...$newTags, ...$tag->tags];
@@ -80,7 +85,10 @@ public function withoutDisabled(CacheTagChecker $checker): self
8085
*/
8186
public function toArray(): array
8287
{
83-
$tags = array_map(static fn (CacheTag $tag): string => $tag->toString(), $this->tags);
88+
$tags = array_map(
89+
static fn (CacheTag $tag): string => $tag->toString(), array_values($this->tags),
90+
);
91+
8492
natsort($tags);
8593

8694
return $tags;

src/Cache/CacheType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public function applyTo(string $tag): string;
99
public function toString(): string;
1010

1111
public function isEmpty(): bool;
12+
13+
public function identifier(): string;
1214
}

src/Cache/CacheType/CustomCacheType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ public function isEmpty(): bool
2929
{
3030
return false;
3131
}
32+
33+
public function identifier(): string
34+
{
35+
return $this->type;
36+
}
3237
}

src/Cache/CacheType/ElementCacheType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public function isEmpty(): bool
3737
{
3838
return false;
3939
}
40+
41+
public function identifier(): string
42+
{
43+
return $this->type->value;
44+
}
4045
}

src/Cache/CacheType/EmptyCacheType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ public function isEmpty(): bool
2020
{
2121
return true;
2222
}
23+
24+
public function identifier(): string
25+
{
26+
return 'empty';
27+
}
2328
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Neusta\Pimcore\HttpCacheBundle\Cache\ResponseTagger;
5+
6+
use Neusta\Pimcore\HttpCacheBundle\Cache\CacheTags;
7+
use Neusta\Pimcore\HttpCacheBundle\Cache\ResponseTagger;
8+
use Symfony\Contracts\Service\ResetInterface;
9+
10+
final class CacheTagCollectionResponseTagger implements ResponseTagger, ResetInterface
11+
{
12+
public CacheTags $collectedTags;
13+
14+
public function __construct(
15+
private readonly ResponseTagger $inner,
16+
) {
17+
$this->collectedTags = new CacheTags();
18+
}
19+
20+
public function tag(CacheTags $tags): void
21+
{
22+
$this->collectedTags = $this->collectedTags->with($tags);
23+
$this->inner->tag($tags);
24+
}
25+
26+
public function reset(): void
27+
{
28+
$this->collectedTags = new CacheTags();
29+
}
30+
}

src/DataCollector.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Neusta\Pimcore\HttpCacheBundle;
4+
5+
use Neusta\Pimcore\HttpCacheBundle\Cache\ResponseTagger\CacheTagCollectionResponseTagger;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
use Symfony\Component\HttpKernel\DataCollector\DataCollector as BaseDataCollector;
9+
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
10+
11+
final class DataCollector extends BaseDataCollector implements LateDataCollectorInterface
12+
{
13+
/**
14+
* @param array<string, mixed> $configuration
15+
*/
16+
public function __construct(
17+
private readonly CacheTagCollectionResponseTagger $cacheTagCollector,
18+
array $configuration = [],
19+
) {
20+
$this->data['configuration'] = $configuration;
21+
}
22+
23+
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
24+
{
25+
// no action needed here, as tags are collected in lateCollect
26+
}
27+
28+
public function lateCollect(): void
29+
{
30+
foreach ($this->cacheTagCollector->collectedTags as $tag) {
31+
$this->data['tags'][] = [
32+
'tag' => $tag->toString(), 'type' => $tag->type->identifier(),
33+
];
34+
}
35+
}
36+
37+
/**
38+
* @return array<array{tag: string, type: string}>
39+
*/
40+
public function getTags(): array
41+
{
42+
return $this->data['tags'] ?? [];
43+
}
44+
45+
/**
46+
* @return array<string, mixed>
47+
*/
48+
public function getConfiguration(): array
49+
{
50+
return $this->data['configuration'] ?? [];
51+
}
52+
53+
public function getName(): string
54+
{
55+
return 'pimcore_http_cache';
56+
}
57+
58+
public function reset(): void
59+
{
60+
$this->data['tags'] = [];
61+
}
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Neusta\Pimcore\HttpCacheBundle\DependencyInjection\CompilerPass;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
final class DisableDataCollectorPass implements CompilerPassInterface
9+
{
10+
public function process(ContainerBuilder $container): void
11+
{
12+
if (!$container->hasDefinition('profiler')) {
13+
$container->removeDefinition('.neusta_pimcore_http_cache.collect_tags_response_tagger');
14+
$container->removeDefinition('neusta_pimcore_http_cache.data_collector');
15+
}
16+
}
17+
}

src/DependencyInjection/NeustaPimcoreHttpCacheExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,7 @@ private function registerElements(ContainerBuilder $container, array $config): v
7070
->addTag('kernel.event_listener', ['event' => DataObjectEvents::POST_UPDATE, 'method' => 'onUpdate'])
7171
->addTag('kernel.event_listener', ['event' => DataObjectEvents::PRE_DELETE, 'method' => 'onDelete']);
7272
}
73+
74+
$container->setParameter('neusta_pimcore_http_cache.config', $config);
7375
}
7476
}

0 commit comments

Comments
 (0)