/** * @param object|class $controller * @return string Name of matchig method */ public static function getMatchingMethod($controller) { $reflClass = new ReflectionClass($controller); $classAnnotation = $reflClass->getAnnotation(RequestMapping::class); $requestHelper = Helper::getService(HttpServletRequest::class); $pathInfo = $requestHelper->getServer(HttpServletRequest::PATH_INFO); if (!$pathInfo) { $pathInfo = $requestHelper->getServer(HttpServletRequest::REQUEST_URI); } /* @var $reflMethod ReflectionMethod */ foreach ($reflClass->getMethods(PHP_ReflectionMethod::IS_PUBLIC) as $reflMethod) { /* @var $methodAnnotation RequestMapping */ $methodAnnotation = $reflMethod->getAnnotation(RequestMapping::class); /* @var $requestHelper HttpServletRequest */ $url = ''; if ($classAnnotation) { $url .= $classAnnotation->value; } if ($methodAnnotation) { $url .= $methodAnnotation->value; } $regexp = '/^' . str_replace('/', '\\/', $url) . '/'; $test = preg_match($regexp, $pathInfo, $prop); $test &= $methodAnnotation !== null && self::isMatching($methodAnnotation); if ($url && $methodAnnotation && $test) { foreach ($prop as $key => $value) { if (!is_numeric($key)) { $requestHelper->setParam($key, $value); } } return $reflMethod->getName(); } } return null; }
/** * * @param string $type Class or Interface name * @return boolean */ public function isInstanceOf($type) { $reflType = new \ReflectionClass($type); if ($reflType->isInterface()) { return $this->reflClass->implementsInterface($type); } else { return $this->reflClass->isSubclassOf($type) || $this->reflClass->getName() === $type; } }
/** * @param ReflectionClass $reflClass * @param array $params * @return object instance of $reflClass * @throws RuntimeException */ public static function getNewInstanceByRefl(\ReflectionClass $reflClass, array $params = null) { $counter = self::$counter++; if (!$reflClass instanceof ReflectionClass) { $reflClass = new ReflectionClass($reflClass); } $instance = $reflClass->newInstanceWithoutConstructor(); self::callPropertyAnnotationHandlers($reflClass, $instance, $counter); self::callConstructor($reflClass, $instance, $params, $counter); return $instance; }
private function handleUnAuthorizedException($instance, $ex) { $reflClass = new ReflectionClass($instance); $throwFurther = true; foreach ($reflClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { if ($method->hasAnnotation(ExceptionHandler::class)) { $throwFurther &= MethodInvoker::invoke($instance, $method->getName(), array()); } } if ($throwFurther) { throw $ex; } }
/** * @covers PhSpring\Reflection\ReflectionClass::getProperties */ public function testGetProperties() { $properties = $this->object->getProperties(); $this->assertInternalType('array', $properties); $this->assertNotEmpty($properties); $property = array_shift($properties); $this->assertInstanceOf(ReflectionProperty::class, $property); }
public static function invoke($instance, $methodName, $args = array()) { $className = get_class($instance); $reflClass = new ReflectionClass($className); if (!$reflClass->hasMethod($methodName)) { throw new BadMethodCallException("The '{$methodName}' method is not exists in {$className}"); } $reflMethod = $reflClass->getMethod($methodName); $annotations = $reflMethod->getAnnotations(); foreach (InvokerConfig::getMethodBeforeHandlers($reflMethod) as $methodAnnotationHandler) { $methodAnnotationHandler->run($reflMethod, $instance); } $expectedParameterSize = sizeof($reflMethod->getParameters()); $invokeParams = (new InvokeParameterHandler($annotations, $reflMethod, $args))->run(); if ($expectedParameterSize > sizeof($invokeParams)) { throw new BadMethodCallException("Not found all expected method parameter: {$className}::{$methodName}()"); } return $reflMethod->invokeArgs($instance, $invokeParams); }
public function getProperties($filter = null) { if ($filter === null) { $filter = ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_STATIC; } if ($this->adapter === null) { $properties = parent::getProperties($filter); } else { $properties = $this->adapter->getProperties($filter); } $retProperties = array(); foreach ($properties as $prop) { $retProperties[$prop->name] = $this->getProperty($prop->name); } return $retProperties; }
private function getScope(ReflectionClass $reflClass) { $scope = Scope::SINGLETON; if ($reflClass->hasAnnotation(Component::class)) { $annotation = $reflClass->getAnnotation(Component::class); $scope = $annotation->scope; if ($reflClass->hasAnnotation(Scope::class)) { $annotation = $reflClass->getAnnotation(Scope::class); $scope = $annotation->value; } } /** * @todo Need more work */ switch ($scope) { case Scope::SINGLETON: case Scope::REQUEST: case Scope::SESSION: $scope = Scope::SINGLETON; break; default: $scope = Scope::PROTOTYPE; break; } return $scope; }
private function fillForm($form, $request = null) { $class = new ReflectionClass($form); /* @var $property ReflectionProperty */ foreach ($class->getProperties() as $property) { $value = array_key_exists($property->getName(), (array) $request) ? $request[$property->getName()] : null; $type = Helper::getPropertyType($property); if (!$this->isPrimitive($type)) { $value = $this->fillForm(ClassInvoker::getNewInstance($type), $value); } $this->setFormFieldValue($property, $form, $value); } return $form; }
/** * @covers PhSpring\Reflection\ReflectionClass::__call */ public function callUndefinedMethod() { $this->object->getDummy(); }