Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a83c249

Browse files
authored
feat: enable auto-refresh at factory level (#970)
1 parent 32e9868 commit a83c249

3 files changed

Lines changed: 72 additions & 5 deletions

File tree

src/Persistence/PersistentObjectFactory.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/Persistence/functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ function refresh(object &$object): object
152152
*/
153153
function refresh_all(): void
154154
{
155-
$objectsTracker = Configuration::instance()->persistedObjectsTracker;
156-
157155
if (\PHP_VERSION_ID < 80400) {
158156
throw new \BadMethodCallException('Cannot use refresh_all() before PHP 8.4.');
159157
}
160158

161-
if (!Configuration::autoRefreshWithLazyObjectsIsEnabled() || null === $objectsTracker) {
159+
$objectsTracker = Configuration::instance()->persistedObjectsTracker;
160+
161+
if (null === $objectsTracker) {
162162
throw new \BadMethodCallException('Cannot use refresh_all() if auto refresh with lazy objects is not enabled.');
163163
}
164164

tests/Integration/Persistence/AutoRefreshTestCase.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,36 @@ public function it_can_refresh_lazy_object(): void
291291
self::assertSame('foo', $object->getProp1());
292292
}
293293

294+
#[Test]
295+
public function it_can_disable_autorefresh(): void
296+
{
297+
$object = $this->factory()->withoutAutorefresh()->create();
298+
$objectId = $object->id;
299+
300+
self::getContainer()->get('services_resetter')->reset(); // @phpstan-ignore method.notFound
301+
self::assertFalse((new \ReflectionClass($object))->isUninitializedLazyObject($object));
302+
303+
$this->updateObject($objectId);
304+
305+
self::assertSame('default1', $object->getProp1());
306+
}
307+
308+
#[Test]
309+
public function it_can_enable_autorefresh_when_disabled_globally(): void
310+
{
311+
self::bootKernel(['disable_auto_refresh' => true]);
312+
313+
$object = $this->factory()->withAutorefresh()->create();
314+
$objectId = $object->id;
315+
316+
self::getContainer()->get('services_resetter')->reset(); // @phpstan-ignore method.notFound
317+
self::assertTrue((new \ReflectionClass($object))->isUninitializedLazyObject($object));
318+
319+
$this->updateObject($objectId);
320+
321+
self::assertSame('foo', $object->getProp1());
322+
}
323+
294324
/**
295325
* @return PersistentObjectFactory<GenericModel>
296326
*/
@@ -304,6 +334,10 @@ abstract protected function objectManager(): ObjectManager;
304334

305335
protected static function createKernel(array $options = []): KernelInterface
306336
{
337+
if (true === ($options['disable_auto_refresh'] ?? false)) {
338+
return parent::createKernel($options);
339+
}
340+
307341
return new TestKernel('enable_auto_refresh_with_lazy_objects', debug: true);
308342
}
309343
}

0 commit comments

Comments
 (0)