-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
23 changed files
with
703 additions
and
9 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
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,40 @@ | ||
<?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\Hooks; | ||
|
||
use Zenstruck\Foundry\Factory; | ||
use Zenstruck\Foundry\ObjectFactory; | ||
|
||
/** | ||
* @phpstan-import-type Parameters from Factory | ||
* @template T of object | ||
* @implements HookEvent<T> | ||
*/ | ||
final class AfterInstantiate implements HookEvent | ||
{ | ||
public function __construct( | ||
/** @var T */ | ||
public readonly object $object, | ||
/** @phpstan-var Parameters */ | ||
public readonly array $parameters, | ||
/** @var ObjectFactory<T> */ | ||
public readonly ObjectFactory $factory, | ||
) { | ||
} | ||
|
||
public function getObjectClass(): 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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\Hooks; | ||
|
||
use Zenstruck\Foundry\Factory; | ||
use Zenstruck\Foundry\Persistence\PersistentObjectFactory; | ||
|
||
/** | ||
* @phpstan-import-type Parameters from Factory | ||
* @template T of object | ||
* @implements HookEvent<T> | ||
*/ | ||
final class AfterPersist implements HookEvent | ||
{ | ||
public function __construct( | ||
/** @var T */ | ||
public readonly object $object, | ||
/** @phpstan-var Parameters */ | ||
public readonly array $parameters, | ||
/** @var PersistentObjectFactory<T> */ | ||
public readonly PersistentObjectFactory $factory, | ||
) { | ||
} | ||
|
||
public function getObjectClass(): 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?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\Hooks; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class AsAfterInstantiateFoundryHook | ||
{ | ||
public function __construct( | ||
public ?string $class = null, | ||
) { | ||
} | ||
} |
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,23 @@ | ||
<?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\Hooks; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class AsAfterPersistFoundryHook | ||
{ | ||
public function __construct( | ||
public ?string $class = null, | ||
) { | ||
} | ||
} |
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,23 @@ | ||
<?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\Hooks; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class AsBeforeInstantiateFoundryHook | ||
{ | ||
public function __construct( | ||
public ?string $class = null, | ||
) { | ||
} | ||
} |
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,40 @@ | ||
<?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\Hooks; | ||
|
||
use Zenstruck\Foundry\Factory; | ||
use Zenstruck\Foundry\ObjectFactory; | ||
|
||
/** | ||
* @phpstan-import-type Parameters from Factory | ||
* @template T of object | ||
* @implements HookEvent<T> | ||
*/ | ||
final class BeforeInstantiate implements HookEvent | ||
{ | ||
public function __construct( | ||
/** @phpstan-var Parameters */ | ||
public array $parameters, | ||
/** @var class-string<T> */ | ||
public readonly string $objectClass, | ||
/** @var ObjectFactory<T> */ | ||
public readonly ObjectFactory $factory, | ||
) { | ||
} | ||
|
||
public function getObjectClass(): 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\Hooks; | ||
|
||
/** | ||
* todo: check if this is possible. | ||
* @internal | ||
* @template T of object | ||
*/ | ||
interface HookEvent | ||
{ | ||
/** @return class-string<T> */ | ||
public function getObjectClass(): 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,57 @@ | ||
<?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\Hooks; | ||
|
||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class HooksRegistry | ||
{ | ||
public function __construct( | ||
/** @var ServiceLocator<array<string, callable(HookEvent<object>): void>> */ | ||
private ServiceLocator $serviceLocator, | ||
) { | ||
} | ||
|
||
/** | ||
* @param HookEvent<object> $hookEvent | ||
*/ | ||
public function callHooks(HookEvent $hookEvent): void | ||
{ | ||
foreach ($this->resolveHooks($hookEvent) as $hook) { | ||
($hook)($hookEvent); | ||
} | ||
} | ||
|
||
public static function hookClassSpecificIndex(string $hookEventClass, string $objectClass): string | ||
{ | ||
return "{$hookEventClass}-{$objectClass}"; | ||
} | ||
|
||
/** | ||
* @param HookEvent<object> $hookEvent | ||
* @return (callable(HookEvent<object>): void)[] | ||
*/ | ||
private function resolveHooks(HookEvent $hookEvent): array | ||
{ | ||
$objectSpecificIndex = self::hookClassSpecificIndex($hookEvent::class, $hookEvent->getObjectClass()); | ||
|
||
return [ | ||
...$this->serviceLocator->has($hookEvent::class) ? $this->serviceLocator->get($hookEvent::class) : [], | ||
...$this->serviceLocator->has($objectSpecificIndex) ? $this->serviceLocator->get($objectSpecificIndex) : [], | ||
]; | ||
} | ||
} |
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.