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

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

Interfaces

  • IFreezable

Exceptions

  • ArgumentOutOfRangeException
  • DeprecatedException
  • DirectoryNotFoundException
  • FatalErrorException
  • FileNotFoundException
  • InvalidStateException
  • IOException
  • MemberAccessException
  • NotImplementedException
  • NotSupportedException
  • StaticClassException
  • UnknownImageFileException
  • 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 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 Callback extends Object
 25: {
 26:     /** @var callable */
 27:     private $cb;
 28: 
 29: 
 30: 
 31:     /**
 32:      * Do not call directly, use callback() function.
 33:      * @param  callable
 34:      */
 35:     public function __construct($cb, $m = NULL)
 36:     {
 37:         if ($m !== NULL) {
 38:             $cb = array($cb, $m); // back-compatibility
 39:         }
 40:         if (PHP_VERSION_ID < 50202 && is_string($cb) && strpos($cb, '::')) {
 41:             $cb = explode('::', $cb, 2);
 42:         } elseif (is_object($cb) && !$cb instanceof Closure) {
 43:             $cb = array($cb, '__invoke');
 44:         }
 45: 
 46:         // remove class namespace
 47:         if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
 48:             $this->cb = substr($this->cb, $a + 1);
 49: 
 50:         } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
 51:             $this->cb[0] = substr($this->cb[0], $a + 1);
 52:         }
 53:         if (!is_callable($cb, TRUE)) {
 54:             throw new InvalidArgumentException("Invalid callback.");
 55:         }
 56:         $this->cb = $cb;
 57:     }
 58: 
 59: 
 60: 
 61:     /**
 62:      * Invokes callback. Do not call directly.
 63:      * @return mixed
 64:      */
 65:     public function __invoke()
 66:     {
 67:         if (!is_callable($this->cb)) {
 68:             throw new InvalidStateException("Callback '$this' is not callable.");
 69:         }
 70:         $args = func_get_args();
 71:         return call_user_func_array($this->cb, $args);
 72:     }
 73: 
 74: 
 75: 
 76:     /**
 77:      * Invokes callback.
 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();
 86:         return call_user_func_array($this->cb, $args);
 87:     }
 88: 
 89: 
 90: 
 91:     /**
 92:      * Invokes callback with an array of parameters.
 93:      * @param  array
 94:      * @return mixed
 95:      */
 96:     public function invokeArgs(array $args)
 97:     {
 98:         if (!is_callable($this->cb)) {
 99:             throw new InvalidStateException("Callback '$this' is not callable.");
100:         }
101:         return call_user_func_array($this->cb, $args);
102:     }
103: 
104: 
105: 
106:     /**
107:      * Verifies that callback can be called.
108:      * @return bool
109:      */
110:     public function isCallable()
111:     {
112:         return is_callable($this->cb);
113:     }
114: 
115: 
116: 
117:     /**
118:      * Returns PHP callback pseudotype.
119:      * @return string|array|Closure
120:      */
121:     public function getNative()
122:     {
123:         return $this->cb;
124:     }
125: 
126: 
127: 
128:     /**
129:      * Returns callback reflection.
130:      * @return FunctionReflection|MethodReflection
131:      */
132:     public function toReflection()
133:     {
134:         if (is_string($this->cb) && strpos($this->cb, '::')) {
135:             return new MethodReflection($this->cb);
136:         } elseif (is_array($this->cb)) {
137:             return new MethodReflection($this->cb[0], $this->cb[1]);
138:         } elseif (is_object($this->cb) && !$this->cb instanceof Closure) {
139:             return new MethodReflection($this->cb, '__invoke');
140:         } else {
141:             return new FunctionReflection($this->cb);
142:         }
143:     }
144: 
145: 
146: 
147:     /**
148:      * @return bool
149:      */
150:     public function isStatic()
151:     {
152:         return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
153:     }
154: 
155: 
156: 
157:     /**
158:      * @return string
159:      */
160:     public function __toString()
161:     {
162:         if ($this->cb instanceof Closure) {
163:             return '{closure}';
164:         } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
165:             return '{lambda}';
166:         } else {
167:             is_callable($this->cb, TRUE, $textual);
168:             return $textual;
169:         }
170:     }
171: 
172: }
173: 
Nette Framework 2.0.3 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0