Passed
Push — feat/add-rector ( 29ca5e )
by Chema
04:27
created

Locator::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 2
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Container;
6
7
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal;
8
9
/**
10
 * @internal
11
 */
12
final class Locator implements LocatorInterface
13
{
14
    private static ?Locator $instance = null;
15
16
    /** @var array<string, mixed> */
17
    private array $instanceCache = [];
18
19
    private ContainerInterface $container;
20
21
    private function __construct(
22
        ?ContainerInterface $container = null,
23
    ) {
24
        $this->container = $container ?? new Container();
25
    }
26
27
    public static function resetInstance(): void
28
    {
29
        self::$instance = null;
30
    }
31
32
    /**
33
     * @template T
34
     *
35
     * @param class-string<T> $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
36
     * @param T $value
37
     */
38
    public static function addSingleton(string $key, mixed $value): void
39
    {
40
        self::getInstance()->add($key, $value);
41
    }
42
43
    /**
44
     * @template T
45
     *
46
     * @param class-string<T> $className
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
47
     *
48
     * @return T|null
49
     */
50
    public static function getSingleton(string $className, ?Container $container = null)
51
    {
52
        return self::getInstance($container)->get($className);
53
    }
54
55
    public static function getInstance(?Container $container = null): self
56
    {
57
        if (self::$instance === null) {
58
            self::$instance = new self($container);
59
        }
60
61
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return Gacela\Framework\Container\Locator. Consider adding an additional type-check to rule them out.
Loading history...
62
    }
63
64
    /**
65
     * @template T
66
     *
67
     * @param class-string<T> $className
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
68
     *
69
     * @return T|null
70
     */
71
    public function get(string $className)
72
    {
73
        if (isset($this->instanceCache[$className])) {
74
            /** @var T $instance */
75
            $instance = $this->instanceCache[$className];
76
77
            return $instance;
78
        }
79
80
        /** @var T|null $locatedInstance */
81
        $locatedInstance = AnonymousGlobal::getByClassName($className)
82
            ?? $this->container->get($className);
83
84
        $this->add($className, $locatedInstance);
85
86
        return $locatedInstance;
87
    }
88
89
    /**
90
     * @template T
91
     *
92
     * @param class-string<T> $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
93
     * @param T|null $value
94
     */
95
    private function add(string $key, mixed $value = null): void
96
    {
97
        $this->instanceCache[$key] = $value;
98
    }
99
}
100