1 | <?php |
||
10 | abstract class ClassDiscovery |
||
11 | { |
||
12 | /** |
||
13 | * Add a class (and condition) to the discovery registry. |
||
14 | * |
||
15 | * @param string $class Class that will be instantiated if found |
||
16 | * @param string $condition Optional other class to check for existence |
||
17 | */ |
||
18 | 6 | public static function register($class, $condition = null) |
|
29 | |||
30 | /** |
||
31 | * Finds a Class. |
||
32 | * |
||
33 | * @return object |
||
34 | * |
||
35 | * @throws NotFoundException |
||
36 | */ |
||
37 | 13 | public static function find() |
|
38 | { |
||
39 | // We have a cache |
||
40 | 13 | if (isset(static::$cache)) { |
|
41 | 1 | return new static::$cache(); |
|
42 | } |
||
43 | |||
44 | 13 | foreach (static::$classes as $name => $definition) { |
|
45 | 12 | if (static::evaluateCondition($definition['condition'])) { |
|
46 | 12 | static::$cache = $definition['class']; |
|
47 | |||
48 | 12 | return new $definition['class'](); |
|
49 | } |
||
50 | 5 | } |
|
51 | |||
52 | 1 | throw new NotFoundException('Not found'); |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Evaulates conditions to boolean. |
||
57 | * |
||
58 | * @param mixed $condition |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | 12 | protected static function evaluateCondition($condition) |
|
84 | } |
||
85 |