forked from zenstruck/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
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
738 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* 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; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <nikophil@gmail.com> | ||
* | ||
* @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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* 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; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <nikophil@gmail.com> | ||
* | ||
* @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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Hooks; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <nikophil@gmail.com> | ||
*/ | ||
#[\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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Hooks; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <nikophil@gmail.com> | ||
*/ | ||
#[\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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Hooks; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <nikophil@gmail.com> | ||
*/ | ||
#[\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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* 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; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <nikophil@gmail.com> | ||
* | ||
* @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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Hooks; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <nikophil@gmail.com> | ||
* | ||
* @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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* 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; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <nikophil@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class HooksRegistry | ||
{ | ||
public function __construct( | ||
/** @var ServiceLocator<list<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 list<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.