Skip to content

Commit

Permalink
feat: doctrine event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed Feb 11, 2024
1 parent 2078a66 commit 424a5cc
Show file tree
Hide file tree
Showing 21 changed files with 397 additions and 144 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"distantmagic/swoole-futures": "^0.1.2",
"doctrine/dbal": "^3.7",
"doctrine/migrations": "^3.6",
"doctrine/orm": "^2.16",
"doctrine/orm": "^3.0",
"doctrine/sql-formatter": "^1.1",
"ezyang/htmlpurifier": "^4.16",
"guzzlehttp/guzzle": "^7.8",
Expand Down
146 changes: 21 additions & 125 deletions composer.lock

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

17 changes: 17 additions & 0 deletions src/Attribute/ListensToDoctrineEntityEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance\Attribute;

use Attribute;
use Distantmagic\Resonance\Attribute as BaseAttribute;

#[Attribute(Attribute::TARGET_CLASS)]
final readonly class ListensToDoctrineEntityEvents extends BaseAttribute
{
/**
* @param class-string $className
*/
public function __construct(public string $className) {}
}
11 changes: 11 additions & 0 deletions src/Attribute/ListensToDoctrineEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance\Attribute;

use Attribute;
use Distantmagic\Resonance\Attribute as BaseAttribute;

#[Attribute(Attribute::TARGET_CLASS)]
final readonly class ListensToDoctrineEvents extends BaseAttribute {}
3 changes: 2 additions & 1 deletion src/DatabaseConnectionPoolConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @psalm-taint-source file $unixSocket
*
* @param non-empty-string $host
* @param null|non-empty-string $password
* @param null|non-empty-string $unixSocket
* @param non-empty-string $username
*/
Expand All @@ -25,7 +26,7 @@ public function __construct(
#[SensitiveParameter]
public bool $logQueries,
#[SensitiveParameter]
public string $password,
public ?string $password,
#[SensitiveParameter]
public bool $poolPrefill,
#[SensitiveParameter]
Expand Down
9 changes: 6 additions & 3 deletions src/DoctrineConnectionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Distantmagic\Resonance\Attribute\GrantsFeature;
use Distantmagic\Resonance\Attribute\Singleton;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
Expand Down Expand Up @@ -35,6 +36,7 @@ public function __construct(
private DoctrineMySQLDriver $doctrineMySQLDriver,
private DoctrinePostgreSQLDriver $doctrinePostgreSQLDriver,
private DoctrineSQLiteDriver $doctrineSQLiteDriver,
private EventManager $eventManager,
private LoggerInterface $logger,
) {
/**
Expand All @@ -49,13 +51,14 @@ public function __construct(
public function buildConnection(string $name = 'default'): Connection
{
return new Connection(
[
config: $this->configuration,
driver: $this->getDriver($name),
eventManager: $this->eventManager,
params: [
'driverOptions' => [
'connectionPoolName' => $name,
],
],
$this->getDriver($name),
$this->configuration,
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/DoctrineEntityListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance;

abstract readonly class DoctrineEntityListener {}
33 changes: 33 additions & 0 deletions src/DoctrineEntityListenerCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance;

use Ds\Map;
use Ds\Set;

readonly class DoctrineEntityListenerCollection
{
/**
* @var Map<class-string,Set<object>>
*/
public Map $listeners;

public function __construct()
{
$this->listeners = new Map();
}

/**
* @param class-string $className
*/
public function addEntityListener(string $className, object $listener): void
{
if (!$this->listeners->hasKey($className)) {
$this->listeners->put($className, new Set());
}

$this->listeners->get($className)->add($listener);
}
}
Loading

0 comments on commit 424a5cc

Please sign in to comment.