@@ -51,6 +51,15 @@ abstract class PersistentObjectFactory extends ObjectFactory
5151
5252 private bool $ isRootFactory = true ;
5353
54+ private bool $ autorefreshEnabled = false ;
55+
56+ public function __construct ()
57+ {
58+ parent ::__construct ();
59+
60+ $ this ->autorefreshEnabled = Configuration::autoRefreshWithLazyObjectsIsEnabled ();
61+ }
62+
5463 /**
5564 * @phpstan-param mixed|Parameters $criteriaOrId
5665 *
@@ -232,7 +241,7 @@ public function create(callable|array $attributes = []): object
232241 $ configuration = Configuration::instance ();
233242
234243 if ($ configuration ->inADataProvider ()
235- && Configuration:: autoRefreshWithLazyObjectsIsEnabled ()
244+ && \ PHP_VERSION_ID >= 80400
236245 && $ this ->isPersisting ()
237246 && !$ this instanceof PersistentProxyObjectFactory
238247 ) {
@@ -282,6 +291,30 @@ final public function withoutPersisting(): static
282291 return $ clone ;
283292 }
284293
294+ final public function withAutorefresh (): static
295+ {
296+ if (\PHP_VERSION_ID < 80400 ) {
297+ throw new \LogicException ('Auto-refresh requires PHP 8.4 or higher. ' );
298+ }
299+
300+ $ clone = clone $ this ;
301+ $ clone ->autorefreshEnabled = true ;
302+
303+ return $ clone ;
304+ }
305+
306+ final public function withoutAutorefresh (): static
307+ {
308+ if (\PHP_VERSION_ID < 80400 ) {
309+ throw new \LogicException ('Auto-refresh requires PHP 8.4 or higher. ' );
310+ }
311+
312+ $ clone = clone $ this ;
313+ $ clone ->autorefreshEnabled = false ;
314+
315+ return $ clone ;
316+ }
317+
285318 /**
286319 * @internal
287320 */
@@ -492,7 +525,7 @@ static function(object $object, array $parameters, PersistentObjectFactory $fact
492525 }
493526
494527 if (
495- Configuration:: autoRefreshWithLazyObjectsIsEnabled ()
528+ $ factoryUsed -> autorefreshEnabled
496529 && !$ factoryUsed instanceof PersistentProxyObjectFactory
497530 ) {
498531 Configuration::instance ()->persistedObjectsTracker ?->add($ object );
0 commit comments