本文整理汇总了PHP中ReflectionFunction类的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionFunction类的具体用法?PHP ReflectionFunction怎么用?PHP ReflectionFunction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ReflectionFunction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: export
protected function export($var, $return = false)
{
if ($var instanceof Closure) {
/* dump anonymous function in to plain code.*/
$ref = new ReflectionFunction($var);
$file = new SplFileObject($ref->getFileName());
$file->seek($ref->getStartLine() - 1);
$result = '';
while ($file->key() < $ref->getEndLine()) {
$result .= $file->current();
$file->next();
}
$begin = strpos($result, 'function');
$end = strrpos($result, '}');
$result = substr($result, $begin, $end - $begin + 1);
} elseif (is_object($var)) {
/* dump object with construct function. */
$result = 'new ' . get_class($var) . '(' . $this->export(get_object_vars($var), true) . ')';
} elseif (is_array($var)) {
/* dump array in plain array.*/
$array = array();
foreach ($var as $k => $v) {
$array[] = var_export($k, true) . ' => ' . $this->export($v, true);
}
$result = 'array(' . implode(', ', $array) . ')';
} else {
$result = var_export($var, true);
}
if (!$return) {
print $result;
}
return $result;
}
示例2: wrapClosures
/**
* Recursively traverses and wraps all Closure objects within the value.
*
* NOTE: THIS MAY NOT WORK IN ALL USE CASES, SO USE AT YOUR OWN RISK.
*
* @param mixed $data Any variable that contains closures.
* @param SerializerInterface $serializer The serializer to use.
*/
public static function wrapClosures(&$data, SerializerInterface $serializer)
{
if ($data instanceof \Closure) {
// Handle and wrap closure objects.
$reflection = new \ReflectionFunction($data);
if ($binding = $reflection->getClosureThis()) {
self::wrapClosures($binding, $serializer);
$scope = $reflection->getClosureScopeClass();
$scope = $scope ? $scope->getName() : 'static';
$data = $data->bindTo($binding, $scope);
}
$data = new SerializableClosure($data, $serializer);
} elseif (is_array($data) || $data instanceof \stdClass || $data instanceof \Traversable) {
// Handle members of traversable values.
foreach ($data as &$value) {
self::wrapClosures($value, $serializer);
}
} elseif (is_object($data) && !$data instanceof \Serializable) {
// Handle objects that are not already explicitly serializable.
$reflection = new \ReflectionObject($data);
if (!$reflection->hasMethod('__sleep')) {
foreach ($reflection->getProperties() as $property) {
if ($property->isPrivate() || $property->isProtected()) {
$property->setAccessible(true);
}
$value = $property->getValue($data);
self::wrapClosures($value, $serializer);
$property->setValue($data, $value);
}
}
}
}
示例3: getReflectedMinimumParams
function getReflectedMinimumParams($name)
{
// Reflection gets this one wrong.
if (strcasecmp($name, 'define') == 0) {
return 2;
}
if (strcasecmp($name, 'strtok') == 0) {
return 1;
}
if (strcasecmp($name, 'implode') == 0) {
return 1;
}
if (strcasecmp($name, "sprintf") == 0) {
return 1;
}
if (strcasecmp($name, "array_merge") == 0) {
return 1;
}
if (strcasecmp($name, "stream_set_timeout") == 0) {
return 2;
}
try {
$func = new \ReflectionFunction($name);
return $func->getNumberOfRequiredParameters();
} catch (\ReflectionException $e) {
return -1;
}
}
示例4: strategyFactory
/**
* @param array $defaultStrategies
* @param array $strategyMap
* @param \FusePump\Cli\Inputs $inputs
* @param array $arguments
* @return callable
*/
function strategyFactory($defaultStrategies, $strategyMap = array(), $inputs = null, $arguments = array())
{
$strategyList = array(function () {
return false;
});
$addStrategy = function ($strategy) use(&$strategyList, $arguments) {
$args = array();
$reflection = new \ReflectionFunction($strategy);
foreach ($reflection->getParameters() as $param) {
if ($param->getName() === 'patchFile') {
continue;
} elseif ($param->getName() === 'superStrategy') {
$args[] = end($strategyList);
} elseif (array_key_exists($param->getName(), $arguments)) {
$args[] = $arguments[$param->getName()];
} else {
$args[] = null;
}
}
$strategyList[] = function ($patchFile) use($strategy, $args) {
return call_user_func_array($strategy, array_merge(array($patchFile), $args));
};
};
foreach ($strategyMap as $option => $strategy) {
if ($inputs->get($option)) {
$addStrategy($strategy);
}
}
if (count($strategyList) < 2) {
foreach ($defaultStrategies as $strategy) {
$addStrategy($strategy);
}
}
return end($strategyList);
}
示例5: call_closure
/**
* Call closure.
*
* @param mixed $closure
* @param array $parameters
*
* @return Closure
*/
protected function call_closure($closure, array $parameters = [])
{
if ($closure instanceof Closure) {
$rc = new ReflectionFunction($closure);
$args = $rc->getParameters();
$params = $parameters;
$classes = [$this->get_class_prefix(get_class($this)), get_class($this), get_parent_class($this)];
foreach ($args as $index => $arg) {
if ($arg->getClass() === null) {
continue;
}
if (in_array($arg->getClass()->name, $classes)) {
$parameters[$index] = $this;
} else {
if ($this->exists($arg->getClass()->name)) {
$parameters[$index] = $this->make($arg->getClass()->name);
}
}
}
if (!empty($args) && empty($parameters)) {
$parameters[0] = $this;
}
if (count($args) > count($parameters)) {
$parameters = array_merge($parameters, $params);
}
return $this->call_closure(call_user_func_array($closure, $parameters), $parameters);
}
return $closure;
}
示例6: import
public function import(\ReflectionFunction $function)
{
$this->name = $function->name;
$this->function = $function->getShortName();
$this->namespace = $function->getNamespaceName();
$this->_importFromReflection($function);
}
示例7: shouldRun
private function shouldRun($callback, $controllerResult)
{
if (is_array($callback)) {
$callbackReflection = new \ReflectionMethod($callback[0], $callback[1]);
} elseif (is_object($callback) && !$callback instanceof \Closure) {
$callbackReflection = new \ReflectionObject($callback);
$callbackReflection = $callbackReflection->getMethod('__invoke');
} else {
$callbackReflection = new \ReflectionFunction($callback);
}
if ($callbackReflection->getNumberOfParameters() > 0) {
$parameters = $callbackReflection->getParameters();
$expectedControllerResult = $parameters[0];
if ($expectedControllerResult->getClass() && (!is_object($controllerResult) || !$expectedControllerResult->getClass()->isInstance($controllerResult))) {
return false;
}
if ($expectedControllerResult->isArray() && !is_array($controllerResult)) {
return false;
}
if (method_exists($expectedControllerResult, 'isCallable') && $expectedControllerResult->isCallable() && !is_callable($controllerResult)) {
return false;
}
}
return true;
}
示例8: execute
/**
* Execute handler
* @param Client $client
* @param $data
* @return mixed|null
*/
public function execute(Client $client, $data)
{
$this->_client = $client;
$this->_data = $data;
// Execute handler object
if (!empty($this->_object)) {
$class = new ReflectionClass(get_class($this->_object));
$method = $class->getMethod($this->_method);
$parameters = $this->prepareParameters($method->getParameters());
if (empty($parameters)) {
$parameters[] = $data;
}
return call_user_func_array([$this->_object, $this->_method], $parameters);
}
// Execute closure handler
if (!empty($this->_closure)) {
$function = new \ReflectionFunction($this->_closure);
$parameters = $this->prepareParameters($function->getParameters());
if (empty($parameters)) {
$parameters[] = $data;
}
return call_user_func_array($this->_closure, $parameters);
}
return null;
}
示例9: run
/**
*
* @param Model_Job $job
* @return void
*/
public function run(Model_Job $job)
{
if (Kohana::$profiling === TRUE) {
$benchmark = Profiler::start('Rub job', $job->name);
}
$this->values(array('job_id' => $job->id))->create();
$this->set_status(Model_Job::STATUS_RUNNING);
try {
$job = $job->job;
$minion_task = Minion_Task::convert_task_to_class_name($job);
if (is_array($job)) {
$passed = call_user_func($job);
} elseif (class_exists($minion_task)) {
Minion_Task::factory(array($job))->execute();
$passed = TRUE;
} elseif (strpos($job, '::') === FALSE) {
$function = new ReflectionFunction($job);
$passed = $function->invoke();
} else {
list($class, $method) = explode('::', $job, 2);
$method = new ReflectionMethod($class, $method);
$passed = $method->invoke(NULL);
}
} catch (Exception $e) {
$this->failed();
return;
}
$this->complete();
if (isset($benchmark)) {
Profiler::stop($benchmark);
}
}
示例10: params2MethodArgs
function params2MethodArgs($method, $params)
{
$funcInfo = new ReflectionFunction($method);
$args = array();
foreach ($funcInfo->getParameters() as $paramInfo) {
$nm = $paramInfo->getName();
$val = null;
if (!array_key_exists($nm, $params)) {
if ($paramInfo->isDefaultValueAvailable()) {
$val = $paramInfo->getDefaultValue();
}
if (is_null($val) && !$paramInfo->isOptional()) {
throw new Exception("Parameter [{$nm}] of [{$method}] can not be null");
}
} else {
$val = $params[$nm];
}
$classInfo = $paramInfo->getClass();
if ($classInfo) {
$val = eval("return new " . $classInfo->getName() . "('{$val}');");
}
$args[] = $val;
}
return $args;
}
示例11: parse
public function parse(&$var, Kint_Object &$o)
{
if (!$var instanceof Closure || !$o instanceof Kint_Object_Instance || !$this->parseChildren($o)) {
return;
}
$o = $o->transplant(new Kint_Object_Closure());
$o->removeRepresentation('properties');
$closure = new ReflectionFunction($var);
$o->filename = $closure->getFileName();
$o->startline = $closure->getStartLine();
foreach ($closure->getParameters() as $param) {
$o->parameters[] = new Kint_Object_Parameter($param);
}
$p = new Kint_Object_Representation('Parameters');
$p->contents =& $o->parameters;
$o->addRepresentation($p, 0);
$statics = array();
if (method_exists($closure, 'getClosureThis') && ($v = $closure->getClosureThis())) {
$statics = array('this' => $v);
}
if (count($statics = $statics + $closure->getStaticVariables())) {
foreach ($statics as $name => &$static) {
$obj = Kint_Object::blank('$' . $name);
$obj->depth = $o->depth + 1;
$static = $this->parser->parse($static, $obj);
if ($static->value === null) {
$static->access_path = null;
}
}
$r = new Kint_Object_Representation('Uses');
$r->contents = $statics;
$o->addRepresentation($r, 0);
}
}
示例12: debugBacktrace
/**
* 代码执行过程回溯信息
*
* @static
* @access public
*/
public static function debugBacktrace()
{
$skipFunc[] = 'Error->debugBacktrace';
$show = $log = '';
$debugBacktrace = debug_backtrace();
ksort($debugBacktrace);
foreach ($debugBacktrace as $k => $error) {
if (!isset($error['file'])) {
try {
if (isset($error['class'])) {
$reflection = new \ReflectionMethod($error['class'], $error['function']);
} else {
$reflection = new \ReflectionFunction($error['function']);
}
$error['file'] = $reflection->getFileName();
$error['line'] = $reflection->getStartLine();
} catch (Exception $e) {
continue;
}
}
$file = str_replace(rootPath(), '', $error['file']);
$func = isset($error['class']) ? $error['class'] : '';
$func .= isset($error['type']) ? $error['type'] : '';
$func .= isset($error['function']) ? $error['function'] : '';
if (in_array($func, $skipFunc)) {
break;
}
$error['line'] = sprintf('%04d', $error['line']);
$show .= '<li>[Line: ' . $error['line'] . ']' . $file . '(' . $func . ')</li>';
$log .= !empty($log) ? ' -> ' : '';
$log .= $file . ':' . $error['line'];
}
return array($show, $log);
}
示例13: test
function test()
{
$x = new ReflectionFunction('array_filter');
$params = $x->getParameters();
$p1 = $params[1];
var_dump($p1->getDefaultValueText());
}
示例14: call
public function call($group, $hook, $parameters = null, $action = null)
{
if (!isset($action)) {
$action = 'execute';
}
if (!isset($this->hooks[$this->site][$group][$hook][$action])) {
$this->register($group, $hook, $action);
}
$calls = [];
if (isset($this->hooks[$this->site][$group][$hook][$action])) {
$calls = $this->hooks[$this->site][$group][$hook][$action];
}
if (isset($this->watches[$this->site][$group][$hook][$action])) {
$calls = array_merge($calls, $this->watches[$this->site][$group][$hook][$action]);
}
$result = [];
foreach ($calls as $code) {
$bait = null;
if (is_string($code)) {
$class = Apps::getModuleClass($code, 'Hooks');
$obj = new $class();
$bait = $obj->{$action}($parameters);
} else {
$ref = new \ReflectionFunction($code);
if ($ref->isClosure()) {
$bait = $code($parameters);
}
}
if (!empty($bait)) {
$result[] = $bait;
}
}
return $result;
}
示例15: updateClassMethodHash
public static function updateClassMethodHash($className, $methodName, $closure)
{
$methodName = strtolower($methodName);
if (isset(self::$specialMethods[$methodName]) && self::$specialMethods[$methodName] == 1) {
$methodName = "phlexmock_" . $methodName;
}
$closureRF = new \ReflectionFunction($closure);
$paramStr = "()";
$params = [];
$closureParams = $closureRF->getParameters();
if (count($closureParams) > 0) {
foreach ($closureParams as $closureParam) {
$params[] = '$' . $closureParam->getName();
}
$paramStr = "(" . implode(",", $params) . ")";
}
$sl = $closureRF->getStartLine();
$el = $closureRF->getEndLine();
$closureContainerScript = $closureRF->getFileName();
if (!isset(self::$closureContainerScriptLines[$closureContainerScript])) {
self::$closureContainerScriptLines[$closureContainerScript] = explode("\n", file_get_contents($closureRF->getFileName()));
}
$lines = self::$closureContainerScriptLines[$closureContainerScript];
$code = '$func = function' . $paramStr . ' { ' . implode("\n", array_slice($lines, $sl, $el - $sl - 1)) . ' };';
self::$classMethodHash[$className][$methodName] = $code;
}