1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Underscore; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Callable registry for Underscore methods. |
7
|
|
|
* |
8
|
|
|
* It is assumed that all callables will be classes with an `__invoke` method. |
9
|
|
|
* |
10
|
|
|
* Existing methods can be overloaded by setting an alias of the same name: |
11
|
|
|
* |
12
|
|
|
* $registry->alias('keys', 'Acme\KeysMutator'); |
13
|
|
|
* |
14
|
|
|
* New methods can be added in the same way: |
15
|
|
|
* |
16
|
|
|
* $registry->alias('fancy', 'Acme\FancyInitializer'); |
17
|
|
|
* |
18
|
|
|
* Every alias is a singleton and will only be created once! If your callable |
19
|
|
|
* requires construction, alias a fully constructed object instead: |
20
|
|
|
* |
21
|
|
|
* $registry->alias('fancy', new FancyInitializer($parameters)); |
22
|
|
|
* |
23
|
|
|
* NOTE: Mutators, accessors, and initializers all use the same registery and |
24
|
|
|
* differ only in how they are used internally by Underscore. |
25
|
|
|
*/ |
26
|
|
|
class Registry |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array Classes aliased by name of method. |
30
|
|
|
*/ |
31
|
|
|
private $aliases = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array Callable instances aliased by name of method. |
35
|
|
|
*/ |
36
|
|
|
private $instances = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $aliases |
40
|
|
|
*/ |
41
|
7 |
|
public function __construct(array $aliases = []) |
42
|
|
|
{ |
43
|
7 |
|
$this->aliases = $aliases; |
44
|
7 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the callable for an Underscore method alias. |
48
|
|
|
* |
49
|
|
|
* @param string $name |
50
|
|
|
* @return callable |
51
|
|
|
*/ |
52
|
58 |
|
public function instance($name) |
53
|
|
|
{ |
54
|
58 |
|
if (empty($this->instances[$name])) { |
55
|
49 |
|
if (empty($this->aliases[$name])) { |
56
|
48 |
|
$this->aliasDefault($name); |
57
|
|
|
} |
58
|
47 |
|
$this->instances[$name] = new $this->aliases[$name]; |
59
|
|
|
} |
60
|
|
|
|
61
|
57 |
|
return $this->instances[$name]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Alias method name to a callable. |
66
|
|
|
* |
67
|
|
|
* @param string $name |
68
|
|
|
* @param callable $spec |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
3 |
|
public function alias($name, $spec) |
72
|
|
|
{ |
73
|
3 |
|
if (is_callable($spec)) { |
74
|
2 |
|
$this->instances[$name] = $spec; |
75
|
|
|
} else { |
76
|
1 |
|
$this->aliases[$name] = $spec; |
77
|
|
|
|
78
|
|
|
// Ensure that the new alias will be used when called. |
79
|
1 |
|
unset($this->instances[$name]); |
80
|
|
|
} |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Define a default mutator, accessor, or initializer for a method name. |
85
|
|
|
* |
86
|
|
|
* @throws \BadMethodCallException If no default can be located. |
87
|
|
|
* @param string $name |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
48 |
|
private function aliasDefault($name) |
91
|
|
|
{ |
92
|
48 |
|
$spec = sprintf('\Underscore\Mutator\%sMutator', ucfirst($name)); |
93
|
|
|
|
94
|
48 |
|
if (!class_exists($spec)) { |
95
|
19 |
|
$spec = sprintf('\Underscore\Accessor\%sAccessor', ucfirst($name)); |
96
|
|
|
} |
97
|
|
|
|
98
|
48 |
|
if (!class_exists($spec)) { |
99
|
6 |
|
$spec = sprintf('\Underscore\Initializer\%sInitializer', ucfirst($name)); |
100
|
|
|
} |
101
|
|
|
|
102
|
48 |
|
if (!class_exists($spec)) { |
103
|
2 |
|
throw new \BadMethodCallException(sprintf('Unknown method Underscore->%s()', $name)); |
104
|
|
|
} |
105
|
|
|
|
106
|
46 |
|
$this->aliases[$name] = $spec; |
107
|
46 |
|
} |
108
|
|
|
} |
109
|
|
|
|