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

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
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * PHP callback encapsulation.
 15:  *
 16:  * @author     David Grudl
 17:  * @property-read bool $callable
 18:  * @property-read string|array|\Closure $native
 19:  * @property-read bool $static
 20:  */
 21: class Callback extends Object
 22: {
 23:     /** @var callable */
 24:     private $cb;
 25: 
 26: 
 27:     /**
 28:      * Factory. Workaround for missing (new Callback)->invoke() in PHP 5.3.
 29:      * @param  mixed   class, object, callable
 30:      * @param  string  method
 31:      * @return Callback
 32:      */
 33:     public static function create($callback, $m = NULL)
 34:     {
 35:         return new self($callback, $m);
 36:     }
 37: 
 38: 
 39:     /**
 40:      * @param  mixed   class, object, callable
 41:      * @param  string  method
 42:      */
 43:     public function __construct($cb, $m = NULL)
 44:     {
 45:         if ($m !== NULL) {
 46:             $cb = array($cb, $m);
 47: 
 48:         } elseif ($cb instanceof self) { // prevents wrapping itself
 49:             $this->cb = $cb->cb;
 50:             return;
 51:         }
 52: 
 53:         if (!is_callable($cb, TRUE)) {
 54:             throw new InvalidArgumentException("Invalid callback.");
 55:         }
 56:         $this->cb = $cb;
 57:     }
 58: 
 59: 
 60:     /**
 61:      * Invokes callback. Do not call directly.
 62:      * @return mixed
 63:      */
 64:     public function __invoke()
 65:     {
 66:         if (!is_callable($this->cb)) {
 67:             throw new InvalidStateException("Callback '$this' is not callable.");
 68:         }
 69:         return call_user_func_array($this->cb, func_get_args());
 70:     }
 71: 
 72: 
 73:     /**
 74:      * Invokes callback.
 75:      * @return mixed
 76:      */
 77:     public function invoke()
 78:     {
 79:         if (!is_callable($this->cb)) {
 80:             throw new InvalidStateException("Callback '$this' is not callable.");
 81:         }
 82:         return call_user_func_array($this->cb, func_get_args());
 83:     }
 84: 
 85: 
 86:     /**
 87:      * Invokes callback with an array of parameters.
 88:      * @param  array
 89:      * @return mixed
 90:      */
 91:     public function invokeArgs(array $args)
 92:     {
 93:         if (!is_callable($this->cb)) {
 94:             throw new InvalidStateException("Callback '$this' is not callable.");
 95:         }
 96:         return call_user_func_array($this->cb, $args);
 97:     }
 98: 
 99: 
100:     /**
101:      * Verifies that callback can be called.
102:      * @return bool
103:      */
104:     public function isCallable()
105:     {
106:         return is_callable($this->cb);
107:     }
108: 
109: 
110:     /**
111:      * Returns PHP callback pseudotype.
112:      * @return string|array|\Closure
113:      */
114:     public function getNative()
115:     {
116:         return $this->cb;
117:     }
118: 
119: 
120:     /**
121:      * Returns callback reflection.
122:      * @return Nette\Reflection\GlobalFunction|Nette\Reflection\Method
123:      */
124:     public function toReflection()
125:     {
126:         if (is_string($this->cb) && strpos($this->cb, '::')) {
127:             return new Nette\Reflection\Method($this->cb);
128:         } elseif (is_array($this->cb)) {
129:             return new Nette\Reflection\Method($this->cb[0], $this->cb[1]);
130:         } elseif (is_object($this->cb) && !$this->cb instanceof \Closure) {
131:             return new Nette\Reflection\Method($this->cb, '__invoke');
132:         } else {
133:             return new Nette\Reflection\GlobalFunction($this->cb);
134:         }
135:     }
136: 
137: 
138:     /**
139:      * @return bool
140:      */
141:     public function isStatic()
142:     {
143:         return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
144:     }
145: 
146: 
147:     /**
148:      * @return string
149:      */
150:     public function __toString()
151:     {
152:         if ($this->cb instanceof \Closure) {
153:             return '{closure}';
154:         } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
155:             return '{lambda}';
156:         } else {
157:             is_callable($this->cb, TRUE, $textual);
158:             return $textual;
159:         }
160:     }
161: 
162: }
163: 
Nette 2.0 API documentation generated by ApiGen 2.8.0