Namespaces

  • 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
  • DateTime
  • Environment
  • Framework
  • FreezableObject
  • Image
  • Object
  • ObjectMixin

Interfaces

  • IFreezable

Exceptions

  • ArgumentOutOfRangeException
  • DeprecatedException
  • DirectoryNotFoundException
  • FatalErrorException
  • FileNotFoundException
  • InvalidArgumentException
  • InvalidStateException
  • IOException
  • MemberAccessException
  • NotImplementedException
  • NotSupportedException
  • OutOfRangeException
  • StaticClassException
  • UnexpectedValueException
  • UnknownImageFileException
  • Overview
  • Namespace
  • 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:  */
 11: 
 12: namespace Nette;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * PHP callback encapsulation.
 20:  *
 21:  * @author     David Grudl
 22:  * @property-read bool $callable
 23:  * @property-read string|array|\Closure $native
 24:  * @property-read bool $static
 25:  */
 26: final class Callback extends Object
 27: {
 28:     /** @var callable */
 29:     private $cb;
 30: 
 31: 
 32: 
 33:     /**
 34:      * Factory. Workaround for missing (new Callback)->invoke() in PHP 5.3.
 35:      * @param  mixed   class, object, callable
 36:      * @param  string  method
 37:      * @return Callback
 38:      */
 39:     public static function create($callback, $m = NULL)
 40:     {
 41:         return new self($callback, $m);
 42:     }
 43: 
 44: 
 45: 
 46:     /**
 47:      * @param  mixed   class, object, callable
 48:      * @param  string  method
 49:      */
 50:     public function __construct($cb, $m = NULL)
 51:     {
 52:         if ($m !== NULL) {
 53:             $cb = array($cb, $m);
 54: 
 55:         } elseif ($cb instanceof self) { // prevents wrapping itself
 56:             $this->cb = $cb->cb;
 57:             return;
 58:         }
 59: 
 60:         if (!is_callable($cb, TRUE)) {
 61:             throw new InvalidArgumentException("Invalid callback.");
 62:         }
 63:         $this->cb = $cb;
 64:     }
 65: 
 66: 
 67: 
 68:     /**
 69:      * Invokes callback. Do not call directly.
 70:      * @return mixed
 71:      */
 72:     public function __invoke()
 73:     {
 74:         if (!is_callable($this->cb)) {
 75:             throw new InvalidStateException("Callback '$this' is not callable.");
 76:         }
 77:         $args = func_get_args();
 78:         return call_user_func_array($this->cb, $args);
 79:     }
 80: 
 81: 
 82: 
 83:     /**
 84:      * Invokes callback.
 85:      * @return mixed
 86:      */
 87:     public function invoke()
 88:     {
 89:         if (!is_callable($this->cb)) {
 90:             throw new InvalidStateException("Callback '$this' is not callable.");
 91:         }
 92:         $args = func_get_args();
 93:         return call_user_func_array($this->cb, $args);
 94:     }
 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:     /**
114:      * Verifies that callback can be called.
115:      * @return bool
116:      */
117:     public function isCallable()
118:     {
119:         return is_callable($this->cb);
120:     }
121: 
122: 
123: 
124:     /**
125:      * Returns PHP callback pseudotype.
126:      * @return string|array|\Closure
127:      */
128:     public function getNative()
129:     {
130:         return $this->cb;
131:     }
132: 
133: 
134: 
135:     /**
136:      * Returns callback reflection.
137:      * @return Nette\Reflection\GlobalFunction|Nette\Reflection\Method
138:      */
139:     public function toReflection()
140:     {
141:         if (is_string($this->cb) && strpos($this->cb, '::')) {
142:             return new Nette\Reflection\Method($this->cb);
143:         } elseif (is_array($this->cb)) {
144:             return new Nette\Reflection\Method($this->cb[0], $this->cb[1]);
145:         } elseif (is_object($this->cb) && !$this->cb instanceof \Closure) {
146:             return new Nette\Reflection\Method($this->cb, '__invoke');
147:         } else {
148:             return new Nette\Reflection\GlobalFunction($this->cb);
149:         }
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:     /**
165:      * @return string
166:      */
167:     public function __toString()
168:     {
169:         if ($this->cb instanceof \Closure) {
170:             return '{closure}';
171:         } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
172:             return '{lambda}';
173:         } else {
174:             is_callable($this->cb, TRUE, $textual);
175:             return $textual;
176:         }
177:     }
178: 
179: }
180: 
Nette Framework 2.0.10 API API documentation generated by ApiGen 2.8.0