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