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
  • PHP

Classes

  • NArrayHash
  • NArrayList
  • NCallback
  • NDateTime53
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NImage
  • NObject
  • NObjectMixin

Interfaces

  • IFreezable

Exceptions

  • ArgumentOutOfRangeException
  • DeprecatedException
  • DirectoryNotFoundException
  • FatalErrorException
  • FileNotFoundException
  • InvalidStateException
  • IOException
  • MemberAccessException
  • NotImplementedException
  • NotSupportedException
  • NStaticClassException
  • NUnknownImageFileException
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 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:  * @property-read bool $callable
 20:  * @property-read string|array|Closure $native
 21:  * @property-read bool $static
 22:  * @package Nette
 23:  */
 24: final class NCallback extends NObject
 25: {
 26:     /** @var callable */
 27:     private $cb;
 28: 
 29: 
 30: 
 31:     /**
 32:      * Factory. Workaround for missing (new Callback)->invoke() in PHP 5.3.
 33:      * @param  mixed   class, object, callable
 34:      * @param  string  method
 35:      * @return NCallback
 36:      */
 37:     public static function create($callback, $m = NULL)
 38:     {
 39:         return new self($callback, $m);
 40:     }
 41: 
 42: 
 43: 
 44:     /**
 45:      * @param  mixed   class, object, callable
 46:      * @param  string  method
 47:      */
 48:     public function __construct($cb, $m = NULL)
 49:     {
 50:         if ($m !== NULL) {
 51:             $cb = array($cb, $m);
 52: 
 53:         } elseif ($cb instanceof self) { // prevents wrapping itself
 54:             $this->cb = $cb->cb;
 55:             return;
 56:         }
 57: 
 58:         if (PHP_VERSION_ID < 50202 && is_string($cb) && strpos($cb, '::')) {
 59:             $cb = explode('::', $cb, 2);
 60:         } elseif (is_object($cb) && !$cb instanceof NClosure) {
 61:             $cb = array($cb, '__invoke');
 62:         }
 63: 
 64:         // remove class namespace
 65:         if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
 66:             $this->cb = substr($this->cb, $a + 1);
 67: 
 68:         } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
 69:             $this->cb[0] = substr($this->cb[0], $a + 1);
 70:         }
 71:         if (!is_callable($cb, TRUE)) {
 72:             throw new InvalidArgumentException("Invalid callback.");
 73:         }
 74:         $this->cb = $cb;
 75:     }
 76: 
 77: 
 78: 
 79:     /**
 80:      * Invokes callback. Do not call directly.
 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.
 96:      * @return mixed
 97:      */
 98:     public function invoke()
 99:     {
100:         if (!is_callable($this->cb)) {
101:             throw new InvalidStateException("Callback '$this' is not callable.");
102:         }
103:         $args = func_get_args();
104:         return call_user_func_array($this->cb, $args);
105:     }
106: 
107: 
108: 
109:     /**
110:      * Invokes callback with an array of parameters.
111:      * @param  array
112:      * @return mixed
113:      */
114:     public function invokeArgs(array $args)
115:     {
116:         if (!is_callable($this->cb)) {
117:             throw new InvalidStateException("Callback '$this' is not callable.");
118:         }
119:         return call_user_func_array($this->cb, $args);
120:     }
121: 
122: 
123: 
124:     /**
125:      * Verifies that callback can be called.
126:      * @return bool
127:      */
128:     public function isCallable()
129:     {
130:         return is_callable($this->cb);
131:     }
132: 
133: 
134: 
135:     /**
136:      * Returns PHP callback pseudotype.
137:      * @return string|array|Closure
138:      */
139:     public function getNative()
140:     {
141:         return $this->cb;
142:     }
143: 
144: 
145: 
146:     /**
147:      * Returns callback reflection.
148:      * @return NFunctionReflection|NMethodReflection
149:      */
150:     public function toReflection()
151:     {
152:         if (is_string($this->cb) && strpos($this->cb, '::')) {
153:             return new NMethodReflection($this->cb);
154:         } elseif (is_array($this->cb)) {
155:             return new NMethodReflection($this->cb[0], $this->cb[1]);
156:         } elseif (is_object($this->cb) && !$this->cb instanceof Closure) {
157:             return new NMethodReflection($this->cb, '__invoke');
158:         } else {
159:             return new NFunctionReflection($this->cb);
160:         }
161:     }
162: 
163: 
164: 
165:     /**
166:      * @return bool
167:      */
168:     public function isStatic()
169:     {
170:         return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
171:     }
172: 
173: 
174: 
175:     /**
176:      * @return string
177:      */
178:     public function __toString()
179:     {
180:         if ($this->cb instanceof Closure) {
181:             return '{closure}';
182:         } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
183:             return '{lambda}';
184:         } else {
185:             is_callable($this->cb, TRUE, $textual);
186:             return $textual;
187:         }
188:     }
189: 
190: }
191: 
Nette Framework 2.0.7 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0