Nette\Utils\Callback::closure PHP Method

closure() public static method

public static closure ( $callable, $m = NULL ) : Closure
return Closure
    public static function closure($callable, $m = NULL)
    {
        if ($m !== NULL) {
            $callable = [$callable, $m];
        } elseif (is_string($callable) && count($tmp = explode('::', $callable)) === 2) {
            $callable = $tmp;
        } elseif ($callable instanceof \Closure) {
            return $callable;
        } elseif (is_object($callable)) {
            $callable = [$callable, '__invoke'];
        }
        if (is_string($callable) && function_exists($callable)) {
            return (new \ReflectionFunction($callable))->getClosure();
        } elseif (is_array($callable) && method_exists($callable[0], $callable[1])) {
            return (new \ReflectionMethod($callable[0], $callable[1]))->getClosure($callable[0]);
        }
        self::check($callable);
        $_callable_ = $callable;
        return function (...$args) use($_callable_) {
            return $_callable_(...$args);
        };
    }

Usage Example

Beispiel #1
0
 /**
  * @param Container $container
  * @param array $callbacks [optional]
  */
 public function __construct(Container $container, $callbacks = [])
 {
     $this->container = $container;
     /** @var $httpRequest Request */
     $request = $container->getService("httpRequest");
     // Determine production/development mode
     $this->active = !Debugger::$productionMode;
     // # Clean cache
     $this->callbacks["cache"] = ['name' => "Clear cache", 'callback' => Callback::closure($this, "clearCache"), 'args' => [[Cache::ALL => TRUE]]];
     // # Clean session
     $this->callbacks["session"] = ['name' => "Clear session", 'callback' => Callback::closure($this, "clearSession"), 'args' => []];
     // # Clean logs
     $this->callbacks["logs"] = ['name' => "Clear logs", 'callback' => Callback::closure($this, "clearLogs"), 'args' => [[Cache::ALL => TRUE]]];
     // Merge custom callbacks
     $this->callbacks = array_merge($this->callbacks, $callbacks);
     // Check signal receiver
     if ($this->active && ($cb = $request->getQuery("callback-do", FALSE))) {
         if ($cb === "all") {
             $this->onCallbacksCall();
             $this->invokeCallbacks();
         } else {
             $this->onCallbackCall($cb);
             $this->invokeCallback($cb);
         }
     }
 }
All Usage Examples Of Nette\Utils\Callback::closure