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

Skip to content

Commit fe6374b

Browse files
committed
feat: auto refresh with lazy object php84 enabled by config (#950)
bot: fix cs [skip ci]
1 parent 9717676 commit fe6374b

19 files changed

Lines changed: 156 additions & 19 deletions

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ MONGO_URL="mongodb://127.0.0.1:27018/dbName?compressors=disabled&gssapiServi
99

1010
USE_DAMA_DOCTRINE_TEST_BUNDLE="0"
1111
USE_FOUNDRY_PHPUNIT_EXTENSION="0"
12+
USE_PHP_84_LAZY_OBJECTS="0"
1213
PHPUNIT_VERSION="12" # allowed values: 9, 10, 11, 12

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
symfony: [ 6.4.*, 7.3.*, 8.0.*-dev ]
1818
database: [ mysql|mongo ]
1919
phpunit: [ 11, 12 ]
20+
use-php-84-lazy-objects: [ 1 ]
2021

2122
# default values:
2223
# deps: [ highest ]
@@ -55,11 +56,15 @@ jobs:
5556

5657
# using Foundry's PHPUnit extension
5758
- {php: 8.4, symfony: '*', phpunit: 12, database: mysql|mongo, use-phpunit-extension: 1}
59+
60+
# disable lazy objects in PHP 8.4
61+
- {php: 8.4, symfony: '*', phpunit: 12, database: mysql|mongo, use-php-84-lazy-objects: 0}
5862
env:
5963
DATABASE_URL: ${{ contains(matrix.database, 'mysql') && 'mysql://root:root@localhost:3306/foundry?serverVersion=5.7.42' || contains(matrix.database, 'pgsql') && 'postgresql://root:root@localhost:5432/foundry?serverVersion=15' || contains(matrix.database, 'sqlite') && 'sqlite:///%kernel.project_dir%/var/data.db' || '' }}
6064
MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
6165
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ contains(matrix.database, 'sql') && 1 || 0 }}
6266
USE_FOUNDRY_PHPUNIT_EXTENSION: ${{ matrix.use-phpunit-extension || 0 }}
67+
USE_PHP_84_LAZY_OBJECTS: ${{ matrix.use-php-84-lazy-objects }}
6368
PHPUNIT_VERSION: ${{ matrix.phpunit }}
6469
services:
6570
postgres:

config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
'%env(default:zenstruck_foundry.faker.seed:int:FOUNDRY_FAKER_SEED)%',
3737
service('.zenstruck_foundry.in_memory.repository_registry'),
3838
service('.foundry.persistence.objects_tracker')->nullOnInvalid(),
39+
param('zenstruck_foundry.enable_auto_refresh_with_lazy_objects'),
3940
])
4041
->public()
4142
;

phpunit-deprecation-baseline.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<line number="25" hash="c6af5d66288d0667e424978000f29571e4954b81">
55
<issue><![CDATA[Since symfony/framework-bundle 6.4: Not setting the "framework.php_errors.log" config option is deprecated. It will default to "true" in 7.0.]]></issue>
66
<issue><![CDATA[Since symfony/var-exporter 7.3: Generating lazy proxy for class "Zenstruck\Foundry\Tests\Integration\ForceFactoriesTraitUsage\SomeObject" is deprecated; leverage native lazy objects instead.]]></issue>
7-
<issue><![CDATA[Since zenstruck/foundry 2.6: Proxy usage is deprecated in PHP 8.4. Use directly PersistentObjectFactory, Foundry now leverages the native PHP lazy system to auto-refresh objects.]]></issue>
7+
<issue><![CDATA[Since zenstruck/foundry 2.7: Proxy usage is deprecated in PHP 8.4. Use directly PersistentObjectFactory, Foundry now leverages the native PHP lazy system to auto-refresh objects.]]></issue>
8+
<issue><![CDATA[Since zenstruck/foundry 2.7: Not setting a value for "zenstruck_foundry.enable_auto_refresh_with_lazy_objects" is deprecated. This option will be forced to true in 3.0.]]></issue>
89
</line>
910
</file>
1011
</files>

src/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function __construct(
6262
?int $forcedFakerSeed = null,
6363
public readonly ?InMemoryRepositoryRegistry $inMemoryRepositoryRegistry = null,
6464
public readonly ?PersistedObjectsTracker $persistedObjectsTracker = null,
65+
private readonly bool $enableAutoRefreshWithLazyObjects = false,
6566
) {
6667
if (null === self::$instance) {
6768
$this->faker->seed(self::fakerSeed($forcedFakerSeed));
@@ -164,4 +165,9 @@ public function isInMemoryEnabled(): bool
164165
{
165166
return $this->inMemory;
166167
}
168+
169+
public static function autoRefreshWithLazyObjectsIsEnabled(): bool
170+
{
171+
return self::isBooted() && self::instance()->enableAutoRefreshWithLazyObjects;
172+
}
167173
}

src/Persistence/PersistenceManager.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ public function refresh(object &$object, bool $force = false, bool $allowRefresh
157157
return $object->_refresh();
158158
}
159159

160-
if (\PHP_VERSION_ID >= 80400 && ($reflector = new \ReflectionClass($object))->isUninitializedLazyObject($object)) {
160+
if (
161+
\PHP_VERSION_ID >= 80400
162+
&& ($reflector = new \ReflectionClass($object))->isUninitializedLazyObject($object)
163+
) {
161164
/** @var T $object */
162165
$object = $reflector->initializeLazyObject($object);
163166
}
@@ -208,7 +211,10 @@ public function refresh(object &$object, bool $force = false, bool $allowRefresh
208211

209212
public function isPersisted(object $object): bool
210213
{
211-
if (\PHP_VERSION_ID >= 80400 && ($reflector = new \ReflectionClass($object))->isUninitializedLazyObject($object)) {
214+
if (
215+
\PHP_VERSION_ID >= 80400
216+
&& ($reflector = new \ReflectionClass($object))->isUninitializedLazyObject($object)
217+
) {
212218
/** @var object $object */
213219
$object = $reflector->initializeLazyObject($object);
214220
}

src/Persistence/PersistentObjectFactory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ public function create(callable|array $attributes = []): object
231231
{
232232
$configuration = Configuration::instance();
233233

234-
if ($configuration->inADataProvider() && \PHP_VERSION_ID >= 80400 && !$this instanceof PersistentProxyObjectFactory) {
234+
if ($configuration->inADataProvider()
235+
&& Configuration::autoRefreshWithLazyObjectsIsEnabled()
236+
&& !$this instanceof PersistentProxyObjectFactory
237+
) {
235238
return ProxyGenerator::wrapFactoryNativeProxy($this, $attributes);
236239
}
237240

@@ -487,7 +490,10 @@ static function(object $object, array $parameters, PersistentObjectFactory $fact
487490
return;
488491
}
489492

490-
if (\PHP_VERSION_ID >= 80400 && !$factoryUsed instanceof PersistentProxyObjectFactory) {
493+
if (
494+
Configuration::autoRefreshWithLazyObjectsIsEnabled()
495+
&& !$factoryUsed instanceof PersistentProxyObjectFactory
496+
) {
491497
Configuration::instance()->persistedObjectsTracker?->add($object);
492498
}
493499

src/Persistence/PersistentProxyObjectFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function __construct()
2929
if (\PHP_VERSION_ID >= 80400) {
3030
trigger_deprecation(
3131
'zenstruck/foundry',
32-
'2.6',
33-
'Proxy usage is deprecated in PHP 8.4. Use directly PersistentObjectFactory, Foundry now leverages the native PHP lazy system to auto-refresh objects.',
32+
'2.7',
33+
'Proxy usage is deprecated in PHP 8.4. Use directly PersistentObjectFactory, Foundry now leverages the native PHP lazy system to auto-refresh objects (it can be enabled with "zenstruck_foundry.enable_auto_refresh_with_lazy_objects" configuration).',
3434
);
3535
}
3636
}

src/Persistence/ProxyGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ public static function unwrap(mixed $what, bool $withAutoRefresh = true): mixed
106106
return $what->_real($withAutoRefresh); // @phpstan-ignore return.type
107107
}
108108

109-
if (\PHP_VERSION_ID >= 80400 && \is_object($what) && ($reflector = new \ReflectionClass($what))->isUninitializedLazyObject($what)) {
109+
if (
110+
\PHP_VERSION_ID >= 80400
111+
&& \is_object($what)
112+
&& ($reflector = new \ReflectionClass($what))->isUninitializedLazyObject($what)
113+
) {
110114
return $reflector->initializeLazyObject($what);
111115
}
112116

src/Persistence/ProxyRepositoryDecorator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function __construct(string $class, bool $inMemory = false)
3232
if (\PHP_VERSION_ID >= 80400) {
3333
trigger_deprecation(
3434
'zenstruck/foundry',
35-
'2.6',
36-
'Proxy usage is deprecated in PHP 8.4. Use directly PersistentObjectFactory, Foundry now leverages the native PHP lazy system to auto-refresh objects.',
35+
'2.7',
36+
'Proxy usage is deprecated in PHP 8.4. Use directly PersistentObjectFactory, Foundry now leverages the native PHP lazy system to auto-refresh objects (it can be enabled with "zenstruck_foundry.enable_auto_refresh_with_lazy_objects" configuration).',
3737
);
3838
}
3939

0 commit comments

Comments
 (0)