function XLLoop_reflection_handler() { try { $input = file_get_contents('php://input'); $value = json_decode($input); if ($value != NULL) { $argc = count($value->args); $args = array(); for ($i = 0; $i < $argc; $i++) { $args[$i] = XLLoop_decode($value->args[$i]); } $function = new ReflectionFunction($value->name); if ($function->isUserDefined()) { $reqArgc = $function->getNumberOfParameters(); for ($i = $argc; $i < $reqArgc; $i++) { $args[$i] = 0; } $result = $function->invokeArgs($args); $enc = XLLoop_encode($result); print json_encode($enc); } else { print json_encode(XLLoop_encode("#Function " . $value->name . "() does not exist")); } } else { print "<html><body><code>XLLoop function handler alive</code></body></html>"; } } catch (Exception $e) { print json_encode(XLLoop_encode("#" . $e->getMessage())); } }
/** * Returns whether this is a user-defined function * * @return boolean True if this is a user-defined function */ public function isUserDefined() { if ($this->reflectionSource instanceof ReflectionFunction) { return $this->reflectionSource->isUserDefined(); } else { return parent::isUserDefined(); } }
function dumpFuncInfo($name) { $funcInfo = new ReflectionFunction($name); var_dump($funcInfo->getName()); var_dump($funcInfo->isInternal()); var_dump($funcInfo->isUserDefined()); var_dump($funcInfo->getStartLine()); var_dump($funcInfo->getEndLine()); var_dump($funcInfo->getStaticVariables()); }
public function startTestSuite(\PHPUnit_Framework_TestSuite $mainSuite) { if (null !== self::$enabledPolyfills) { return; } self::$enabledPolyfills = false; foreach ($mainSuite->tests() as $suite) { $testClass = $suite->getName(); if (!($tests = $suite->tests())) { continue; } if (!preg_match('/^(.+)\\\\Tests(\\\\.*)Test$/', $testClass, $m)) { $mainSuite->addTest(self::warning('Unknown naming convention for ' . $testClass)); continue; } $testedClass = new \ReflectionClass($m[1] . $m[2]); $bootstrap = new \SplFileObject(dirname($testedClass->getFileName()) . '/bootstrap.php'); $warnings = array(); $defLine = null; foreach (new \RegexIterator($bootstrap, '/return p\\\\' . $testedClass->getShortName() . '::/') as $defLine) { if (!preg_match('/^\\s*function (?P<name>[^\\(]++)(?P<signature>\\([^\\)]*+\\)) \\{ (?<return>return p\\\\' . $testedClass->getShortName() . '::[^\\(]++)(?P<args>\\([^\\)]*+\\)); \\}$/', $defLine, $f)) { $warnings[] = self::warning('Invalid line in bootstrap.php: ' . trim($defLine)); continue; } $testNamespace = substr($testClass, 0, strrpos($testClass, '\\')); if (function_exists($testNamespace . '\\' . $f['name'])) { continue; } try { $r = new \ReflectionFunction($f['name']); if ($r->isUserDefined()) { throw new \ReflectionException(); } if (false !== strpos($f['signature'], '&')) { $defLine = sprintf('return \\%s%s', $f['name'], $f['args']); } else { $defLine = sprintf("return \\call_user_func_array('%s', func_get_args())", $f['name']); } } catch (\ReflectionException $e) { $defLine = sprintf("throw new \\PHPUnit_Framework_SkippedTestError('Internal function not found: %s')", $f['name']); } eval(<<<EOPHP namespace {$testNamespace}; use Symfony\\Polyfill\\Util\\TestListener; use {$testedClass->getNamespaceName()} as p; function {$f['name']}{$f['signature']} { if ('{$testClass}' === TestListener::\$enabledPolyfills) { {$f['return']}{$f['args']}; } {$defLine}; } EOPHP ); } if (!$warnings && null === $defLine) { $warnings[] = new \PHPUnit_Framework_SkippedTestCase('No Polyfills found in bootstrap.php for ' . $testClass); } else { $mainSuite->addTest(new static($suite)); } } foreach ($warnings as $w) { $mainSuite->addTest($w); } }
var_dump($rf->getNumberOfParameters()); print "\n"; print "--- getNumberOfRequiredParameters(\"f\") ---\n"; var_dump($rf->getNumberOfRequiredParameters()); print "\n"; print "--- getParameters(\"f\") ---\n"; var_dump($rf->getParameters()); print "\n"; print "--- getStaticVariables(\"f\") ---\n"; var_dump($rf->getStaticVariables()); print "\n"; print "--- isInternal(\"f\") ---\n"; var_dump($rf->isInternal()); print "\n"; print "--- isUserDefined(\"f\") ---\n"; var_dump($rf->isUserDefined()); print "\n"; print "--- returnsReference(\"f\") ---\n"; var_dump($rf->returnsReference()); print "\n"; print "--- export(\"f\") ---\n"; var_dump($rf->export('f', true)); print "\n"; # invoke() can't be used because $b is pass-by-reference. print "--- invokeArgs(\"f\") ---\n"; $b = "b"; var_dump($rf->invokeArgs(array("a", &$b, "c"))); var_dump($rf->invokeArgs(array("a", &$b, "c"))); print "\n"; print "--- getStaticVariables(\"f\") ---\n"; $rf = new ReflectionFunction("f");
$funcs = get_defined_functions(); foreach ($funcs['internal'] as $func) { $rf = new ReflectionFunction($func); $info = array('kind' => 'f', 'namespace' => $rf->getNamespaceName()); $params = array(); foreach ($rf->getParameters() as $rp) { $class = ''; if (!defined('HHVM_VERSION')) { $class = $rp->getClass(); } $param = ''; if ($class) { $param = $class->getName() . ' '; } elseif ($rp->isArray()) { $param = 'array '; } $param .= '$' . $rp->getName(); if ($rp->isOptional() && $rf->isUserDefined()) { $param .= '=' . json_encode($rp->getDefaultValue()); } $params[] = $param; } $info['type'] = '(' . implode(', ', $params) . ')'; if ($rf->isInternal()) { $filename = '(ext-' . $rf->getExtensionName() . ')'; } else { $filename = str_replace($base, '', $rf->getFileName()); } fwrite($fp, implode("\t", array($rf->getName(), $filename, $rf->getStartLine() . ';"', '')) . $build($info) . PHP_EOL); } });
/** * Routing and controller initialization * * @static * @return void */ public static function start() { global $request, $response; // TODO: move to method? $request = new Bottle_Request; $response = new Bottle_Response; $functions = get_defined_functions(); $controllers = $functions['user']; $controllers_list = array(); $views_list = array(); foreach($controllers as $controller) { if (substr($controller, 0, 2) != '__') { $controller = new ReflectionFunction($controller); if ($controller->isUserDefined()) { $docline = $controller->getDocComment(); if (preg_match('#^( |\t)*\*( )?@route (?P<route>.+?)$#umsi', $docline, $matches)) { $route = new Bottle_Route($controller->getName()); $route->setMask($matches['route']); $route->bindController($controller); $controllers_list[$controller->getName()] = $route->getMask(); if ($route->isServed($request->uri())) { if (preg_match('#^( |\t)*\*( )?@view (?P<view>.+?)$#umsi', $docline, $matches)) { $view = new Bottle_View($matches['view']); $response->setView($view); $views_list[] = $view; } /* * optional controller condition support * decorator param may be a single word (function * name), or a function name followed by an argument * list (separated by spaces). If an argument starts * with a $, and the controller also have an * argument with the same name, that value will be * passed in the function. */ if (preg_match('#^( |\t)*\*( )?@requires (?P<condition>.+?)$#umsi', $docline, $matches)) { // checking if the condition function has params if(strpos($matches['condition'], ' ')) { $condition_parts = explode(' ', $matches['condition']); $condition_name = array_shift($condition_parts); $route->setCondition($condition_name, $condition_parts); } else { if(!function_exists($matches['condition'])) { throw new Bottle_Exception('Unknown condition: '.$matches['condition']. ' for controller '.$controller->getName()); } $route->setCondition($matches['condition']); } } $request->setRouter($route); //break; } else { // fetching all views for the url() function if (preg_match('#^( |\t)*\*( )?@view (?P<view>.+?)$#umsi', $docline, $matches)) { $view = new Bottle_View($matches['view']); $views_list[] = $view; } } } } } } // giving the route list to each view foreach($views_list as $view) { $view->setRoutes($controllers_list); } $response->dispatch($request); }
/** hoho */ function test($a, $b = 1, $c = "") { static $var = 1; } $func = new ReflectionFunction("test"); var_dump($func->export("test")); echo "--getName--\n"; var_dump($func->getName()); echo "--isInternal--\n"; var_dump($func->isInternal()); echo "--isUserDefined--\n"; var_dump($func->isUserDefined()); echo "--getFilename--\n"; var_dump($func->getFilename()); echo "--getStartline--\n"; var_dump($func->getStartline()); echo "--getEndline--\n"; var_dump($func->getEndline()); echo "--getDocComment--\n"; var_dump($func->getDocComment()); echo "--getStaticVariables--\n"; var_dump($func->getStaticVariables()); echo "--invoke--\n"; var_dump($func->invoke(array(1, 2, 3))); echo "--invokeArgs--\n"; var_dump($func->invokeArgs(array(1, 2, 3))); echo "--returnsReference--\n";
<?php function addStars($message) { return "** {$message} **"; } $reflection = new ReflectionFunction('addStars'); var_dump($reflection->getName()); var_dump($reflection->getParameters()); var_dump($reflection->isUserDefined()); var_dump($reflection->getFileName()); /* foreach (get_class_methods($reflection) as $method) { echo $method . ":"; var_dump($reflection->$method()); } */ class Robot { public $name; public function __construct($name) { $this->name = $name; } public function sayHello() { return 'Hello ' . $this->name; } } $reflection = new ReflectionClass('Robot'); var_dump($reflection->getMethods()); var_dump($reflection->getProperties()); var_dump($reflection->newInstance('R2D2'));