Skip to content

Commit ac92c76

Browse files
fix: make StateRegistry a real singleton
1 parent 97bad6e commit ac92c76

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/Concerns/StatefullyLoadsAttributes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ public function __call($method, $parameters)
156156
continue;
157157
}
158158

159-
$stateInstance = StateRegistry::tryFrom($state);
159+
$stateInstance = app(StateRegistry::class)->tryFrom($state);
160+
160161
if ($stateInstance === null) {
161162
continue;
162163
}

src/StateRegistry.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,29 @@
1010
*/
1111
class StateRegistry
1212
{
13-
private static array $stateClasses = [];
13+
/**
14+
* @var array<string, ResourceState>
15+
*/
16+
private array $stateClasses = [];
1417

1518
/**
1619
* Register a state enum class.
1720
*/
18-
public static function register(string $stateClass): void
21+
public function register(string $stateClass): void
1922
{
2023
if (! is_subclass_of($stateClass, ResourceState::class)) {
21-
throw new InvalidArgumentException("State class {$stateClass} must implement the ResourceState interface.");
24+
throw new InvalidArgumentException("State class {$stateClass} must be a valid ResourceState enum.");
2225
}
2326

24-
self::$stateClasses[] = $stateClass;
27+
$this->stateClasses[] = $stateClass;
2528
}
2629

2730
/**
2831
* Try to find a state by value across all registered state classes.
2932
*/
30-
public static function tryFrom(string $value): ?ResourceState
33+
public function tryFrom(string $value): ?ResourceState
3134
{
32-
foreach (self::$stateClasses as $stateClass) {
35+
foreach ($this->stateClasses as $stateClass) {
3336
$state = $stateClass::tryFrom($value);
3437
if ($state !== null) {
3538
return $state;
@@ -42,9 +45,9 @@ public static function tryFrom(string $value): ?ResourceState
4245
/**
4346
* Find a state by value across all registered state classes.
4447
*/
45-
public static function from(string $value): ResourceState
48+
public function from(string $value): ResourceState
4649
{
47-
$state = self::tryFrom($value);
50+
$state = $this->tryFrom($value);
4851

4952
if ($state === null) {
5053
throw new InvalidArgumentException("Unknown state: {$value}");
@@ -56,10 +59,10 @@ public static function from(string $value): ResourceState
5659
/**
5760
* Get all available states from all registered classes.
5861
*/
59-
public static function all(): array
62+
public function all(): array
6063
{
6164
$states = [];
62-
foreach (self::$stateClasses as $stateClass) {
65+
foreach ($this->stateClasses as $stateClass) {
6366
$states = array_merge($states, $stateClass::cases());
6467
}
6568

@@ -69,8 +72,8 @@ public static function all(): array
6972
/**
7073
* Clear all registered state classes.
7174
*/
72-
public static function clear(): void
75+
public function clear(): void
7376
{
74-
self::$stateClasses = [];
77+
$this->stateClasses = [];
7578
}
7679
}

src/StatefulJsonResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct($resource)
5757
*/
5858
public static function __callStatic($method, $parameters)
5959
{
60-
$state = StateRegistry::tryFrom($method);
60+
$state = app(StateRegistry::class)->tryFrom($method);
6161

6262
if ($state === null) {
6363
return parent::__callStatic($method, $parameters);

0 commit comments

Comments
 (0)