Resolve closure and return its result

@param callable $closure
@param array $args
@return mixed
491612
public function resolveClosure(Closure $closure, array $args = [])
    {
        $closureRef = new ReflectionFunction($closure);
        $dependencies = $this->getMethodDependencies($closureRef, $args);

        return $closureRef->invokeArgs($dependencies);
    }
959863
public function share(Closure $closure)
    {
        return function($container) use ($closure)
        {
            // We'll simply declare a static variable within the Closures and if it has
            // not been set we will execute the given Closures to resolve this value
            // and return it back to these consumers of the method as an instance.
            static $object;

            if (is_null($object)) {
                $object = $closure($container);
            }

            return $object;
        };
    }
459760
function first(callable $closure = null) {
        if($closure === null) {
            if(empty($this->data)) {
                return null;
            }
            
            $keys = \array_keys($this->data);
            return ($this->data[$keys[0]] ?? null);
        }
        
        foreach($this->data as $key => $val) {
            $feed = (bool) $closure($val, $key);
            if($feed) {
                return $val;
            }
        }
        
        return null;
    }
476161
public function resolveClosure(\Closure $closureUnresolved)
    {
        $serviceId = $this->getRandomServiceName();
        $this->container->delegate($serviceId, $closureUnresolved);
        return $this->container->get($serviceId);
    }
589505
public function closureWrapper($closure)
    {
        $dependentService = $this->container->dependentService;
        return function (...$args) use ($closure, $dependentService) {
            return $dependentService->call($closure, $args);
        };
    }
943614
protected function resolveClosure($closure)
    {
        $scenario = $closure();

        if ($interfaces = class_implements($scenario)) {
            if (in_array('Keios\Apparatus\Contracts\Runnable', $interfaces)) {
                return is_object($scenario) ? $scenario : new $scenario();
            }
        }

        throw new InvalidScenarioException(
            sprintf('Closure return value is not a valid scenario object or scenario class name.')
        );
    }
56233
public function bind(\Closure $function, array $args = [], $noneValue = null)
    {
        return Option::create($this->callFunction($function, $this->value, $args), $noneValue);
    }
741782
public function resolveClosure(\Closure $closure)
    {
        $reflection = new \ReflectionFunction($closure);

        $dependencies = $reflection->getParameters();
        $instances = $this->resolveDependencies($dependencies); // Actually resolve the dependencies
        $o = $reflection->invokeArgs($instances);

        return $o;
    }
618909
public function evaluate(\Closure $callback, Array $args = []) {
    $args = array_values($args);
    $func = new \ReflectionFunction($callback);
    $filename = $func->getFileName();
    $start_line = $func->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block
    $end_line = $func->getEndLine();
    $length = $end_line - $start_line;

    $source = file($filename);
    $body = array_slice($source, $start_line, $length);

    $col = strpos($body[0], 'function');
    $body[0] = substr($body[0], $col);

    $last = count($body) - 1;
    $col = strpos($body[$last], '}') + 1;
    $body[$last] = substr($body[$last], 0, $col);

    $code = ['<?php'];
    $calling_args = [];
    foreach ($func->getParameters() as $i => $param) {
      $code[] = '$' . $param->name . ' = ' . var_export($args[$i], TRUE) . ';';
      $calling_args[] = '$' . $param->name;
    }

    $code[] = '$evaluation = ' . implode("", $body) . ';';
    $code[] = 'echo json_encode($evaluation(' . implode(', ', $calling_args) . '));';

    $transfer = base64_encode(implode(PHP_EOL, $code));

    $execution = [];
    // Linux uses tempfile while OSX uses mktemp.
    $execution[] = 't=`which tempfile || which mktemp`; f=$($t)';
    $execution[] = "echo $transfer | base64 --decode > \$f";

    $pipe = implode(';' . PHP_EOL, $execution);

    $execution[] = strtr('@bin @alias @options scr $f', [
      '@options' => implode(' ', $this->getOptions()),
      '@alias' => $this->getAlias(),
      '@bin' => $this->drushBin,
    ]);
    $execution[] = 'rm $f';

    $transfer = implode(';' . PHP_EOL, $execution);

    $output = $this->target->exec($transfer);
    return json_decode($output, TRUE);
  }
596315
protected function executeCallback($callback, $args)
    {
        if (is_string($callback)) {
            return $this->callStringCallback($callback, $args);
        } elseif ($callback instanceof Closure) {
            return call_user_func_array($callback, $args);
        }

        return '';
    }
301561
public function executeAndClose(Closure $closure, ?string $connection = null)
	{
		$returnValue = $closure($this->connection($connection));

		$this->close($connection);

		return $returnValue;
	}
941554
public static function call($function, \Closure $callback = null)
    {
        $result = null;
        if (is_array($function)) {
            $result = self::methodDI($function, $callback);
        } elseif (is_callable($function, true)) {
            $result = self::functionDI($function, $callback);
        }
        return $result;
    }
1084637
public function each(\Closure $closure): self
    {
        // init
        $array = [];

        foreach ($this->getGenerator() as $key => $value) {
            $array[$key] = $closure($value, $key);
        }

        return static::create(
            $array,
            $this->iteratorClass,
            false
        );
    }
1060655
private function guess(\Closure $closure): Guess
    {
        $guesses = [];

        foreach ($this->guessers as $guesser) {
            if ($guess = $closure($guesser)) {
                $guesses[] = $guess;
            }
        }

        return Guess::getBestGuess($guesses);
    }
790013
public function call(callable $callable, array $args = [])
    {
        if (is_string($callable) && strpos($callable, '::') !== false) {
            $callable = explode('::', $callable);
        }

        if (is_array($callable)) {
            if (is_string($callable[0])) {
                $callable[0] = $this->getContainer()->get($callable[0]);
            }

            $reflection = new ReflectionMethod($callable[0], $callable[1]);

            if ($reflection->isStatic()) {
                $callable[0] = null;
            }

            return $reflection->invokeArgs($callable[0], $this->reflectArguments($reflection, $args));
        }

        if (is_object($callable)) {
            $reflection = new ReflectionMethod($callable, '__invoke');

            return $reflection->invokeArgs($callable, $this->reflectArguments($reflection, $args));
        }

        $reflection = new ReflectionFunction($callable);

        return $reflection->invokeArgs($this->reflectArguments($reflection, $args));
    }
797435
public static function captureQueries(\Closure $closure)
    {
        DB::enableQueryLog();
        $closure();
        $logs = DB::getQueryLog();
        return array_map(function ($log) {
            return static::inlineBindings($log['query'], $log['bindings']);
        }, $logs);
    }
1178071
public function compose(array $closures, array $arguments = [])
    {
        $result = call_user_func_array(array_shift($closures), $arguments);

        foreach ($closures as $closure)
        {
            $result = $closure($result);
        }

        return $result;
    }