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