Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

Interfaces

Exceptions

  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  * @package Nette
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * PHP callback encapsulation.
 13:  *
 14:  * @author     David Grudl
 15:  * @property-read bool $callable
 16:  * @property-read string|array|Closure $native
 17:  * @property-read bool $static
 18:  * @package Nette
 19:  */
 20: class Callback extends Object
 21: {
 22:     /** @var callable */
 23:     private $cb;
 24: 
 25: 
 26:     /**
 27:      * Factory. Workaround for missing (new Callback)->invoke() in PHP 5.3.
 28:      * @param  mixed   class, object, callable
 29:      * @param  string  method
 30:      * @return Callback
 31:      */
 32:     public static function create($callback, $m = NULL)
 33:     {
 34:         return new self($callback, $m);
 35:     }
 36: 
 37: 
 38:     /**
 39:      * @param  mixed   class, object, callable
 40:      * @param  string  method
 41:      */
 42:     public function __construct($cb, $m = NULL)
 43:     {
 44:         if ($m !== NULL) {
 45:             $cb = array($cb, $m);
 46: 
 47:         } elseif ($cb instanceof self) { // prevents wrapping itself
 48:             $this->cb = $cb->cb;
 49:             return;
 50:         }
 51: 
 52:         if (PHP_VERSION_ID < 50202 && is_string($cb) && strpos($cb, '::')) {
 53:             $cb = explode('::', $cb, 2);
 54:         } elseif (is_object($cb) && !$cb instanceof Closure) {
 55:             $cb = array($cb, '__invoke');
 56:         }
 57: 
 58:         // remove class namespace
 59:         if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
 60:             $this->cb = substr($this->cb, $a + 1);
 61: 
 62:         } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
 63:             $this->cb[0] = substr($this->cb[0], $a + 1);
 64:         }
 65:         if (!is_callable($cb, TRUE)) {
 66:             throw new InvalidArgumentException("Invalid callback.");
 67:         }
 68:         $this->cb = $cb;
 69:     }
 70: 
 71: 
 72:     /**
 73:      * Invokes callback. Do not call directly.
 74:      * @return mixed
 75:      */
 76:     public function __invoke()
 77:     {
 78:         if (!is_callable($this->cb)) {
 79:             throw new InvalidStateException("Callback '$this' is not callable.");
 80:         }
 81:         $_args=func_get_args(); return call_user_func_array($this->cb, $_args);
 82:     }
 83: 
 84: 
 85:     /**
 86:      * Invokes callback.
 87:      * @return mixed
 88:      */
 89:     public function invoke()
 90:     {
 91:         if (!is_callable($this->cb)) {
 92:             throw new InvalidStateException("Callback '$this' is not callable.");
 93:         }
 94:         $_args=func_get_args(); return call_user_func_array($this->cb, $_args);
 95:     }
 96: 
 97: 
 98:     /**
 99:      * Invokes callback with an array of parameters.
100:      * @param  array
101:      * @return mixed
102:      */
103:     public function invokeArgs(array $args)
104:     {
105:         if (!is_callable($this->cb)) {
106:             throw new InvalidStateException("Callback '$this' is not callable.");
107:         }
108:         return call_user_func_array($this->cb, $args);
109:     }
110: 
111: 
112:     /**
113:      * Verifies that callback can be called.
114:      * @return bool
115:      */
116:     public function isCallable()
117:     {
118:         return is_callable($this->cb);
119:     }
120: 
121: 
122:     /**
123:      * Returns PHP callback pseudotype.
124:      * @return string|array|Closure
125:      */
126:     public function getNative()
127:     {
128:         return $this->cb;
129:     }
130: 
131: 
132:     /**
133:      * Returns callback reflection.
134:      * @return FunctionReflection|MethodReflection
135:      */
136:     public function toReflection()
137:     {
138:         if (is_string($this->cb) && strpos($this->cb, '::')) {
139:             return new MethodReflection($this->cb);
140:         } elseif (is_array($this->cb)) {
141:             return new MethodReflection($this->cb[0], $this->cb[1]);
142:         } elseif (is_object($this->cb) && !$this->cb instanceof Closure) {
143:             return new MethodReflection($this->cb, '__invoke');
144:         } else {
145:             return new FunctionReflection($this->cb);
146:         }
147:     }
148: 
149: 
150:     /**
151:      * @return bool
152:      */
153:     public function isStatic()
154:     {
155:         return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
156:     }
157: 
158: 
159:     /**
160:      * @return string
161:      */
162:     public function __toString()
163:     {
164:         if ($this->cb instanceof Closure) {
165:             return '{closure}';
166:         } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
167:             return '{lambda}';
168:         } else {
169:             is_callable($this->cb, TRUE, $textual);
170:             return $textual;
171:         }
172:     }
173: 
174: }
175: 
Nette Framework 2.0.18 (for PHP 5.2, un-prefixed) API documentation generated by ApiGen 2.8.0