Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NArrayTools
  • NCallback
  • NComponent
  • NComponentContainer
  • NConfigurator
  • NDateTime53
  • NDebug
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NGenericRecursiveIterator
  • NImage
  • NImageMagick
  • NInstanceFilterIterator
  • NObject
  • NObjectMixin
  • NPaginator
  • NRecursiveComponentIterator
  • NServiceLocator
  • NSmartCachingIterator
  • NString
  • NTools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

  • NAmbiguousServiceException
  • Overview
  • Package
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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:  * @package Nette
 20:  */
 21: final class NCallback extends NObject
 22: {
 23:     /** @var callback */
 24:     private $cb;
 25: 
 26: 
 27: 
 28:     /**
 29:      * Do not call directly, use callback() function.
 30:      * @param  mixed   class, object, function, callback
 31:      * @param  string  method
 32:      */
 33:     public function __construct($t, $m = NULL)
 34:     {
 35:         if ($m === NULL) {
 36:             $this->cb = $t;
 37:         } else {
 38:             $this->cb = array($t, $m);
 39:         }
 40: 
 41:         // __invoke support
 42:         if (is_object($this->cb)) {
 43:             $this->cb = array($this->cb, '__invoke');
 44: 
 45:         } elseif (PHP_VERSION_ID < 50202) {
 46:             // explode 'NClass::method' into array
 47:             if (is_string($this->cb) && strpos($this->cb, ':')) {
 48:                 $this->cb = explode('::', $this->cb);
 49:             }
 50: 
 51:             // remove class namespace
 52:             if (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
 53:                 $this->cb[0] = substr($this->cb[0], $a + 1);
 54:             }
 55: 
 56:         } else {
 57:             // remove class namespace
 58:             if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
 59:                 $this->cb = substr($this->cb, $a + 1);
 60: 
 61:             } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
 62:                 $this->cb[0] = substr($this->cb[0], $a + 1);
 63:             }
 64:         }
 65: 
 66:         if (!is_callable($this->cb, TRUE)) {
 67:             throw new InvalidArgumentException("Invalid callback.");
 68:         }
 69:     }
 70: 
 71: 
 72: 
 73:     /**
 74:      * Invokes callback. Do not call directly.
 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:         $args = func_get_args();
 83:         return call_user_func_array($this->cb, $args);
 84:     }
 85: 
 86: 
 87: 
 88:     /**
 89:      * Invokes callback.
 90:      * @return mixed
 91:      */
 92:     public function invoke()
 93:     {
 94:         if (!is_callable($this->cb)) {
 95:             throw new InvalidStateException("Callback '$this' is not callable.");
 96:         }
 97:         $args = func_get_args();
 98:         return call_user_func_array($this->cb, $args);
 99:     }
100: 
101: 
102: 
103:     /**
104:      * Invokes callback with an array of parameters.
105:      * @param  array
106:      * @return mixed
107:      */
108:     public function invokeArgs(array $args)
109:     {
110:         if (!is_callable($this->cb)) {
111:             throw new InvalidStateException("Callback '$this' is not callable.");
112:         }
113:         return call_user_func_array($this->cb, $args);
114:     }
115: 
116: 
117: 
118:     /**
119:      * Verifies that callback can be called.
120:      * @return bool
121:      */
122:     public function isCallable()
123:     {
124:         return is_callable($this->cb);
125:     }
126: 
127: 
128: 
129:     /**
130:      * Returns PHP callback pseudotype.
131:      * @return callback
132:      */
133:     public function getNative()
134:     {
135:         return $this->cb;
136:     }
137: 
138: 
139: 
140:     /**
141:      * @return string
142:      */
143:     public function __toString()
144:     {
145:         is_callable($this->cb, TRUE, $textual);
146:         return $textual;
147:     }
148: 
149: }
150: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0