aboutsummaryrefslogtreecommitdiff
path: root/lib/Container.php
blob: 086bd1f6ce78a6c9657a138fe8ea1fffc8df73eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php

declare(strict_types=1);

class Container implements \ArrayAccess
{
    private array $values = [];
    private array $resolved = [];

    public function offsetSet($offset, $value): void
    {
        $this->values[$offset] = $value;
    }

    #[\ReturnTypeWillChange]
    public function offsetGet($offset)
    {
        if (!isset($this->values[$offset])) {
            throw new \Exception(sprintf('Unknown container key: "%s"', $offset));
        }
        if (!isset($this->resolved[$offset])) {
            $this->resolved[$offset] = $this->values[$offset]($this);
        }
        return $this->resolved[$offset];
    }

    #[\ReturnTypeWillChange]
    public function offsetExists($offset)
    {
    }

    public function offsetUnset($offset): void
    {
    }
}