本文整理汇总了PHP中ReflectionMethod::getClosure方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionMethod::getClosure方法的具体用法?PHP ReflectionMethod::getClosure怎么用?PHP ReflectionMethod::getClosure使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionMethod
的用法示例。
在下文中一共展示了ReflectionMethod::getClosure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: closure
/**
* @param mixed class, object, callable
* @param string method
* @return \Closure
*/
public static function closure($callable, $m = NULL)
{
if ($m !== NULL) {
$callable = array($callable, $m);
} elseif (is_string($callable) && count($tmp = explode('::', $callable)) === 2) {
$callable = $tmp;
} elseif ($callable instanceof \Closure) {
return $callable;
} elseif (is_object($callable)) {
$callable = array($callable, '__invoke');
}
if (PHP_VERSION_ID >= 50400) {
if (is_string($callable) && function_exists($callable)) {
$r = new \ReflectionFunction($callable);
return $r->getClosure();
} elseif (is_array($callable) && method_exists($callable[0], $callable[1])) {
$r = new \ReflectionMethod($callable[0], $callable[1]);
return $r->getClosure($callable[0]);
}
}
self::check($callable);
$_callable_ = $callable;
return function () use($_callable_) {
return call_user_func_array($_callable_, func_get_args());
};
}
示例2: exec
/**
* Execute command
*
* @return int|mixed
* @throws \Exception
*/
public function exec()
{
$args = func_get_args();
if (!$this->cmd) {
throw new FWException('no cmd provided', 1);
}
$build = new ProcessBuilder();
$pro = $build->setPrefix($this->cmd)->setArguments($args)->getProcess();
$pro->setTimeout($this->timeout);
$rm = new \ReflectionMethod($this, 'output');
$func = $rm->getClosure($this);
$re = 0;
if ($this->async) {
$pro->start($func);
} else {
try {
$pro->mustRun($func);
$this->out = $pro->getOutput();
$re = $this->out;
} catch (ProcessFailedException $e) {
getLog()->error($e->getMessage());
$re = 1;
}
}
return $re;
}
示例3: __construct
public function __construct(LoggerInterface $logger)
{
$build = new \ReflectionMethod($this, 'buildCallback');
$run = new \ReflectionMethod($this, 'runCallback');
$this->buildCallback = $build->getClosure($this);
$this->runCallback = $run->getClosure($this);
$this->logger = $logger;
}
示例4: method
public function method($controller_root, $method)
{
$controller = $this->controller;
$class = $this->class;
$controller_model_name = $controller_root . '\\' . $class;
$func = new \ReflectionMethod($controller_model_name, $method);
$Closure = $func->getClosure(new $controller_model_name());
$controller->{$class}()->Methods[$method] = $Closure->bindTo($controller->{$class}(), $controller->{$class}());
return $controller->{$class}()->Methods[$method];
}
示例5: execute
function execute($class_name, $method_name, $instance)
{
try {
$ref = new ReflectionMethod($class_name, $method_name);
$method = $ref->getClosure($instance);
var_dump($method());
} catch (Exception $e) {
$c = get_class($e);
echo "{$c}: {$e->getMessage()}\n";
}
}
示例6: __construct
public function __construct()
{
if (PHP_VERSION_ID >= 50400) {
if (is_int($code = http_response_code())) {
$this->code = $code;
}
}
if (PHP_VERSION_ID >= 50401) {
// PHP bug #61106
$rm = new \ReflectionMethod('Nette\\Http\\Helpers::removeDuplicateCookies');
header_register_callback($rm->getClosure());
// requires closure due PHP bug #66375
}
}
示例7: addMethod
/**
* Extracts the named method from the supplied object and attaches
* it to the skeleton instance
*
* @param object $object
* @param string $methodName
* @param string $newMethodName - optional: attach the method under a new name
* @throws InvalidObjectException When the $object param isn't actually an object
* @throws InvalidMethodException When the named method is not callable
* @return \Vanqard\Frankenbuilder\Builder fluent interface
*/
public function addMethod($object, $methodName, $newMethodName = null)
{
if (!is_object($object)) {
throw new InvalidObjectException('First parameter must be an object instance');
}
if (!is_callable(array($object, $methodName))) {
throw new InvalidMethodException('The named method must be callable on the supplied object');
}
$methodInstance = new \ReflectionMethod($object, $methodName);
$limb = $methodInstance->getClosure($object);
$limbName = !is_null($newMethodName) ? $newMethodName : $methodName;
$this->addToSkeleton($limbName, $limb);
return $this;
}
示例8: call
/**
* Call a action of controller
*
* @access public
* @param MVC $mvc MVC Application object
* @param string $method Method or Function of the Class Controller
* @param string $fileView String of the view file
* @return array Response array
* @throws \LogicException
*/
public final function call(MVC $mvc, $method, $fileView = null)
{
if (!method_exists($this, $method)) {
throw new \LogicException(sprintf('Method "s" don\'t exists.', $method));
}
# Replace the view object
$this->view = $mvc->view();
# Arguments of method
$arguments = array();
# Create a reflection method
$reflectionMethod = new \ReflectionMethod(get_class($this), $method);
$reflectionParams = $reflectionMethod->getParameters();
foreach ($reflectionParams as $param) {
if ($paramClass = $param->getClass()) {
$className = $paramClass->name;
if ($className === 'MVC\\MVC' || $className === '\\MVC\\MVC') {
$arguments[] = $mvc;
} elseif ($className === 'MVC\\Server\\HttpRequest' || $className === '\\MVC\\Server\\HttpRequest') {
$arguments[] = $mvc->request();
}
} else {
foreach ($mvc->request()->params as $keyReqParam => $valueReqParam) {
if ($param->name === $keyReqParam) {
$arguments[] = $valueReqParam;
break;
}
}
}
}
$response = call_user_func_array($reflectionMethod->getClosure($this), $arguments);
if (empty($response)) {
throw new \LogicException('Response null returned.');
}
if (is_string($response)) {
$this->response['body'] = $response;
} elseif ($mvc->request()->isAjax()) {
$this->response['body'] = $this->renderJson($response);
} elseif (is_array($response)) {
if (!$fileView) {
throw new \LogicException('File view is null.');
}
$class = explode("\\", get_called_class());
$classname = end($class);
// Class without Controller
$classname = str_replace('Controller', '', $classname);
$file = $classname . "/{$fileView}";
$this->response['body'] = $this->renderHtml($file, $response);
}
return $this->response;
}
示例9: loadTable
public static function loadTable($name, $db_name = db_name)
{
if (isset(self::$tables[$db_name][$name])) {
return self::$tables[$db_name][$name];
}
$blackList = array("index", "foreign", "unique");
$sName = "\\database\\{$db_name}\\{$name}";
if (class_exists($sName)) {
$table_load = new $sName();
foreach ($table_load as $key => $value) {
if (!preg_grep("/^{$key}\$/", $blackList)) {
$keys = array_keys($value);
$values = array_values($value);
$array = array();
foreach ($keys as $k => $v) {
if (is_int($v)) {
$keys[$k] = $values[$k];
$values[$k] = true;
}
}
if (method_exists($table_load, $key)) {
$options = new \lib\sql\options();
$func = new \ReflectionMethod($sName, $key);
$Closure = $func->getClosure($table_load);
$options->{$key} = \Closure::bind($Closure, $options);
$options->table = $table_load;
$options->tableName = $table_load;
$options->fieldName = $key;
$values[] = $options;
$keys[] = 'closure';
}
$array = array_combine($keys, $values);
$table_load->{$key} = (object) $array;
}
}
foreach ($table_load as $key => $value) {
if (method_exists($table_load, $key)) {
if (isset($table_load->{$key}->closure)) {
$closure = $table_load->{$key}->closure;
call_user_func($closure->{$key});
}
}
}
self::$tables[$db_name][$name] = $table_load;
return $table_load;
}
return null;
}
示例10: testExpandClasses
public function testExpandClasses()
{
$r = new \ReflectionClass(AddClassesToCachePass::class);
$pass = $r->newInstanceWithoutConstructor();
$r = new \ReflectionMethod(AddClassesToCachePass::class, 'expandClasses');
$expand = $r->getClosure($pass);
$this->assertSame('Foo', $expand(array('Foo'), array())[0]);
$this->assertSame('Foo', $expand(array('\\Foo'), array())[0]);
$this->assertSame('Foo', $expand(array('Foo'), array('\\Foo'))[0]);
$this->assertSame('Foo', $expand(array('Foo'), array('Foo'))[0]);
$this->assertSame('Foo', $expand(array('\\Foo'), array('\\Foo\\Bar'))[0]);
$this->assertSame('Foo', $expand(array('Foo'), array('\\Foo\\Bar'))[0]);
$this->assertSame('Foo', $expand(array('\\Foo'), array('\\Foo\\Bar\\Acme'))[0]);
$this->assertSame('Foo\\Bar', $expand(array('Foo\\'), array('\\Foo\\Bar'))[0]);
$this->assertSame('Foo\\Bar\\Acme', $expand(array('Foo\\'), array('\\Foo\\Bar\\Acme'))[0]);
$this->assertEmpty($expand(array('Foo\\'), array('\\Foo')));
$this->assertSame('Acme\\Foo\\Bar', $expand(array('**\\Foo\\'), array('\\Acme\\Foo\\Bar'))[0]);
$this->assertEmpty($expand(array('**\\Foo\\'), array('\\Foo\\Bar')));
$this->assertEmpty($expand(array('**\\Foo\\'), array('\\Acme\\Foo')));
$this->assertEmpty($expand(array('**\\Foo\\'), array('\\Foo')));
$this->assertSame('Acme\\Foo', $expand(array('**\\Foo'), array('\\Acme\\Foo'))[0]);
$this->assertEmpty($expand(array('**\\Foo'), array('\\Acme\\Foo\\AcmeBundle')));
$this->assertEmpty($expand(array('**\\Foo'), array('\\Acme\\FooBar\\AcmeBundle')));
$this->assertSame('Foo\\Acme\\Bar', $expand(array('Foo\\*\\Bar'), array('\\Foo\\Acme\\Bar'))[0]);
$this->assertEmpty($expand(array('Foo\\*\\Bar'), array('\\Foo\\Acme\\Bundle\\Bar')));
$this->assertSame('Foo\\Acme\\Bar', $expand(array('Foo\\**\\Bar'), array('\\Foo\\Acme\\Bar'))[0]);
$this->assertSame('Foo\\Acme\\Bundle\\Bar', $expand(array('Foo\\**\\Bar'), array('\\Foo\\Acme\\Bundle\\Bar'))[0]);
$this->assertSame('Acme\\Bar', $expand(array('*\\Bar'), array('\\Acme\\Bar'))[0]);
$this->assertEmpty($expand(array('*\\Bar'), array('\\Bar')));
$this->assertEmpty($expand(array('*\\Bar'), array('\\Foo\\Acme\\Bar')));
$this->assertSame('Foo\\Acme\\Bar', $expand(array('**\\Bar'), array('\\Foo\\Acme\\Bar'))[0]);
$this->assertSame('Foo\\Acme\\Bundle\\Bar', $expand(array('**\\Bar'), array('\\Foo\\Acme\\Bundle\\Bar'))[0]);
$this->assertEmpty($expand(array('**\\Bar'), array('\\Bar')));
$this->assertSame('Foo\\Bar', $expand(array('Foo\\*'), array('\\Foo\\Bar'))[0]);
$this->assertEmpty($expand(array('Foo\\*'), array('\\Foo\\Acme\\Bar')));
$this->assertSame('Foo\\Bar', $expand(array('Foo\\**'), array('\\Foo\\Bar'))[0]);
$this->assertSame('Foo\\Acme\\Bar', $expand(array('Foo\\**'), array('\\Foo\\Acme\\Bar'))[0]);
$this->assertSame(array('Foo\\Bar'), $expand(array('Foo\\*'), array('Foo\\Bar', 'Foo\\BarTest')));
$this->assertSame(array('Foo\\Bar', 'Foo\\BarTest'), $expand(array('Foo\\*', 'Foo\\*Test'), array('Foo\\Bar', 'Foo\\BarTest')));
$this->assertSame('Acme\\FooBundle\\Controller\\DefaultController', $expand(array('**Bundle\\Controller\\'), array('\\Acme\\FooBundle\\Controller\\DefaultController'))[0]);
$this->assertSame('FooBundle\\Controller\\DefaultController', $expand(array('**Bundle\\Controller\\'), array('\\FooBundle\\Controller\\DefaultController'))[0]);
$this->assertSame('Acme\\FooBundle\\Controller\\Bar\\DefaultController', $expand(array('**Bundle\\Controller\\'), array('\\Acme\\FooBundle\\Controller\\Bar\\DefaultController'))[0]);
$this->assertSame('Bundle\\Controller\\Bar\\DefaultController', $expand(array('**Bundle\\Controller\\'), array('\\Bundle\\Controller\\Bar\\DefaultController'))[0]);
$this->assertSame('Acme\\Bundle\\Controller\\Bar\\DefaultController', $expand(array('**Bundle\\Controller\\'), array('\\Acme\\Bundle\\Controller\\Bar\\DefaultController'))[0]);
$this->assertSame('Foo\\Bar', $expand(array('Foo\\Bar'), array())[0]);
$this->assertSame('Foo\\Acme\\Bar', $expand(array('Foo\\**'), array('\\Foo\\Acme\\Bar'))[0]);
}
示例11: load
/**
* Add routes to an App from this class by inferring details from PHPDoc @url and @method properties
* @param App $app
*/
public function load(App $app)
{
$class = get_called_class();
$methods = get_class_methods($class);
foreach ($methods as $class_method) {
$method_props = [];
$rm = new \ReflectionMethod($this, $class_method);
$phpdoc = preg_split('/\\r\\n|\\n|\\r/', preg_replace('%^\\s*/?\\*+((?<=\\*)/|[ \\t]*)%m', '', $rm->getDocComment()));
foreach ($phpdoc as $docline) {
if (preg_match('#^@(url|method)\\s+(\\S+)$#', $docline, $matches)) {
$method_props[$matches[1]] = $matches[2];
}
}
if (isset($method_props['url'])) {
$route = $app->route($class . '::' . $class_method, $method_props['url'], $rm->getClosure($this));
if (isset($method_props['method'])) {
$route->via($method_props['method']);
}
}
}
}
示例12: castToClosure
/**
* Casts the given callable to Closure
* @param callable $callable
* @return \Closure
* @throws \InvalidArgumentException
*/
protected function castToClosure($callable)
{
if (!is_callable($callable)) {
throw new \InvalidArgumentException('Argument 1 must be callable, ' . gettype($callable) . ' given.');
}
$result = null;
if (is_object($callable)) {
if ($callable instanceof \Closure) {
$result = $callable;
} else {
if (method_exists($callable, '__invoke')) {
$r = new \ReflectionObject($callable);
$result = $r->getMethod('__invoke')->getClosure($callable);
}
}
} else {
try {
$r = new \ReflectionMethod($callable);
$result = $r->getClosure();
} catch (\Exception $e) {
try {
$r = new \ReflectionFunction($callable);
$result = $r->getClosure();
} catch (\Exception $e) {
if (is_array($callable)) {
$r = new \ReflectionObject($callable[0]);
$result = $r->getMethod($callable[1])->getClosure($callable[0]);
}
}
}
}
if ($result === null) {
throw new \InvalidArgumentException('Unsupported callable given.');
}
return $result;
}
示例13: getClosure
/**
* Returns the function/method as closure.
*
* @param object $object Object
*
* @return \Closure
*/
public function getClosure($object)
{
if (PHP_VERSION >= 50400) {
return parent::getClosure();
} else {
$that = $this;
return function () use($object, $that) {
return $that->invokeArgs($object, func_get_args());
};
}
}
示例14: getClosure
/**
* @return Closure
*/
public function getClosure()
{
if ($this->closure instanceof \Closure) {
return $this->closure;
}
$reflect = new \ReflectionMethod($this->closure);
return $reflect->getClosure();
}
示例15: yolisp
function yolisp($swag, array &$env = [])
{
static $OP_CACHE = [];
// HAH! Take that Zend!
if (!$swag instanceof cons) {
// implicitly quote non-strings
if (!is_string($swag)) {
return $swag;
}
// lookup in environment
if (array_key_exists($swag, $env)) {
return $env[$swag];
} else {
if (isset($OP_CACHE[$swag])) {
return $OP_CACHE[$swag];
} else {
if (array_key_exists($swag, DEFAULT_ENV)) {
$callable = DEFAULT_ENV[$swag];
if (is_array($callable)) {
return $OP_CACHE[$swag] = (new \ReflectionMethod(...$callable))->getClosure();
} else {
if (is_string($callable)) {
return $OP_CACHE[$swag] = (new \ReflectionFunction($callable))->getClosure();
} else {
return $callable;
}
}
} else {
if (function_exists($swag)) {
return $OP_CACHE[$swag] = (new \ReflectionFunction($swag))->getClosure();
// we do class lookup after function lookup because everyone knows functional programming is superior
// what did you expect? this is yolisp, not yojava
} else {
if (class_exists($swag)) {
return $swag;
} else {
if (array_key_exists($swag, OPS)) {
$format = OPS[$swag];
$ops = substr_count($format, '$');
$ops += $optional_ops = substr_count($format, '?');
if ($ops === 0) {
throw new \Exception("Invalid operator format string: \"{$format}\"");
}
$param_names = [];
for ($i = 0; $i < $ops; $i++) {
$param_names[] = '$op' . $i;
}
$param_list = '';
for ($i = 0; $i < $ops; $i++) {
if ($i !== 0) {
$param_list .= ', ';
}
$param_list .= $param_names[$i];
if ($i >= $ops - $optional_ops) {
$param_list .= ' = NULL';
}
}
$parts = explode(' ', $format);
if ($optional_ops) {
$optionless_expr = '';
$i = 0;
foreach ($parts as $part) {
if ($part === '?') {
$optionless_expr .= ' ';
} else {
if ($part === '$') {
$optionless_expr .= ' ' . $param_names[$i];
$i++;
} else {
$optionless_expr .= ' ' . $part;
}
}
}
}
$expr = '';
$i = 0;
foreach ($parts as $part) {
if ($part === '?' || $part === '$') {
$expr .= ' ' . $param_names[$i];
$i++;
} else {
$expr .= ' ' . $part;
}
}
if ($optional_ops) {
$body = "if (func_num_args() < {$ops}) { return {$optionless_expr}; } else { return {$expr}; }";
} else {
$body = "return {$expr};";
}
// And people said eval() and create_function() were evil!
return $OP_CACHE[$swag] = create_function($param_list, $body);
} else {
throw new \Exception("Could not find {$swag} in environment");
}
}
}
}
}
}
}
//.........这里部分代码省略.........