15
15
use Symfony \Component \VarExporter \LazyObjectInterface ;
16
16
use Symfony \Component \VarExporter \LazyProxyTrait ;
17
17
use Symfony \Component \VarExporter \ProxyHelper ;
18
+ use Zenstruck \Foundry \Factory ;
18
19
19
20
/**
20
21
* @author Kevin Bond <[email protected] >
21
22
*
22
23
* @internal
24
+ *
25
+ * @phpstan-import-type Attributes from Factory
23
26
*/
24
27
final class ProxyGenerator
25
28
{
@@ -43,6 +46,19 @@ public static function wrap(object $object): Proxy
43
46
return self ::generateClassFor ($ object )::createLazyProxy (static fn () => $ object ); // @phpstan-ignore-line
44
47
}
45
48
49
+ /**
50
+ * @template T of object
51
+ *
52
+ * @param PersistentProxyObjectFactory<T> $factory
53
+ * @phpstan-param Attributes $attributes
54
+ *
55
+ * @return T&Proxy<T>
56
+ */
57
+ public static function wrapFactory (PersistentProxyObjectFactory $ factory , callable |array $ attributes ): Proxy
58
+ {
59
+ return self ::generateClassFor ($ factory )::createLazyProxy (static fn () => $ factory ->create ($ attributes )); // @phpstan-ignore-line
60
+ }
61
+
46
62
/**
47
63
* @template T
48
64
*
@@ -76,8 +92,8 @@ public static function unwrap(mixed $what): mixed
76
92
*/
77
93
private static function generateClassFor (object $ object ): string
78
94
{
79
- /** @var class-string $class */
80
- $ class = $ object instanceof DoctrineProxy ? \get_parent_class ( $ object ) : $ object ::class;
95
+ $ class = self :: extractClassName ( $ object );
96
+
81
97
$ proxyClass = self ::proxyClassNameFor ($ class );
82
98
83
99
/** @var class-string<LazyObjectInterface&Proxy<T>&T> $proxyClass */
@@ -151,4 +167,16 @@ public static function proxyClassNameFor(string $class): string
151
167
{
152
168
return \str_replace ('\\' , '' , $ class ).'Proxy ' ;
153
169
}
170
+
171
+ /**
172
+ * @return class-string
173
+ */
174
+ private static function extractClassName (object $ object ): string
175
+ {
176
+ if ($ object instanceof PersistentProxyObjectFactory) {
177
+ return $ object ::class ();
178
+ }
179
+
180
+ return $ object instanceof DoctrineProxy ? \get_parent_class ($ object ) : $ object ::class; // @phpstan-ignore return.type
181
+ }
154
182
}
0 commit comments