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:  * PHP callback encapsulation.
 19:  *
 20:  * @author     David Grudl
 21:  * @property-read bool $callable
 22:  * @property-read string|array|\Closure $native
 23:  * @property-read bool $static
 24:  */
 25: final class Callback extends Object
 26: {
 27:     /** @var callable */
 28:     private $cb;
 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 Callback
 36:      */
 37:     public static function create($callback, $m = NULL)
 38:     {
 39:         return new self($callback, $m);
 40:     }
 41: 
 42: 
 43:     /**
 44:      * @param  mixed   class, object, callable
 45:      * @param  string  method
 46:      */
 47:     public function __construct($cb, $m = NULL)
 48:     {
 49:         if ($m !== NULL) {
 50:             $cb = array($cb, $m);
 51: 
 52:         } elseif ($cb instanceof self) { // prevents wrapping itself
 53:             $this->cb = $cb->cb;
 54:             return;
 55:         }
 56: 
 57:         if (!is_callable($cb, TRUE)) {
 58:             throw new InvalidArgumentException("Invalid callback.");
 59:         }
 60:         $this->cb = $cb;
 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:         return call_user_func_array($this->cb, func_get_args());
 74:     }
 75: 
 76: 
 77:     /**
 78:      * Invokes callback.
 79:      * @return mixed
 80:      */
 81:     public function invoke()
 82:     {
 83:         if (!is_callable($this->cb)) {
 84:             throw new InvalidStateException("Callback '$this' is not callable.");
 85:         }
 86:         return call_user_func_array($this->cb, func_get_args());
 87:     }
 88: 
 89: 
 90:     /**
 91:      * Invokes callback with an array of parameters.
 92:      * @param  array
 93:      * @return mixed
 94:      */
 95:     public function invokeArgs(array $args)
 96:     {
 97:         if (!is_callable($this->cb)) {
 98:             throw new InvalidStateException("Callback '$this' is not callable.");
 99:         }
100:         return call_user_func_array($this->cb, $args);
101:     }
102: 
103: 
104:     /**
105:      * Verifies that callback can be called.
106:      * @return bool
107:      */
108:     public function isCallable()
109:     {
110:         return is_callable($this->cb);
111:     }
112: 
113: 
114:     /**
115:      * Returns PHP callback pseudotype.
116:      * @return string|array|\Closure
117:      */
118:     public function getNative()
119:     {
120:         return $this->cb;
121:     }
122: 
123: 
124:     /**
125:      * Returns callback reflection.
126:      * @return Nette\Reflection\GlobalFunction|Nette\Reflection\Method
127:      */
128:     public function toReflection()
129:     {
130:         if (is_string($this->cb) && strpos($this->cb, '::')) {
131:             return new Nette\Reflection\Method($this->cb);
132:         } elseif (is_array($this->cb)) {
133:             return new Nette\Reflection\Method($this->cb[0], $this->cb[1]);
134:         } elseif (is_object($this->cb) && !$this->cb instanceof \Closure) {
135:             return new Nette\Reflection\Method($this->cb, '__invoke');
136:         } else {
137:             return new Nette\Reflection\GlobalFunction($this->cb);
138:         }
139:     }
140: 
141: 
142:     /**
143:      * @return bool
144:      */
145:     public function isStatic()
146:     {
147:         return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
148:     }
149: 
150: 
151:     /**
152:      * @return string
153:      */
154:     public function __toString()
155:     {
156:         if ($this->cb instanceof \Closure) {
157:             return '{closure}';
158:         } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
159:             return '{lambda}';
160:         } else {
161:             is_callable($this->cb, TRUE, $textual);
162:             return $textual;
163:         }
164:     }
165: 
166: }
167: 
Nette Framework 2.0.12 API API documentation generated by ApiGen 2.8.0