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:      * Factory. Workaround for missing (new Callback)->invoke() in PHP 5.3.
 32:      * @param  mixed   class, object, callable
 33:      * @param  string  method
 34:      * @return NCallback
 35:      */
 36:     public static function create($callback, $m = NULL)
 37:     {
 38:         return new self($callback, $m);
 39:     }
 40: 
 41: 
 42:     /**
 43:      * @param  mixed   class, object, callable
 44:      * @param  string  method
 45:      */
 46:     public function __construct($cb, $m = NULL)
 47:     {
 48:         if ($m !== NULL) {
 49:             $cb = array($cb, $m);
 50: 
 51:         } elseif ($cb instanceof self) { // prevents wrapping itself
 52:             $this->cb = $cb->cb;
 53:             return;
 54:         }
 55: 
 56:         if (PHP_VERSION_ID < 50202 && is_string($cb) && strpos($cb, '::')) {
 57:             $cb = explode('::', $cb, 2);
 58:         } elseif (is_object($cb) && !$cb instanceof NClosure) {
 59:             $cb = array($cb, '__invoke');
 60:         }
 61: 
 62:         // remove class namespace
 63:         if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
 64:             $this->cb = substr($this->cb, $a + 1);
 65: 
 66:         } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
 67:             $this->cb[0] = substr($this->cb[0], $a + 1);
 68:         }
 69:         if (!is_callable($cb, TRUE)) {
 70:             throw new InvalidArgumentException("Invalid callback.");
 71:         }
 72:         $this->cb = $cb;
 73:     }
 74: 
 75: 
 76:     /**
 77:      * Invokes callback. Do not call directly.
 78:      * @return mixed
 79:      */
 80:     public function __invoke()
 81:     {
 82:         if (!is_callable($this->cb)) {
 83:             throw new InvalidStateException("Callback '$this' is not callable.");
 84:         }
 85:         $_args=func_get_args(); return call_user_func_array($this->cb, $_args);
 86:     }
 87: 
 88: 
 89:     /**
 90:      * Invokes callback.
 91:      * @return mixed
 92:      */
 93:     public function invoke()
 94:     {
 95:         if (!is_callable($this->cb)) {
 96:             throw new InvalidStateException("Callback '$this' is not callable.");
 97:         }
 98:         $_args=func_get_args(); return call_user_func_array($this->cb, $_args);
 99:     }
100: 
101: 
102:     /**
103:      * Invokes callback with an array of parameters.
104:      * @param  array
105:      * @return mixed
106:      */
107:     public function invokeArgs(array $args)
108:     {
109:         if (!is_callable($this->cb)) {
110:             throw new InvalidStateException("Callback '$this' is not callable.");
111:         }
112:         return call_user_func_array($this->cb, $args);
113:     }
114: 
115: 
116:     /**
117:      * Verifies that callback can be called.
118:      * @return bool
119:      */
120:     public function isCallable()
121:     {
122:         return is_callable($this->cb);
123:     }
124: 
125: 
126:     /**
127:      * Returns PHP callback pseudotype.
128:      * @return string|array|Closure
129:      */
130:     public function getNative()
131:     {
132:         return $this->cb;
133:     }
134: 
135: 
136:     /**
137:      * Returns callback reflection.
138:      * @return NFunctionReflection|NMethodReflection
139:      */
140:     public function toReflection()
141:     {
142:         if (is_string($this->cb) && strpos($this->cb, '::')) {
143:             return new NMethodReflection($this->cb);
144:         } elseif (is_array($this->cb)) {
145:             return new NMethodReflection($this->cb[0], $this->cb[1]);
146:         } elseif (is_object($this->cb) && !$this->cb instanceof Closure) {
147:             return new NMethodReflection($this->cb, '__invoke');
148:         } else {
149:             return new NFunctionReflection($this->cb);
150:         }
151:     }
152: 
153: 
154:     /**
155:      * @return bool
156:      */
157:     public function isStatic()
158:     {
159:         return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
160:     }
161: 
162: 
163:     /**
164:      * @return string
165:      */
166:     public function __toString()
167:     {
168:         if ($this->cb instanceof Closure) {
169:             return '{closure}';
170:         } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
171:             return '{lambda}';
172:         } else {
173:             is_callable($this->cb, TRUE, $textual);
174:             return $textual;
175:         }
176:     }
177: 
178: }
179: 
Nette Framework 2.0.12 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0