Packages

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

Classes

  • ArrayHash
  • ArrayList
  • Callback
  • Configurator
  • DateTime53
  • Framework
  • FreezableObject
  • Image
  • Object
  • ObjectMixin

Interfaces

  • IFreezable

Exceptions

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