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