-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
262 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Attribute; | ||
|
||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
|
||
#[\Attribute(\Attribute::TARGET_METHOD)] | ||
final class AsFoundryHook extends AsEventListener | ||
{ | ||
public function __construct( | ||
/** @var class-string */ | ||
public readonly ?string $objectClass = null, | ||
int $priority = 0, | ||
) { | ||
parent::__construct(priority: $priority); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,25 @@ | |
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* | ||
* @template T of object | ||
* @implements Event<T> | ||
* | ||
* @phpstan-import-type Parameters from Factory | ||
*/ | ||
final class AfterInstantiate | ||
final class AfterInstantiate implements Event | ||
{ | ||
public function __construct( | ||
/** @var T */ | ||
public readonly object $object, | ||
/** @phpstan-var Parameters */ | ||
public readonly array $parameters, | ||
/** @var ObjectFactory<object> */ | ||
/** @var ObjectFactory<T> */ | ||
public readonly ObjectFactory $factory, | ||
) { | ||
} | ||
|
||
public function objectClassName(): string | ||
{ | ||
return $this->object::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,17 +19,25 @@ | |
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* | ||
* @template T of object | ||
* @implements Event<T> | ||
* | ||
* @phpstan-import-type Parameters from Factory | ||
*/ | ||
final class BeforeInstantiate | ||
final class BeforeInstantiate implements Event | ||
{ | ||
public function __construct( | ||
/** @phpstan-var Parameters */ | ||
public array $parameters, | ||
/** @var class-string */ | ||
/** @var class-string<T> */ | ||
public readonly string $objectClass, | ||
/** @var ObjectFactory<object> */ | ||
/** @var ObjectFactory<T> */ | ||
public readonly ObjectFactory $factory, | ||
) { | ||
} | ||
|
||
public function objectClassName(): string | ||
{ | ||
return $this->objectClass; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Object\Event; | ||
|
||
/** | ||
* @template T of object | ||
*/ | ||
interface Event | ||
{ | ||
/** | ||
* @return class-string<T> | ||
*/ | ||
public function objectClassName(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Object\Event; | ||
|
||
final class HookListenerFilter | ||
{ | ||
/** @var \Closure(Event<object>): void */ | ||
private \Closure $listener; | ||
|
||
/** | ||
* @param array{0: object, 1: string} $listener | ||
* @param class-string|null $objectClass | ||
*/ | ||
public function __construct(array $listener, private ?string $objectClass = null) | ||
{ | ||
if (!\is_callable($listener)) { | ||
throw new \InvalidArgumentException(\sprintf('Listener must be a callable, "%s" given.', \get_debug_type($listener))); | ||
} | ||
|
||
$this->listener = $listener(...); | ||
} | ||
|
||
/** | ||
* @param Event<object> $event | ||
*/ | ||
public function __invoke(Event $event): void | ||
{ | ||
if ($this->objectClass && $event->objectClassName() !== $this->objectClass) { | ||
return; | ||
} | ||
|
||
($this->listener)($event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,21 +14,31 @@ | |
namespace Zenstruck\Foundry\Persistence\Event; | ||
|
||
use Zenstruck\Foundry\Factory; | ||
use Zenstruck\Foundry\Object\Event\Event; | ||
use Zenstruck\Foundry\Persistence\PersistentObjectFactory; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* | ||
* @template T of object | ||
* @implements Event<T> | ||
* | ||
* @phpstan-import-type Parameters from Factory | ||
*/ | ||
final class AfterPersist | ||
final class AfterPersist implements Event | ||
{ | ||
public function __construct( | ||
/** @var T */ | ||
public readonly object $object, | ||
/** @phpstan-var Parameters */ | ||
public readonly array $parameters, | ||
/** @var PersistentObjectFactory<object> */ | ||
/** @var PersistentObjectFactory<T> */ | ||
public readonly PersistentObjectFactory $factory, | ||
) { | ||
} | ||
|
||
public function objectClassName(): string | ||
{ | ||
return $this->object::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.