Registry.php 778 B
Newer Older
<?php

namespace Hp\Patterns\Registry;

use InvalidArgumentException;
use Hp\Patterns\ServiceLocator\ServiceInterface;

abstract class Registry
{
    private static array $services = [];

    private static array $allowedKeys = [
        'RegisterService'
    ];

    public static function set(string $key, ServiceInterface $service): void
    {
        if (!in_array($key, self::$allowedKeys, true)) {
            throw new InvalidArgumentException();
        }

        self::$services[$key] = $service;
    }

    public static function get(string $key): ServiceInterface
    {
        if (!in_array($key, self::$allowedKeys, true) || !isset(self::$services[$key])) {
            throw new InvalidArgumentException();
        }

        return self::$services[$key];
    }
}