Overview

Namespaces

  • Budovy
  • Kdyby
    • BootstrapFormRenderer
      • DI
      • Latte
  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • Nextras
    • Datagrid
  • None
  • PHP
  • Tester
    • CodeCoverage
    • Runner
      • Output
  • Vodacek
    • Forms
      • Controls
  • WebLoader
    • Filter
    • Nette

Classes

  • Arrays
  • Callback
  • FileSystem
  • Finder
  • Html
  • Json
  • LimitedScope
  • MimeTypeDetector
  • Neon
  • NeonEntity
  • Paginator
  • Strings
  • Validators

Exceptions

  • AssertionException
  • JsonException
  • NeonException
  • RegexpException
  • TokenizerException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Utils;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * PHP callable tools.
 15:  *
 16:  * @author     David Grudl
 17:  */
 18: class Callback
 19: {
 20: 
 21:     /**
 22:      * @param  mixed   class, object, callable
 23:      * @param  string  method
 24:      * @return \Closure
 25:      */
 26:     public static function closure($callable, $m = NULL)
 27:     {
 28:         if ($m !== NULL) {
 29:             $callable = array($callable, $m);
 30:         } elseif ($callable instanceof \Closure) {
 31:             return $callable;
 32:         }
 33: 
 34:         self::check($callable, TRUE);
 35:         $_callable_ = $callable;
 36:         return function() use ($_callable_) {
 37:             Callback::check($_callable_);
 38:             return call_user_func_array($_callable_, func_get_args());
 39:         };
 40:     }
 41: 
 42: 
 43:     /**
 44:      * Invokes callback.
 45:      * @return mixed
 46:      */
 47:     public static function invoke($callable)
 48:     {
 49:         self::check($callable);
 50:         return call_user_func_array($callable, array_slice(func_get_args(), 1));
 51:     }
 52: 
 53: 
 54:     /**
 55:      * Invokes callback with an array of parameters.
 56:      * @return mixed
 57:      */
 58:     public static function invokeArgs($callable, array $args = array())
 59:     {
 60:         self::check($callable);
 61:         return call_user_func_array($callable, $args);
 62:     }
 63: 
 64: 
 65:     /**
 66:      * @return callable
 67:      */
 68:     public static function check($callable, $syntax = FALSE)
 69:     {
 70:         if (!is_callable($callable, $syntax)) {
 71:             throw new Nette\InvalidArgumentException($syntax
 72:                 ? 'Given value is not a callable type.'
 73:                 : "Callback '" . self::toString($callable) . "' is not callable."
 74:             );
 75:         }
 76:         return $callable;
 77:     }
 78: 
 79: 
 80:     /**
 81:      * @return string
 82:      */
 83:     public static function toString($callable)
 84:     {
 85:         if ($callable instanceof \Closure) {
 86:             if ($inner = self::unwrap($callable)) {
 87:                 return '{closure ' . self::toString($inner) . '}';
 88:             }
 89:             return '{closure}';
 90:         } elseif (is_string($callable) && $callable[0] === "\0") {
 91:             return '{lambda}';
 92:         } else {
 93:             is_callable($callable, TRUE, $textual);
 94:             return $textual;
 95:         }
 96:     }
 97: 
 98: 
 99:     /**
100:      * @return Nette\Reflection\GlobalFunction|Nette\Reflection\Method
101:      */
102:     public static function toReflection($callable)
103:     {
104:         if ($callable instanceof \Closure && $inner = self::unwrap($callable)) {
105:             $callable = $inner;
106:         } elseif ($callable instanceof Nette\Callback) {
107:             $callable = $callable->getNative();
108:         }
109: 
110:         if (is_string($callable) && strpos($callable, '::')) {
111:             return new Nette\Reflection\Method($callable);
112:         } elseif (is_array($callable)) {
113:             return new Nette\Reflection\Method($callable[0], $callable[1]);
114:         } elseif (is_object($callable) && !$callable instanceof \Closure) {
115:             return new Nette\Reflection\Method($callable, '__invoke');
116:         } else {
117:             return new Nette\Reflection\GlobalFunction($callable);
118:         }
119:     }
120: 
121: 
122:     /**
123:      * @return bool
124:      */
125:     public static function isStatic($callable)
126:     {
127:         return is_array($callable) ? is_string($callable[0]) : is_string($callable);
128:     }
129: 
130: 
131: 
132:     /**
133:      * Unwraps closure created by self::closure(), used i.e. by ObjectMixin in PHP < 5.4
134:      * @internal
135:      * @return callable
136:      */
137:     public static function unwrap(\Closure $closure)
138:     {
139:         $rm = new \ReflectionFunction($closure);
140:         $vars = $rm->getStaticVariables();
141:         return isset($vars['_callable_']) ? $vars['_callable_'] : NULL;
142:     }
143: 
144: }
145: 
API documentation generated by ApiGen 2.8.0