1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Peridot\Leo; |
4
|
|
|
|
5
|
|
|
use Peridot\Leo\Formatter\Formatter; |
6
|
|
|
use Peridot\Leo\Formatter\FormatterInterface; |
7
|
|
|
use Peridot\Leo\Responder\ExceptionResponder; |
8
|
|
|
use Peridot\Leo\Responder\ResponderInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Leo. Singleton access to Leo and |
12
|
|
|
* all of its internals. A singleton is used as |
13
|
|
|
* opposed to static methods and properties because |
14
|
|
|
* of how php handles static closures. |
15
|
|
|
* |
16
|
|
|
* For instance: |
17
|
|
|
* |
18
|
|
|
* Leo::extend(callable $fn) will not allow binding |
19
|
|
|
* of variables inside $fn - i.e via the DynamicObjectTrait |
20
|
|
|
* |
21
|
|
|
* @package Peridot\Leo |
22
|
|
|
*/ |
23
|
|
|
final class Leo |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \Peridot\Leo\Leo |
27
|
|
|
*/ |
28
|
|
|
private static $instance; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var FormatterInterface |
32
|
|
|
*/ |
33
|
|
|
protected $formatter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ResponderInterface |
37
|
|
|
*/ |
38
|
|
|
protected $responder; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Assertion |
42
|
|
|
*/ |
43
|
|
|
protected $assertion; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Private access Constructor. |
47
|
|
|
*/ |
48
|
|
|
private function __construct() |
49
|
|
|
{ |
50
|
|
|
$this->formatter = new Formatter(); |
51
|
|
|
$this->responder = new ExceptionResponder($this->formatter); |
52
|
|
|
$this->assertion = new Assertion($this->responder); |
53
|
|
|
|
54
|
|
|
$this->assertion->extend(__DIR__ . '/Core/Definitions.php'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return the Leo Assertion. |
59
|
|
|
* |
60
|
|
|
* @return Assertion |
61
|
|
|
*/ |
62
|
|
|
public function getAssertion() |
63
|
|
|
{ |
64
|
|
|
return $this->assertion; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the Assertion used by Leo. |
69
|
|
|
* |
70
|
|
|
* @param $assertion |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function setAssertion($assertion) |
74
|
|
|
{ |
75
|
|
|
$this->assertion = $assertion; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return the FormatterInterface used by Leo. |
82
|
|
|
* |
83
|
|
|
* @return FormatterInterface |
84
|
|
|
*/ |
85
|
|
|
public function getFormatter() |
86
|
|
|
{ |
87
|
|
|
return $this->formatter; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the FormatterInterface used by Leo. |
92
|
|
|
* |
93
|
|
|
* @param FormatterInterface $formatter |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function setFormatter(FormatterInterface $formatter) |
97
|
|
|
{ |
98
|
|
|
$this->formatter = $formatter; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Return the ResponderInterface being used by Leo. |
105
|
|
|
* |
106
|
|
|
* @return ResponderInterface |
107
|
|
|
*/ |
108
|
|
|
public function getResponder() |
109
|
|
|
{ |
110
|
|
|
return $this->responder; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the ResponderInterface used by Leo. |
115
|
|
|
* |
116
|
|
|
* @param ResponderInterface $responder |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setResponder(ResponderInterface $responder) |
120
|
|
|
{ |
121
|
|
|
$this->responder = $responder; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Singleton access to Leo. A singleton is used instead of a facade as |
128
|
|
|
* PHP has some hangups about binding scope from static methods. This should |
129
|
|
|
* be used to access all Assertion members. |
130
|
|
|
* |
131
|
|
|
* @code |
132
|
|
|
* |
133
|
|
|
* $assertion = Leo::instance()->getAssertion(); |
134
|
|
|
* $assertion->extend(function($assertion)) { |
135
|
|
|
* $assertion->addMethod('coolAssertion', function($expected, $message = "") { |
136
|
|
|
* $this->flag('message', $message); |
137
|
|
|
* return new CoolMatcher($expected); |
138
|
|
|
* }); |
139
|
|
|
* }); |
140
|
|
|
* |
141
|
|
|
* @endcode |
142
|
|
|
* |
143
|
|
|
* @return Leo |
144
|
|
|
*/ |
145
|
|
|
public static function instance() |
146
|
|
|
{ |
147
|
|
|
if (!self::$instance) { |
148
|
|
|
self::$instance = new self(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return self::$instance; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Singleton access to Leo's assertion object. |
156
|
|
|
* |
157
|
|
|
* @return Assertion |
158
|
|
|
*/ |
159
|
|
|
public static function assertion() |
160
|
|
|
{ |
161
|
|
|
return self::instance()->getAssertion(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|