Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • ArrayHash
  • ArrayList
  • Arrays
  • Callback
  • DateTime
  • FileSystem
  • Finder
  • Html
  • Image
  • Json
  • ObjectHelpers
  • ObjectMixin
  • Paginator
  • Random
  • Reflection
  • SafeStream
  • Strings
  • TokenIterator
  • Tokenizer
  • Validators

Interfaces

  • IHtmlString

Exceptions

  • AssertionException
  • ImageException
  • JsonException
  • RegexpException
  • TokenizerException
  • UnknownImageFileException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Utils;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * PHP callable tools.
 15:  */
 16: class Callback
 17: {
 18:     use Nette\StaticClass;
 19: 
 20:     /**
 21:      * @param  mixed   class, object, callable
 22:      * @param  string  method
 23:      * @return \Closure
 24:      */
 25:     public static function closure($callable, $m = null)
 26:     {
 27:         if ($m !== null) {
 28:             $callable = [$callable, $m];
 29: 
 30:         } elseif (is_string($callable) && count($tmp = explode('::', $callable)) === 2) {
 31:             $callable = $tmp;
 32: 
 33:         } elseif ($callable instanceof \Closure) {
 34:             return $callable;
 35: 
 36:         } elseif (is_object($callable)) {
 37:             $callable = [$callable, '__invoke'];
 38:         }
 39: 
 40:         if (is_string($callable) && function_exists($callable)) {
 41:             return (new \ReflectionFunction($callable))->getClosure();
 42: 
 43:         } elseif (is_array($callable) && method_exists($callable[0], $callable[1])) {
 44:             return (new \ReflectionMethod($callable[0], $callable[1]))->getClosure($callable[0]);
 45:         }
 46: 
 47:         self::check($callable);
 48:         $_callable_ = $callable;
 49:         return function (...$args) use ($_callable_) {
 50:             return $_callable_(...$args);
 51:         };
 52:     }
 53: 
 54: 
 55:     /**
 56:      * Invokes callback.
 57:      * @return mixed
 58:      * @deprecated
 59:      */
 60:     public static function invoke($callable, ...$args)
 61:     {
 62:         self::check($callable);
 63:         return call_user_func_array($callable, $args);
 64:     }
 65: 
 66: 
 67:     /**
 68:      * Invokes callback with an array of parameters.
 69:      * @return mixed
 70:      * @deprecated
 71:      */
 72:     public static function invokeArgs($callable, array $args = [])
 73:     {
 74:         self::check($callable);
 75:         return call_user_func_array($callable, $args);
 76:     }
 77: 
 78: 
 79:     /**
 80:      * Invokes internal PHP function with own error handler.
 81:      * @param  string
 82:      * @return mixed
 83:      */
 84:     public static function invokeSafe($function, array $args, $onError)
 85:     {
 86:         $prev = set_error_handler(function ($severity, $message, $file) use ($onError, &$prev, $function) {
 87:             if ($file === '' && defined('HHVM_VERSION')) { // https://github.com/facebook/hhvm/issues/4625
 88:                 $file = func_get_arg(5)[1]['file'];
 89:             }
 90:             if ($file === __FILE__) {
 91:                 $msg = $message;
 92:                 if (ini_get('html_errors')) {
 93:                     $msg = html_entity_decode(strip_tags($msg));
 94:                 }
 95:                 $msg = preg_replace("#^$function\(.*?\): #", '', $msg);
 96:                 if ($onError($msg, $severity) !== false) {
 97:                     return;
 98:                 }
 99:             }
100:             return $prev ? $prev(...func_get_args()) : false;
101:         });
102: 
103:         try {
104:             return call_user_func_array($function, $args);
105:         } finally {
106:             restore_error_handler();
107:         }
108:     }
109: 
110: 
111:     /**
112:      * @return callable
113:      */
114:     public static function check($callable, $syntax = false)
115:     {
116:         if (!is_callable($callable, $syntax)) {
117:             throw new Nette\InvalidArgumentException($syntax
118:                 ? 'Given value is not a callable type.'
119:                 : sprintf("Callback '%s' is not callable.", self::toString($callable))
120:             );
121:         }
122:         return $callable;
123:     }
124: 
125: 
126:     /**
127:      * @return string
128:      */
129:     public static function toString($callable)
130:     {
131:         if ($callable instanceof \Closure) {
132:             $inner = self::unwrap($callable);
133:             return '{closure' . ($inner instanceof \Closure ? '}' : ' ' . self::toString($inner) . '}');
134:         } elseif (is_string($callable) && $callable[0] === "\0") {
135:             return '{lambda}';
136:         } else {
137:             is_callable(is_object($callable) ? [$callable, '__invoke'] : $callable, true, $textual);
138:             return $textual;
139:         }
140:     }
141: 
142: 
143:     /**
144:      * @return \ReflectionMethod|\ReflectionFunction
145:      */
146:     public static function toReflection($callable)
147:     {
148:         if ($callable instanceof \Closure) {
149:             $callable = self::unwrap($callable);
150:         }
151: 
152:         $class = class_exists(Nette\Reflection\Method::class) ? Nette\Reflection\Method::class : 'ReflectionMethod';
153:         if (is_string($callable) && strpos($callable, '::')) {
154:             return new $class($callable);
155:         } elseif (is_array($callable)) {
156:             return new $class($callable[0], $callable[1]);
157:         } elseif (is_object($callable) && !$callable instanceof \Closure) {
158:             return new $class($callable, '__invoke');
159:         } else {
160:             $class = class_exists(Nette\Reflection\GlobalFunction::class) ? Nette\Reflection\GlobalFunction::class : 'ReflectionFunction';
161:             return new $class($callable);
162:         }
163:     }
164: 
165: 
166:     /**
167:      * @return bool
168:      */
169:     public static function isStatic($callable)
170:     {
171:         return is_array($callable) ? is_string($callable[0]) : is_string($callable);
172:     }
173: 
174: 
175:     /**
176:      * Unwraps closure created by self::closure()
177:      * @internal
178:      * @return callable
179:      */
180:     public static function unwrap(\Closure $closure)
181:     {
182:         $r = new \ReflectionFunction($closure);
183:         if (substr($r->getName(), -1) === '}') {
184:             $vars = $r->getStaticVariables();
185:             return isset($vars['_callable_']) ? $vars['_callable_'] : $closure;
186: 
187:         } elseif ($obj = $r->getClosureThis()) {
188:             return [$obj, $r->getName()];
189: 
190:         } elseif ($class = $r->getClosureScopeClass()) {
191:             return [$class->getName(), $r->getName()];
192: 
193:         } else {
194:             return $r->getName();
195:         }
196:     }
197: }
198: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0