Skip to content
Closed
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"require": {
"php": "~8.2.0 || ~8.3.0 || ~8.4.0",
"ext-openssl": "*",
"symfony/event-dispatcher": "^5.4.29|^6.4.0|^7.0.0",
"symfony/type-info": "^7.2.4"
},
"require-dev": {
Expand Down
208 changes: 207 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/Cryptography/CryptographySubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Cryptography;

use Patchlevel\Hydrator\Event\PostExtract;
use Patchlevel\Hydrator\Event\PreHydrate;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class CryptographySubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly PayloadCryptographer $cryptography,
) {
}

public function preHydrate(PreHydrate $event): void
{
$event->data = $this->cryptography->decrypt($event->metadata, $event->data);
}

public function postExtract(PostExtract $event): void
{
$event->data = $this->cryptography->encrypt($event->metadata, $event->data);
}

/** @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>> */
public static function getSubscribedEvents(): array
{
return [
PreHydrate::class => 'preHydrate',
PostExtract::class => 'postExtract',
];
}
}
17 changes: 17 additions & 0 deletions src/Event/PostExtract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Event;

use Patchlevel\Hydrator\Metadata\ClassMetadata;

final class PostExtract
{
/** @param array<string, mixed> $data */
public function __construct(
public readonly ClassMetadata $metadata,
public array $data,
) {
}
}
17 changes: 17 additions & 0 deletions src/Event/PreHydrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Event;

use Patchlevel\Hydrator\Metadata\ClassMetadata;

final class PreHydrate
{
/** @param array<string, mixed> $data */
public function __construct(
public readonly ClassMetadata $metadata,
public array $data,
) {
}
}
19 changes: 10 additions & 9 deletions src/MetadataHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Patchlevel\Hydrator;

use Patchlevel\Hydrator\Cryptography\PayloadCryptographer;
use Patchlevel\Hydrator\Event\PostExtract;
use Patchlevel\Hydrator\Event\PreHydrate;
use Patchlevel\Hydrator\Metadata\AttributeMetadataFactory;
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Patchlevel\Hydrator\Metadata\ClassNotFound;
use Patchlevel\Hydrator\Metadata\MetadataFactory;
use Patchlevel\Hydrator\Normalizer\HydratorAwareNormalizer;
use ReflectionParameter;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Throwable;
use TypeError;

Expand All @@ -26,7 +28,7 @@ final class MetadataHydrator implements Hydrator

public function __construct(
private readonly MetadataFactory $metadataFactory = new AttributeMetadataFactory(),
private readonly PayloadCryptographer|null $cryptographer = null,
private readonly EventDispatcherInterface|null $eventDispatcher = null,
) {
}

Expand All @@ -46,9 +48,9 @@ public function hydrate(string $class, array $data): object
throw new ClassNotSupported($class, $e);
}

if ($this->cryptographer) {
$data = $this->cryptographer->decrypt($metadata, $data);
}
$event = new PreHydrate($metadata, $data);
$this->eventDispatcher?->dispatch($event);
$data = $event->data;

$object = $metadata->newInstance();

Expand Down Expand Up @@ -173,11 +175,10 @@ public function extract(object $object): array
$data[$propertyMetadata->fieldName()] = $value;
}

if ($this->cryptographer) {
return $this->cryptographer->encrypt($metadata, $data);
}
$event = new PostExtract($metadata, $data);
$this->eventDispatcher?->dispatch($event);

return $data;
return $event->data;
} finally {
unset($this->stack[$objectId]);
}
Expand Down
11 changes: 10 additions & 1 deletion tests/Benchmark/HydratorWithCryptographyBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Patchlevel\Hydrator\Tests\Benchmark;

use Patchlevel\Hydrator\Cryptography\CryptographySubscriber;
use Patchlevel\Hydrator\Cryptography\PersonalDataPayloadCryptographer;
use Patchlevel\Hydrator\Cryptography\Store\InMemoryCipherKeyStore;
use Patchlevel\Hydrator\Hydrator;
use Patchlevel\Hydrator\MetadataHydrator;
use Patchlevel\Hydrator\Tests\Benchmark\Fixture\PersonalDataProfileCreated;
use Patchlevel\Hydrator\Tests\Benchmark\Fixture\ProfileId;
use PhpBench\Attributes as Bench;
use Symfony\Component\EventDispatcher\EventDispatcher;

#[Bench\BeforeMethods('setUp')]
final class HydratorWithCryptographyBench
Expand All @@ -23,8 +25,15 @@ public function __construct()
{
$this->store = new InMemoryCipherKeyStore();

$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber(
new CryptographySubscriber(
PersonalDataPayloadCryptographer::createWithOpenssl($this->store),
),
);

$this->hydrator = new MetadataHydrator(
cryptographer: PersonalDataPayloadCryptographer::createWithOpenssl($this->store),
eventDispatcher: $eventDispatcher,
);
}

Expand Down
Loading