本文整理汇总了PHP中ReflectionClass::getMethod方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionClass::getMethod方法的具体用法?PHP ReflectionClass::getMethod怎么用?PHP ReflectionClass::getMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionClass
的用法示例。
在下文中一共展示了ReflectionClass::getMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: toCall
/**
* 调用具体的业务逻辑接口,通常来说是项目中的
* controller,也可以RPC调用
* @param Request $request
* @return mixed
* @throws \Simple\Application\Game\Cycle\Exception\GameCycleException
*/
public function toCall(Request $request)
{
$head = $request->getHeader();
$module = ucfirst($head[0]);
$action = $head[1];
// TODO 获取类的命名空间
$cls = new \ReflectionClass(APP_TOP_NAMESPACE . '\\Controller\\' . $module . 'Controller');
$instance = null;
//优先调用init方法
if ($cls->hasMethod('__init__')) {
$initMethod = $cls->getMethod('__init__');
if ($initMethod->isPublic() == true) {
$instance = $cls->newInstanceArgs(array());
$initMethod->invokeArgs($instance, array());
}
}
$method = $cls->getMethod($action);
if ($method->isPublic() == false) {
throw new GameCycleException('无法调用接口:' . $module . '.' . $action);
}
if ($instance == null) {
$instance = $cls->newInstanceArgs(array());
}
$ret = $method->invokeArgs($instance, array($request));
if ($ret instanceof Response === false) {
throw new GameCycleException('接口返回的对象必须为:Response对象。');
}
return $ret;
}
示例2: mapBy
/**
* Map collection by key. For objects, return the property, use
* get method or is method, if avaliable.
*
* @param array $input Array of arrays or objects.
* @param string $key Key to map by.
* @throws \InvalidArgumentException If array item is not an array or object.
* @throws \LogicException If array item could not be mapped by given key.
* @return array Mapped array.
*/
public function mapBy(array $input, $key)
{
return array_map(function ($item) use($key) {
if (is_array($item)) {
if (!array_key_exists($key, $item)) {
throw new \LogicException("Could not map item by key \"{$key}\". Array key does not exist.");
}
return $item[$key];
}
if (!is_object($item)) {
throw new \InvalidArgumentException("Item must be an array or object.");
}
$ref = new \ReflectionClass($item);
if ($ref->hasProperty($key) && $ref->getProperty($key)->isPublic()) {
return $item->{$key};
}
if ($ref->hasMethod($key) && !$ref->getMethod($key)->isPrivate()) {
return $item->{$key}();
}
$get = 'get' . ucfirst($key);
if ($ref->hasMethod($get) && !$ref->getMethod($get)->isPrivate()) {
return $item->{$get}();
}
$is = 'is' . ucfirst($key);
if ($ref->hasMethod($is) && !$ref->getMethod($is)->isPrivate()) {
return $item->{$is}();
}
throw new \LogicException("Could not map item by key \"{$key}\". Cannot access the property directly or through getter/is method.");
}, $input);
}
示例3: doCall
function doCall($method, $parameters, $instance, $serverKey)
{
$callParameters = validateCallParameters($method, $parameters, $instance);
$reflectedClass = new ReflectionClass(get_class($instance));
$reflectedMethod = $reflectedClass->getMethod($method->getName());
$result;
$result = $reflectedMethod->invokeArgs($instance, $callParameters);
$resultJson = "";
//Print custom errors
$reflectedErrorMethod = $reflectedClass->getMethod("getErrors");
$reflectedErrors = $reflectedErrorMethod->invoke($instance);
if (!empty($reflectedErrors)) {
$resultJson .= "[";
foreach ($reflectedErrors as $reflectedError) {
$reflectedErrorClass = new ReflectionClass(get_class($reflectedError));
$code = $reflectedErrorClass->getMethod("getCode")->invoke($reflectedError);
$message = $reflectedErrorClass->getMethod("getMessage")->invoke($reflectedError);
$resultJson .= '{"code":' . JsonUtils::encodeToJson($code) . ',"message":' . JsonUtils::encodeToJson($message) . '},';
}
$resultJson = JsonUtils::removeLastChar($reflectedErrors, $resultJson);
$resultJson .= "]";
} else {
$resultJson .= serializeMethodResult($method, $result, $instance, $serverKey);
}
return $resultJson;
}
示例4: call_function
public function call_function($function_name, $parameters)
{
if (!method_exists($this, $function_name)) {
return false;
}
$reflector = new ReflectionClass($this);
if (count($parameters) < $reflector->getMethod($function_name)->getNumberOfRequiredParameters()) {
return false;
}
$collapse_parameters = false;
$function_parameters = $reflector->getMethod($function_name)->getParameters();
if (count($function_parameters) && end($function_parameters)->name == 'parameters') {
$collapse_parameters = true;
}
if (count($parameters) > count($function_parameters) && !$collapse_parameters) {
return false;
}
if ($collapse_parameters) {
$non_optional = array_splice($parameters, 0, count($function_parameters) - 1);
if (count($parameters)) {
$parameters = array_merge($non_optional, array($parameters));
} else {
$parameters = $non_optional;
}
}
return call_user_func_array(array($this, $function_name), $parameters);
}
示例5: getControllerMethod
public static function getControllerMethod($bgp_module_name, $public_method)
{
$method_definition = array();
if (!empty($bgp_module_name) && !empty($public_method)) {
$bgp_controller_name = 'BGP_Controller_' . ucfirst(strtolower($bgp_module_name));
if (is_subclass_of($bgp_controller_name, 'BGP_Controller')) {
// Reflection
$reflector = new ReflectionClass($bgp_controller_name);
$method = $reflector->getMethod($public_method);
$name = $method->name;
$class = $method->class;
$doc = $reflector->getMethod($name)->getDocComment();
// Parse Doc
if (is_string($doc)) {
$doc = new DocBlock($doc);
$desc = $doc->description;
$params = $doc->all_params;
// Params
if (!empty($params['param'])) {
$args = $params['param'];
} else {
$args = array();
}
$http = $params['http_method'][0];
$resource = $params['resource'][0];
$response = $params['return'][0];
$method_definition = array('resource' => trim($resource), 'id' => trim($public_method), 'name' => trim($http), 'description' => trim($desc), 'params' => $args, 'response' => trim($response));
}
}
}
return $method_definition;
}
示例6: getMethodFirstParameter
/**
* @return \ReflectionParameter
*/
protected function getMethodFirstParameter()
{
$backtrace = debug_backtrace();
$methodInfo = $this->reflection->getMethod($this->getName());
$parameters = $methodInfo->getParameters();
return $parameters[0];
}
示例7: test_Jetpack_API_Plugins_Install_Endpoint
/**
* @author tonykova
* @covers Jetpack_API_Plugins_Install_Endpoint
*/
public function test_Jetpack_API_Plugins_Install_Endpoint()
{
$endpoint = new Jetpack_JSON_API_Plugins_Install_Endpoint(array('stat' => 'plugins:1:new', 'method' => 'POST', 'path' => '/sites/%s/plugins/new', 'path_labels' => array('$site' => '(int|string) The site ID, The site domain'), 'request_format' => array('plugin' => '(string) The plugin slug.'), 'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format, 'example_request_data' => array('headers' => array('authorization' => 'Bearer YOUR_API_TOKEN'), 'body' => array('plugin' => 'buddypress')), 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/plugins/new'));
$the_plugin_file = 'the/the.php';
$the_real_folder = WP_PLUGIN_DIR . '/the';
$the_real_file = WP_PLUGIN_DIR . '/' . $the_plugin_file;
// Check if 'The' plugin folder is already there.
if (file_exists($the_real_folder)) {
$this->markTestSkipped('The plugn the test tries to install (the) is already installed. Skipping.');
}
$class = new ReflectionClass('Jetpack_JSON_API_Plugins_Install_Endpoint');
$plugins_property = $class->getProperty('plugins');
$plugins_property->setAccessible(true);
$plugins_property->setValue($endpoint, array($the_plugin_file));
$validate_plugins_method = $class->getMethod('validate_plugins');
$validate_plugins_method->setAccessible(true);
$result = $validate_plugins_method->invoke($endpoint);
$this->assertTrue($result);
$install_plugin_method = $class->getMethod('install');
$install_plugin_method->setAccessible(true);
$result = $install_plugin_method->invoke($endpoint);
$this->assertTrue($result);
$this->assertTrue(file_exists($the_real_folder));
// Clean up
$this->rmdir($the_real_folder);
}
示例8: action
/**
* action method is execute action.
*
* @param string $name is action name
* @param array $arguments for action
*/
public function action($name, $arguments = array())
{
//switch canvas for action name...
$this->_canvas->switchAction($name);
// check authetication
// if ($this->authentication($action);
$this->_assign = array();
$ref = new ReflectionClass($this);
$method = sprintf("_%s", $name);
if (!$ref->hasMethod($method) || !$ref->getMethod($method)->isPublic()) {
throw new Joy_Exception_NotFound_Method("Action Not Found ({$method})");
}
// FIXME: ReflectionMethod class not found setAccesible method in PHP 5.2.10 version.
$ref->getMethod($method)->setAccessible(TRUE);
//->invokeArgs($this, $arguments);
$view = $ref->getMethod($method)->invokeArgs($this, $arguments);
// $view = call_user_method_array($method, $this, $arguments);
if ($view == null) {
$view = new Joy_View_Empty();
}
// has layout
if (!is_null($layout = $this->_canvas->getLayout())) {
$layout = new Joy_View_Layout(array("file" => $layout));
$layout->setPlaceHolder($view);
return $layout;
}
return $view;
}
示例9: loadType
/**
* {@inheritDoc}
*
* Build type from introspection of that class
*
* @api
*/
public function loadType($className, RdfMapperInterface $mapper, RdfTypeFactory $typeFactory)
{
if (!class_exists($className)) {
throw new TypeNotFoundException('Class ' . $className . ' not found');
}
$typenode = $this->createType($mapper, array());
/** @var $type TypeInterface */
$type = $typenode->getRdfElement();
$type->setVocabulary('lucky', 'http://localhost/lucky');
$typeof = strtr($className, '\\', '_');
$type->setRdfType('typeof', "lucky:{$typeof}");
$class = new \ReflectionClass($className);
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
if (!strncmp($method->getShortName(), 'set', 3) && strlen($method->getShortName()) > 3) {
$candidate = substr($method->getShortName(), 3);
if ($class->hasMethod('get' . $candidate) && $class->getMethod('set' . $candidate)->getNumberOfParameters() == 1 && $class->getMethod('get' . $candidate)->getNumberOfParameters() == 0) {
// TODO: introspect if method is using array value => collection instead of property
$this->addProperty($typeFactory, $type, lcfirst($candidate));
}
}
}
$fields = $class->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($fields as $field) {
if (isset($type->{$field->getName()})) {
// there was a getter and setter method for this
continue;
}
$this->addProperty($typeFactory, $type, $field->getName());
}
$this->classNames[$className] = $className;
return $typenode;
}
示例10: testGenerator
public function testGenerator()
{
/** @var OpsWay\Test2\Solid\I $generator */
$generator = OpsWay\Test2\Solid\Factory::create('d');
$generator->generate();
$this->assertFileExists($file = __DIR__ . '/../../test/D/IReader.php');
include $file;
$this->assertTrue(interface_exists('IReader'), 'interface IReader not exists');
$this->assertFileExists($file = __DIR__ . '/../../test/D/CsvReader.php');
include $file;
$this->assertTrue(class_exists('CsvReader'), 'class CsvReader not exists');
$this->assertTrue(method_exists('CsvReader', 'read'), 'Method read not exists in CsvReader');
$this->assertFileExists($file = __DIR__ . '/../../test/D/App.php');
include $file;
$this->assertTrue(class_exists('App'), 'class App not exists');
$this->assertContains('private $reader', file_get_contents($file));
$this->assertFileExists($file = __DIR__ . '/../../test/D/run.php');
$this->assertContains('$app = new App();', file_get_contents($file));
$this->assertContains('print_r($app->parse());', file_get_contents($file));
$app = new ReflectionClass('App');
$this->assertGreaterThan(0, count($app->getMethods()), 'Too less methods in ' . $app->getName());
$construct = $app->getMethod('__construct');
$this->assertNotFalse($construct->getName());
$construct = $app->getMethod('parse');
$this->assertNotFalse($construct->getName());
$this->assertEquals(0, count($construct->getParameters()), 'Too less arguments in ' . $construct->getName());
$this->assertContains('$reader = new CsvReader();', file_get_contents($app->getFileName()));
$this->assertContains('return $reader->read();', file_get_contents($app->getFileName()));
$this->checkRead(new App());
}
示例11: testAbstractStartServiceCommand
/**
* Tests AbstractStartServiceCommand.
*/
public function testAbstractStartServiceCommand()
{
/** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject $output */
$output = $this->getMockForAbstractClass('Symfony\\Component\\Console\\Output\\OutputInterface');
/** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject $input */
$input = $this->getMockForAbstractClass('Symfony\\Component\\Console\\Input\\InputInterface');
$input->expects($this->once())->method('getArgument')->with('target')->willReturn('target');
$factory = $this->getMockBuilder('ONGR\\ConnectionsBundle\\Pipeline\\PipelineFactory')->setMethods(['setProgressBar'])->getMock();
$factory->method('setProgressBar')->will($this->returnValue(null));
/** @var PipelineStarter|\PHPUnit_Framework_MockObject_MockObject $PipelineStarter */
$pipelineStarter = $this->getMock('ONGR\\ConnectionsBundle\\Pipeline\\PipelineStarter');
$pipelineStarter->setPipelineFactory($factory);
$pipelineStarter->method('getPipelineFactory')->will($this->returnValue($factory));
$pipelineStarter->expects($this->once())->method('startPipeline')->with('prefix', 'target');
/** @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject $container */
$container = $this->getMockForAbstractClass('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$container->expects($this->any())->method('get')->with('PipelineStarterService')->willReturn($pipelineStarter);
/** @var AbstractStartServiceCommand|\PHPUnit_Framework_MockObject_MockObject $command */
$command = $this->getMockBuilder('ONGR\\ConnectionsBundle\\Command\\AbstractStartServiceCommand')->setConstructorArgs(['name', 'description'])->setMethods(['addArgument', 'getContainer'])->getMock();
$command->expects($this->once())->method('addArgument')->with('target', InputArgument::OPTIONAL, $this->anything());
$command->expects($this->once())->method('getContainer')->willReturn($container);
$reflection = new \ReflectionClass($command);
$method = $reflection->getMethod('configure');
$method->setAccessible(true);
$method->invoke($command);
$method = $reflection->getMethod('start');
$method->setAccessible(true);
$method->invoke($command, $input, $output, 'PipelineStarterService', 'prefix');
$this->assertEquals('description', $command->getDescription());
}
示例12: start
public function start()
{
//var_dump("APP start");
$actionParametersClassName = '';
$actionParametersClass = '';
$this->initController();
$reflectionController = new \ReflectionClass($this->controller);
try {
$action = $reflectionController->getMethod($this->actionName);
} catch (\ReflectionException $e) {
$this->actionName = self::DEFAULT_ACTION_NAME;
$action = $reflectionController->getMethod($this->actionName);
}
if ($action->getParameters() != null) {
$actionParametersClass = $action->getParameters()[0]->getClass();
$actionParametersClassName = $actionParametersClass->getName();
}
if (preg_match('/\\w+BindingModel/', $actionParametersClassName)) {
$bindModelInstance = new $actionParametersClassName();
$this->requestBindingModelInstance = $bindModelInstance;
$bindModelProperties = $actionParametersClass->getProperties();
foreach ($bindModelProperties as $prop) {
if (!isset($_POST[$prop->getName()])) {
throw new \Exception("Parameters Do not respond to the Binding Model.");
}
$setterName = 'set' . $prop->getName();
$bindModelInstance->{$setterName}($_POST[$prop->getName()]);
}
$this->callControllerAction($this->requestBindingModelInstance);
} else {
$this->callControllerAction($this->requestParams);
}
}
示例13: evaluateCallback
/**
* Evaluate the callback and create an object instance if required and possible.
*
* @param array|callable $callback The callback to invoke.
*
* @return array|callable
*/
protected static function evaluateCallback($callback)
{
if (is_array($callback) && count($callback) == 2 && is_string($callback[0]) && is_string($callback[1])) {
$class = new \ReflectionClass($callback[0]);
// Ff the method is static, do not create an instance.
if ($class->hasMethod($callback[1]) && $class->getMethod($callback[1])->isStatic()) {
return $callback;
}
// Fetch singleton instance.
if ($class->hasMethod('getInstance')) {
$getInstanceMethod = $class->getMethod('getInstance');
if ($getInstanceMethod->isStatic()) {
$callback[0] = $getInstanceMethod->invoke(null);
return $callback;
}
}
// Create a new instance.
$constructor = $class->getConstructor();
if (!$constructor || $constructor->isPublic()) {
$callback[0] = $class->newInstance();
} else {
$callback[0] = $class->newInstanceWithoutConstructor();
$constructor->setAccessible(true);
$constructor->invoke($callback[0]);
}
}
return $callback;
}
示例14: testGetSingle
public function testGetSingle()
{
$singletags = ['br', 'clear', 'col', 'hr', 'xkcd'];
$method = $this->reflectionClass->getMethod('getSingle');
$this->parser = $this->reflectionClass->newInstance();
$this->assertEquals($singletags, $method->invoke($this->parser));
}
示例15: invoke
public function invoke(URLComponent $url)
{
$class_name = $url->getController();
$method = $url->getAction();
$class = $this->app . '\\action\\' . $class_name;
//Response对象
$response = Response::getInstance();
//Request对象
$request = Request::getInstance();
#实例化控制器,使用反射
$reflection = new \ReflectionClass($class);
$instacne = $reflection->newInstance();
//先执行初始化方法init
if ($reflection->hasMethod('init')) {
$init = $reflection->getMethod('init');
$data = $init->invokeArgs($instacne, array($request, $response));
if ($data) {
//如果有返回数据则输出
$response->setBody($data);
$response->send();
return true;
}
}
if ($reflection->hasMethod($method)) {
$method = $reflection->getMethod($method);
} elseif ($reflection->hasMethod('getMiss')) {
$method = $reflection->getMethod('getMiss');
} else {
throw new RouteException('Method does not exist.');
}
$data = $method->invokeArgs($instacne, array($request, $response));
#输出
$response->setBody($data);
$response->send();
}