-
-
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.
feature: allow to create objects in dataProvider thanks to lazy proxy
- Loading branch information
Showing
12 changed files
with
265 additions
and
144 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
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 |
---|---|---|
|
@@ -15,11 +15,14 @@ | |
use Symfony\Component\VarExporter\LazyObjectInterface; | ||
use Symfony\Component\VarExporter\LazyProxyTrait; | ||
use Symfony\Component\VarExporter\ProxyHelper; | ||
use Zenstruck\Foundry\Factory; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
* @internal | ||
* | ||
* @phpstan-import-type Attributes from Factory | ||
*/ | ||
final class ProxyGenerator | ||
{ | ||
|
@@ -43,6 +46,19 @@ public static function wrap(object $object): Proxy | |
return self::generateClassFor($object)::createLazyProxy(static fn() => $object); // @phpstan-ignore-line | ||
} | ||
|
||
/** | ||
* @template T of object | ||
* | ||
* @param PersistentProxyObjectFactory<T> $factory | ||
* @phpstan-param Attributes $attributes | ||
* | ||
* @return T&Proxy<T> | ||
*/ | ||
public static function wrapFactory(PersistentProxyObjectFactory $factory, callable|array $attributes): Proxy | ||
{ | ||
return self::generateClassFor($factory)::createLazyProxy(static fn() => $factory->create($attributes)); // @phpstan-ignore-line | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
|
@@ -76,8 +92,8 @@ public static function unwrap(mixed $what): mixed | |
*/ | ||
private static function generateClassFor(object $object): string | ||
{ | ||
/** @var class-string $class */ | ||
$class = $object instanceof DoctrineProxy ? \get_parent_class($object) : $object::class; | ||
$class = self::extractClassName($object); | ||
|
||
$proxyClass = self::proxyClassNameFor($class); | ||
|
||
/** @var class-string<LazyObjectInterface&Proxy<T>&T> $proxyClass */ | ||
|
@@ -151,4 +167,16 @@ public static function proxyClassNameFor(string $class): string | |
{ | ||
return \str_replace('\\', '', $class).'Proxy'; | ||
} | ||
|
||
/** | ||
* @return class-string | ||
*/ | ||
private static function extractClassName(object $object): string | ||
{ | ||
if ($object instanceof PersistentProxyObjectFactory) { | ||
return $object::class(); | ||
} | ||
|
||
return $object instanceof DoctrineProxy ? \get_parent_class($object) : $object::class; // @phpstan-ignore return.type | ||
} | ||
} |
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,7 +14,10 @@ | |
use PHPUnit\Framework\Attributes\After; | ||
use PHPUnit\Framework\Attributes\Before; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\VarExporter\LazyObjectInterface; | ||
use Webmozart\Assert\Assert; | ||
use Zenstruck\Foundry\Configuration; | ||
use Zenstruck\Foundry\Persistence\Proxy; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -26,7 +29,16 @@ trait Factories | |
* @before | ||
*/ | ||
#[Before] | ||
public static function _bootFoundry(): void | ||
public function _beforeHook(): void | ||
{ | ||
$this->_bootFoundry(); | ||
$this->_loadDataProvidedProxies(); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
private function _bootFoundry(): void | ||
{ | ||
if (!\is_subclass_of(static::class, KernelTestCase::class)) { // @phpstan-ignore-line | ||
// unit test | ||
|
@@ -46,6 +58,32 @@ public static function _bootFoundry(): void | |
}); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
private function _loadDataProvidedProxies(): void | ||
{ | ||
if (!\is_subclass_of(static::class, KernelTestCase::class)) { // @phpstan-ignore-line | ||
return; | ||
} | ||
|
||
$providedData = method_exists($this, 'getProvidedData') ? $this->getProvidedData() : $this->providedData(); // @phpstan-ignore method.notFound | ||
|
||
foreach ($providedData as $providedDatum) { | ||
if ($providedDatum instanceof Proxy) { | ||
$providedDatum->_initializeLazyObject(); | ||
} | ||
|
||
if (is_array($providedDatum) && count($providedDatum)) { | ||
foreach ($providedDatum as $itemFromCollection) { | ||
if ($itemFromCollection instanceof Proxy) { | ||
$itemFromCollection->_initializeLazyObject(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
* @after | ||
|
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
Oops, something went wrong.