1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17: 18: 19: 20: 21: 22: 23: 24:
25: final class Callback extends Object
26: {
27:
28: private $cb;
29:
30:
31: 32: 33: 34: 35: 36:
37: public static function create($callback, $m = NULL)
38: {
39: return new self($callback, $m);
40: }
41:
42:
43: 44: 45: 46:
47: public function __construct($cb, $m = NULL)
48: {
49: if ($m !== NULL) {
50: $cb = array($cb, $m);
51:
52: } elseif ($cb instanceof self) {
53: $this->cb = $cb->cb;
54: return;
55: }
56:
57: if (!is_callable($cb, TRUE)) {
58: throw new InvalidArgumentException("Invalid callback.");
59: }
60: $this->cb = $cb;
61: }
62:
63:
64: 65: 66: 67:
68: public function __invoke()
69: {
70: if (!is_callable($this->cb)) {
71: throw new InvalidStateException("Callback '$this' is not callable.");
72: }
73: return call_user_func_array($this->cb, func_get_args());
74: }
75:
76:
77: 78: 79: 80:
81: public function invoke()
82: {
83: if (!is_callable($this->cb)) {
84: throw new InvalidStateException("Callback '$this' is not callable.");
85: }
86: return call_user_func_array($this->cb, func_get_args());
87: }
88:
89:
90: 91: 92: 93: 94:
95: public function invokeArgs(array $args)
96: {
97: if (!is_callable($this->cb)) {
98: throw new InvalidStateException("Callback '$this' is not callable.");
99: }
100: return call_user_func_array($this->cb, $args);
101: }
102:
103:
104: 105: 106: 107:
108: public function isCallable()
109: {
110: return is_callable($this->cb);
111: }
112:
113:
114: 115: 116: 117:
118: public function getNative()
119: {
120: return $this->cb;
121: }
122:
123:
124: 125: 126: 127:
128: public function toReflection()
129: {
130: if (is_string($this->cb) && strpos($this->cb, '::')) {
131: return new Nette\Reflection\Method($this->cb);
132: } elseif (is_array($this->cb)) {
133: return new Nette\Reflection\Method($this->cb[0], $this->cb[1]);
134: } elseif (is_object($this->cb) && !$this->cb instanceof \Closure) {
135: return new Nette\Reflection\Method($this->cb, '__invoke');
136: } else {
137: return new Nette\Reflection\GlobalFunction($this->cb);
138: }
139: }
140:
141:
142: 143: 144:
145: public function isStatic()
146: {
147: return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
148: }
149:
150:
151: 152: 153:
154: public function __toString()
155: {
156: if ($this->cb instanceof \Closure) {
157: return '{closure}';
158: } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
159: return '{lambda}';
160: } else {
161: is_callable($this->cb, TRUE, $textual);
162: return $textual;
163: }
164: }
165:
166: }
167: