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

Skip to content

Commit 3a131ef

Browse files
committed
minor: improve deprecation message
1 parent 30df79d commit 3a131ef

5 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/Configuration.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,22 @@ public static function autoRefreshWithLazyObjectsIsEnabled(): bool
171171
return self::isBooted() && self::instance()->enableAutoRefreshWithLazyObjects;
172172
}
173173

174-
public static function triggerProxyDeprecation(): void
174+
public static function triggerProxyDeprecation(?string $additionalMessage = null): void
175175
{
176176
if (\PHP_VERSION_ID < 80400) {
177177
return;
178178
}
179179

180-
trigger_deprecation(
181-
'zenstruck/foundry',
182-
'2.7',
183-
<<<DEPRECATION
184-
Proxy usage is deprecated in PHP 8.4. You should extend directly PersistentObjectFactory in your factories.
185-
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).
186-
See https://github.com/zenstruck/foundry/blob/1.x/UPGRADE-2.7.md to upgrade.
187-
DEPRECATION,
188-
);
180+
$message = <<<DEPRECATION
181+
Proxy usage is deprecated in PHP 8.4. You should extend directly PersistentObjectFactory in your factories.
182+
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).
183+
See https://github.com/zenstruck/foundry/blob/1.x/UPGRADE-2.7.md to upgrade.
184+
DEPRECATION;
185+
186+
if ($additionalMessage) {
187+
$message = "{$additionalMessage}\n{$message}";
188+
}
189+
190+
trigger_deprecation('zenstruck/foundry', '2.7', $message);
189191
}
190192
}

src/Persistence/PersistenceManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ public function refresh(object &$object, bool $force = false, bool $allowRefresh
211211

212212
public function isPersisted(object $object): bool
213213
{
214+
if ($object instanceof Proxy) {
215+
$object = $object->_real(withAutoRefresh: false);
216+
}
217+
214218
if (
215219
\PHP_VERSION_ID >= 80400
216220
&& ($reflector = new \ReflectionClass($object))->isUninitializedLazyObject($object)

src/Persistence/PersistentProxyObjectFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct()
2626
{
2727
parent::__construct();
2828

29-
Configuration::triggerProxyDeprecation();
29+
Configuration::triggerProxyDeprecation('Class PersistentProxyObjectFactory is deprecated and will be removed in Foundry 3.');
3030
}
3131

3232
/**

src/Persistence/ProxyRepositoryDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class ProxyRepositoryDecorator extends RepositoryDecorator
3030
*/
3131
public function __construct(string $class, bool $inMemory = false)
3232
{
33-
Configuration::triggerProxyDeprecation();
33+
Configuration::triggerProxyDeprecation('Class ProxyRepositoryDecorator is deprecated and will be removed in Foundry 3.');
3434

3535
parent::__construct($class, $inMemory); // @phpstan-ignore argument.type
3636
}

src/Persistence/functions.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ function repository(string $class): RepositoryDecorator
3737
*/
3838
function proxy_repository(string $class): ProxyRepositoryDecorator
3939
{
40+
Configuration::triggerProxyDeprecation('Function proxy_repository() is deprecated and will be removed in Foundry 3.');
41+
4042
return new ProxyRepositoryDecorator($class, Configuration::instance()->isInMemoryEnabled()); // @phpstan-ignore return.type
4143
}
4244

@@ -67,6 +69,8 @@ function persistent_factory(string $class, array|callable $attributes = []): Per
6769
*/
6870
function proxy_factory(string $class, array|callable $attributes = []): PersistentProxyObjectFactory
6971
{
72+
Configuration::triggerProxyDeprecation('Function proxy_factory() is deprecated and will be removed in Foundry 3.');
73+
7074
return AnonymousFactoryGenerator::create($class, PersistentProxyObjectFactory::class)::new($attributes);
7175
}
7276

@@ -96,7 +100,7 @@ function persist(string $class, array|callable $attributes = []): object
96100
*/
97101
function proxy(object $object): object
98102
{
99-
Configuration::triggerProxyDeprecation();
103+
Configuration::triggerProxyDeprecation('Function proxy() is deprecated and will be removed in Foundry 3.');
100104

101105
return ProxyGenerator::wrap($object);
102106
}
@@ -112,7 +116,7 @@ function proxy(object $object): object
112116
*/
113117
function unproxy(mixed $what, bool $withAutoRefresh = true): mixed
114118
{
115-
Configuration::triggerProxyDeprecation();
119+
Configuration::triggerProxyDeprecation('Function unproxy() is deprecated and will be removed in Foundry 3.');
116120

117121
return ProxyGenerator::unwrap($what, $withAutoRefresh);
118122
}

0 commit comments

Comments
 (0)