|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\ClassResolver\Factory\FactoryResolver; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @psalm-suppress MethodSignatureMismatch |
|
12
|
|
|
* |
|
13
|
|
|
* @method AbstractFactory getFactory() |
|
14
|
|
|
*/ |
|
15
|
|
|
trait FactoryResolverAwareTrait |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var array<string,AbstractFactory> */ |
|
18
|
|
|
private static array $factories = []; |
|
19
|
|
|
|
|
20
|
3 |
|
public static function __callStatic(string $name = '', array $arguments = []) |
|
21
|
|
|
{ |
|
22
|
3 |
|
if ($name === 'getFactory') { |
|
23
|
2 |
|
return self::doGetFactory(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
if (method_exists(static::class, $name)) { |
|
27
|
|
|
/** @psalm-suppress ParentNotFound */ |
|
28
|
|
|
return parent::__callStatic($name, $arguments); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
throw new RuntimeException("Method unknown: '{$name}'"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
56 |
|
public function __call(string $name = '', array $arguments = []) |
|
35
|
|
|
{ |
|
36
|
56 |
|
if ($name === 'getFactory') { |
|
37
|
56 |
|
return self::doGetFactory(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (method_exists(static::class, $name)) { |
|
41
|
|
|
/** @psalm-suppress ParentNotFound */ |
|
42
|
|
|
return parent::__call($name, $arguments); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
throw new RuntimeException("Method unknown: '{$name}'"); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
45 |
|
public static function resetCache(): void |
|
49
|
|
|
{ |
|
50
|
45 |
|
self::$factories = []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
58 |
|
private static function doGetFactory(): AbstractFactory |
|
54
|
|
|
{ |
|
55
|
58 |
|
return self::$factories[static::class] |
|
56
|
58 |
|
??= (new FactoryResolver())->resolve(static::class); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|