alecrabbit /
php-console-spinner
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace AlecRabbit\Spinner\Container; |
||
| 6 | |||
| 7 | use AlecRabbit\Spinner\Container\Contract\IDefinitionRegistry; |
||
| 8 | use AlecRabbit\Spinner\Container\Contract\IServiceDefinition; |
||
| 9 | use ArrayObject; |
||
| 10 | use Traversable; |
||
| 11 | |||
| 12 | final class DefinitionRegistry implements IDefinitionRegistry |
||
| 13 | { |
||
| 14 | private static ?IDefinitionRegistry $instance = null; |
||
| 15 | |||
| 16 | /** @var ArrayObject<string, IServiceDefinition> */ |
||
| 17 | private readonly ArrayObject $definitions; |
||
| 18 | |||
| 19 | private function __construct( |
||
| 20 | ArrayObject $definitions = new ArrayObject(), |
||
| 21 | ) { |
||
| 22 | /** @var ArrayObject<string, IServiceDefinition> $definitions */ |
||
| 23 | $this->definitions = $definitions; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 24 | } |
||
| 25 | |||
| 26 | public static function getInstance(): IDefinitionRegistry |
||
| 27 | { |
||
| 28 | if (self::$instance === null) { |
||
| 29 | self::$instance = new self(); |
||
| 30 | } |
||
| 31 | return self::$instance; |
||
|
0 ignored issues
–
show
|
|||
| 32 | } |
||
| 33 | |||
| 34 | public function load(): Traversable |
||
| 35 | { |
||
| 36 | yield from $this->definitions; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function bind(IServiceDefinition ...$serviceDefinitions): void |
||
| 40 | { |
||
| 41 | foreach ($serviceDefinitions as $serviceDefinition) { |
||
| 42 | $this->definitions->offsetSet($serviceDefinition->getId(), $serviceDefinition); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | } |
||
| 46 |