forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistry.php
More file actions
41 lines (33 loc) · 1011 Bytes
/
Registry.php
File metadata and controls
41 lines (33 loc) · 1011 Bytes
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
36
37
38
39
40
41
<?php declare(strict_types=1);
namespace DesignPatterns\Structural\Registry;
abstract class Registry
{
const LOGGER = 'logger';
/**
* this introduces global state in your application which can not be mocked up for testing
* and is therefor considered an anti-pattern! Use dependency injection instead!
*
* @var Service[]
*/
private static $services = [];
/**
* @var array
*/
private static $allowedKeys = [
self::LOGGER,
];
public static function set(string $key, Service $value)
{
if (!in_array($key, self::$allowedKeys)) {
throw new \InvalidArgumentException('Invalid key given');
}
self::$services[$key] = $value;
}
public static function get(string $key): Service
{
if (!in_array($key, self::$allowedKeys) || !isset(self::$services[$key])) {
throw new \InvalidArgumentException('Invalid key given');
}
return self::$services[$key];
}
}