Exemple #1
0
 public function __construct(\ReflectionFunctionAbstract $Reflection, callable $SourceLoader)
 {
     $this->Reflection = $Reflection;
     $this->Parameters = $this->Reflection->getParameters();
     $this->UsedVariablesMap = $this->Reflection->getStaticVariables();
     $this->SourceLoader = $SourceLoader;
 }
Exemple #2
0
 /**
  * Python-style args/kwargs argument arrays. Creates an indexed
  * argument list to use with reflection::
  * 
  *     class Pants {
  *         function doPants($arg1, $arg2, $arg3=null, $foo=>null) {
  *         }
  *     }
  *     
  * 	   $args = ['arg1', 'arg2', 'foo'=>'bar'];
  *     $rm = (new ReflectionClass('Pants'))->getMethod('doPants');
  *     $return = $rm->invokeArgs(func_get_call_args($rm), $args);
  */
 public static function getCallArgs(\ReflectionFunctionAbstract $rm, $args, $ignoreUnknown = false)
 {
     if (!$args) {
         $args = [];
     }
     $callArgs = [];
     $inArgs = true;
     foreach ($rm->getParameters() as $idx => $param) {
         $paramFound = false;
         if ($inArgs && ($inArgs = isset($args[$idx]))) {
             $callArgs[] = $args[$idx];
             $paramFound = true;
             unset($args[$idx]);
         } else {
             if (array_key_exists($param->name, $args)) {
                 $paramFound = true;
                 $callArgs[] = $args[$param->name];
                 unset($args[$param->name]);
             }
         }
         if (!$paramFound) {
             if ($param->isDefaultValueAvailable()) {
                 $callArgs[] = $param->getDefaultValue();
             } else {
                 throw new \UnexpectedValueException("No value for argument {$param->name} for function {$rm->getName()}");
             }
         }
     }
     if ($args && !$ignoreUnknown) {
         throw new \UnexpectedValueException("Unknown keyword arguments: " . implode(", ", array_keys($args)));
     }
     return $callArgs;
 }
 /**
  * @param \ReflectionFunctionAbstract $reflectionFunction
  *
  * @return string
  */
 private function determineRequestObjectClass(\ReflectionFunctionAbstract $reflectionFunction)
 {
     $reflectionParameters = $reflectionFunction->getParameters();
     /** @var \ReflectionParameter $reflectionParameter */
     $reflectionParameter = reset($reflectionParameters);
     return $reflectionParameter->getClass()->name;
 }
Exemple #4
0
 protected function getParameters(\ReflectionFunctionAbstract $ref = null, array $userParams = [])
 {
     $parameters = [];
     $expectedParameters = $ref->getParameters();
     foreach ($expectedParameters as $parameter) {
         $name = $parameter->getName();
         $class = $parameter->getClass();
         if ($parameter->isVariadic()) {
             return array_merge($parameters, array_values($userParams));
         }
         if (isset($userParams[$name])) {
             $parameters[] = $userParams[$name];
             unset($userParams[$name]);
         } elseif ($parameter->isDefaultValueAvailable()) {
             $parameters[] = $parameter->getDefaultValue();
         } elseif ($this->has($name)) {
             $parameters[] = $this->get($name);
         } elseif ($class) {
             $parameters[] = $this->get($class->name);
         } else {
             throw new InjectorException("Unable to resolve parameter '{$name}'");
         }
     }
     return $parameters;
 }
Exemple #5
0
 /**
  * Get the list of injectable arguments for a function or method.
  *
  * The returned array can be used with `call_user_func_array()` or
  * `invokeArgs()`.
  *
  * @param \ReflectionFunctionAbstract $method
  * @return array List of arguments
  * @throws \RuntimeException When an argument cannot be found
  */
 public function getInjections(\ReflectionFunctionAbstract $method)
 {
     $injections = [];
     $parameters = $method->getParameters();
     foreach ($parameters as $param) {
         $found = false;
         $injection = null;
         try {
             $injection = $this->findKey($param->getName());
             $found = true;
         } catch (\RuntimeException $e) {
         }
         if ($paramType = $param->getType()) {
             try {
                 $injection = $this->findKey($paramType);
                 $found = true;
             } catch (\RuntimeException $e) {
             }
         }
         if (!$found && $param->isDefaultValueAvailable()) {
             $injection = $param->getDefaultValue();
             $found = true;
         }
         if (!$found) {
             $paramName = $param->getName() . ' (' . $param->getType() . ')';
             throw new \RuntimeException("Could not find a definition for {$paramName}.");
         }
         $injections[] = $injection;
     }
     return $injections;
 }
 /**
  * Helper class for getFunctionName().
  *
  * @param \ReflectionFunctionAbstract $function
  * @return string
  */
 private static function getClassName(\ReflectionFunctionAbstract $function)
 {
     if ($function instanceof \ReflectionMethod) {
         return $function->getDeclaringClass()->getName() . '::';
     }
     return '';
 }
 /**
  * Helper method to retrieve the name of a ReflectionFunctionAbstract
  * @param \ReflectionFunctionAbstract $reflection
  * @return string
  */
 protected function getReflectionFunctionName(\ReflectionFunctionAbstract $reflection)
 {
     // Class method
     if ($reflection instanceof \ReflectionMethod) {
         return $reflection->getDeclaringClass()->getName() . '::' . $reflection->getName();
     }
     return $reflection->getName();
 }
 private function getParameter(\ReflectionFunctionAbstract $reflectionFunction, $name)
 {
     foreach ($reflectionFunction->getParameters() as $parameter) {
         if ($parameter->getName() === $name) {
             return $parameter;
         }
     }
 }
 private function fetchFunctionIdentifier(\ReflectionFunctionAbstract $function)
 {
     $functionIdentifier = $function->getName();
     if ($function instanceof \ReflectionMethod) {
         $functionIdentifier = sprintf('%s::%s', $function->getDeclaringClass()->getName(), $function->getName());
     }
     return $functionIdentifier;
 }
 /**
  * @param \ReflectionFunctionAbstract $reflection
  *
  * @return string
  */
 protected static function getFunctionName(\ReflectionFunctionAbstract $reflection)
 {
     $name = $reflection->name . '()';
     if ($reflection instanceof \ReflectionMethod) {
         $name = $reflection->getDeclaringClass()->name . '::' . $name;
     }
     return $name;
 }
Exemple #11
0
 /**
  * @param string                      $type
  * @param mixed                       $value
  * @param integer                     $position
  * @param \ReflectionFunctionAbstract $func
  */
 public function __construct($type, $value, $position, \ReflectionFunctionAbstract $func)
 {
     $this->type = $type;
     $this->value = $value;
     $this->position = $position;
     $this->func = $func;
     $params = $func->getParameters();
     $this->reflection = isset($params[$position]) ? $params[$position] : null;
 }
Exemple #12
0
 /**
  * @param \ReflectionFunctionAbstract $reflection
  *
  * @return string
  */
 protected function fetchCode(\ReflectionFunctionAbstract $reflection)
 {
     $file = $reflection->getFileName();
     if (!file_exists($file)) {
         return '';
     }
     $startLine = $reflection->getStartLine();
     return implode('', array_slice(file($file), $startLine, $reflection->getEndLine() - $startLine - 1));
 }
 /**
  * Generate key for parameter
  *
  * @param \ReflectionParameter        $parameter
  * @param \ReflectionFunctionAbstract $method
  *
  * @return string
  */
 public static function generateForParameter(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $method)
 {
     if ($method instanceof \ReflectionMethod) {
         $key = $method->getDeclaringClass()->getName() . '::' . $method->getName() . ':' . $parameter->getName();
     } else {
         $key = 'function::' . $method->getName() . ':' . $parameter->getName();
     }
     return $key;
 }
 /**
  * Set correct order of arguments
  *
  * @param MethodDefinition $methodDefinition
  * @param \ReflectionFunctionAbstract $reflectionMethod
  * @throws \InvalidArgumentException
  */
 public function setOrderArguments(MethodDefinition $methodDefinition, \ReflectionFunctionAbstract $reflectionMethod)
 {
     $parameters = [];
     // Get parameter names
     foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
         $parameters[] = $reflectionParameter->getName();
     }
     $methodDefinition->setParametersCollectionOrder($parameters);
 }
 /**
  * @inheritdoc
  */
 public function collect()
 {
     $requirements = array();
     foreach ($this->reflection->getParameters() as $parameter) {
         $requirement = $this->createRequirement($parameter);
         $resourceName = $requirement->getResourceName();
         $requirements[$resourceName] = $requirement;
     }
     return $requirements;
 }
 /**
  * Get the attribute name for ParamFetcher injection
  *
  * @param \ReflectionFunctionAbstract $controllerReflector
  *
  * @return null|string
  */
 private function getAttributeName(\ReflectionFunctionAbstract $controllerReflector)
 {
     foreach ($controllerReflector->getParameters() as $parameter) {
         $hintedClass = $parameter->getClass();
         if ($hintedClass !== null && $hintedClass->implementsInterface(ParamFetcherInterface::class)) {
             return $parameter->getName();
         }
     }
     return null;
 }
 /**
  * @param \ReflectionFunctionAbstract $r
  * @param Request                     $request
  *
  * @return void
  */
 private function injectPaginatedRequest(\ReflectionFunctionAbstract $r, Request $request)
 {
     $paginatedRequest = new PaginatedRequest($request, $this->pagePath, $this->pageDefaultValue, $this->maxResultsPath, $this->maxResultsDefaultValue);
     foreach ($r->getParameters() as $param) {
         if (!$param->getClass() || !$param->getClass()->isInstance($paginatedRequest)) {
             continue;
         }
         $request->attributes->set($param->getName(), $paginatedRequest);
     }
 }
 /**
  *
  * @param \ReflectionFunctionAbstract $function        	
  */
 public static function getFunctionBody(\ReflectionFunctionAbstract $function)
 {
     $source = file($function->getFileName());
     $start = $function->getStartLine() - 1;
     $end = $function->getEndLine();
     $body = implode('', array_slice($source, $start, $end - $start));
     $open = strpos($body, '{');
     $close = strrpos($body, '}');
     return trim(substr($body, $open + 1, (strlen($body) - $close) * -1));
 }
Exemple #19
0
 /**
  * Populates arguments assembled from the event and the given target function.
  * 
  * @param \ReflectionFunctionAbstract $ref
  * @param EventParamResolverInterface $resolver
  * @return array
  */
 protected function loadArguments(\ReflectionFunctionAbstract $ref, EventParamResolverInterface $resolver)
 {
     $args = [];
     if ($ref->getNumberOfParameters() > 1) {
         foreach (array_slice($ref->getParameters(), 1) as $param) {
             $args[] = $resolver->resolve($param->getClass(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : false);
         }
     }
     return $args;
 }
 /**
  * @param \ReflectionParameter        $parameter
  * @param \ReflectionFunctionAbstract $context
  */
 public function __construct(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $context)
 {
     $this->parameter = $parameter;
     $this->context = $context;
     $name = $context->getName();
     if ($context instanceof \ReflectionMethod) {
         $name = $context->class . '::' . $name;
     }
     parent::__construct("Unable to resolve '{$parameter->name}' argument in '{$name}'.");
 }
Exemple #21
0
 /**
  * @return string|NULL
  */
 public static function getReturnType(\ReflectionFunctionAbstract $func)
 {
     if (PHP_VERSION_ID >= 70000 && $func->hasReturnType()) {
         return (string) $func->getReturnType();
     }
     $type = preg_replace('#[|\\s].*#', '', (string) self::parseAnnotation($func, 'return'));
     if ($type) {
         return $func instanceof \ReflectionMethod ? self::expandClassName($type, $func->getDeclaringClass()) : ltrim($type, '\\');
     }
 }
 /**
  * Generates list of arguments using autowiring.
  * @return array
  */
 public static function autowireArguments(\ReflectionFunctionAbstract $method, array $arguments, $container)
 {
     $optCount = 0;
     $num = -1;
     $res = array();
     $methodName = ($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName() . '()';
     foreach ($method->getParameters() as $num => $parameter) {
         if (array_key_exists($num, $arguments)) {
             $res[$num] = $arguments[$num];
             unset($arguments[$num]);
             $optCount = 0;
         } elseif (array_key_exists($parameter->getName(), $arguments)) {
             $res[$num] = $arguments[$parameter->getName()];
             unset($arguments[$parameter->getName()]);
             $optCount = 0;
         } elseif (($class = PhpReflection::getParameterType($parameter)) && !PhpReflection::isBuiltinType($class)) {
             $res[$num] = $container->getByType($class, FALSE);
             if ($res[$num] === NULL) {
                 if ($parameter->allowsNull()) {
                     $optCount++;
                 } elseif (class_exists($class) || interface_exists($class)) {
                     $rc = new \ReflectionClass($class);
                     if ($class !== ($hint = $rc->getName())) {
                         throw new ServiceCreationException("Service of type {$class} needed by {$methodName} not found, did you mean {$hint}?");
                     }
                     throw new ServiceCreationException("Service of type {$class} needed by {$methodName} not found. Did you register it in configuration file?");
                 } else {
                     throw new ServiceCreationException("Class {$class} needed by {$methodName} not found. Check type hint and 'use' statements.");
                 }
             } else {
                 if ($container instanceof ContainerBuilder) {
                     $res[$num] = '@' . $res[$num];
                 }
                 $optCount = 0;
             }
         } elseif ($parameter->isOptional() || $parameter->isDefaultValueAvailable()) {
             // !optional + defaultAvailable = func($a = NULL, $b) since 5.3.17 & 5.4.7
             // optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ...
             $res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
             $optCount++;
         } else {
             throw new ServiceCreationException("Parameter \${$parameter->getName()} in {$methodName} has no class type hint or default value, so its value must be specified.");
         }
     }
     // extra parameters
     while (array_key_exists(++$num, $arguments)) {
         $res[$num] = $arguments[$num];
         unset($arguments[$num]);
         $optCount = 0;
     }
     if ($arguments) {
         throw new ServiceCreationException("Unable to pass specified arguments to {$methodName}.");
     }
     return $optCount ? array_slice($res, 0, -$optCount) : $res;
 }
 /**
  * Create new instance of each dependency. Null if the parameter is not typed.
  *
  * @param \ReflectionFunctionAbstract $reflection
  * @return array Array of instance of dependencies.
  */
 private function generateParams(\ReflectionFunctionAbstract $reflection)
 {
     $params = $reflection->getParameters();
     return array_reduce($params, function ($acc, \ReflectionParameter $param) {
         $class = $param->getClass();
         if ($class != null) {
             $acc[] = new $class->name();
         }
         return $acc;
     }, []);
 }
Exemple #24
0
 /**
  * @param \ReflectionFunctionAbstract $function
  *
  * @return boolean
  */
 public static function isVariadic(\ReflectionFunctionAbstract $function)
 {
     if (self::$supportsVariadicParameters === null) {
         self::$supportsVariadicParameters = method_exists('\\ReflectionParameter', 'isVariadic');
     }
     foreach ($function->getParameters() as $parameter) {
         if ($parameter->getName() === '...' || self::$supportsVariadicParameters && $parameter->isVariadic()) {
             return true;
         }
     }
     return false;
 }
 /**
  * Constructs a FunctionParser from a reflected function. Triggers all code parsing from the constructor.
  *
  * @param \ReflectionFunctionAbstract $reflection The reflected function or method.
  */
 public function __construct(\ReflectionFunctionAbstract $reflection)
 {
     if (!$reflection->isUserDefined()) {
         throw new \InvalidArgumentException('You can only parse the code of user-defined functions.');
     }
     $this->reflection = $reflection;
     $this->tokenizer = $this->fetchTokenizer();
     $this->parameters = $this->fetchParameters();
     $this->code = $this->parseCode();
     $this->body = $this->parseBody();
     $this->context = $this->parseContext();
 }
 /**
  * @param \ReflectionFunctionAbstract $reflection
  *
  * @return string
  */
 protected static function getFunctionName(\ReflectionFunctionAbstract $reflection)
 {
     if (!$reflection instanceof \ReflectionMethod) {
         return $reflection->name;
     }
     $class = $reflection->getDeclaringClass()->name;
     // see https://github.com/facebook/hhvm/issues/3874
     if (0 === strpos($class, 'Closure')) {
         $class = 'Closure';
     }
     return $class . '::' . $reflection->name;
 }
 public function validate(\ReflectionFunctionAbstract $method, array $arguments)
 {
     foreach ($method->getParameters() as $parameterNumber => $parameter) {
         if (array_key_exists($parameterNumber, $arguments)) {
             $this->argumentValidator->validate($parameter, $arguments[$parameterNumber]);
         } else {
             if ($this->shouldParameterHaveAnArgument($parameter)) {
                 throw new MissingRequiredArgumentException($parameter->getDeclaringClass()->getName(), $parameter->getName());
             }
         }
     }
 }
Exemple #28
0
function getFunctionString(ReflectionFunctionAbstract $ref)
{
    $name = $ref->getName() . '(';
    if ($ref instanceof ReflectionMethod) {
        if ($ref->isStatic()) {
            $name = '::' . $name;
        } else {
            $name = '->' . $name;
        }
    }
    $name .= getParamString($ref);
    $name .= ')';
    return $name;
}
Exemple #29
0
 /**
  * Get constructor parameters definitions.
  *
  * @param \ReflectionFunctionAbstract $constructor
  *
  * @return array
  */
 public function getParametersDefinition(\ReflectionFunctionAbstract $constructor)
 {
     $parameters = [];
     foreach ($constructor->getParameters() as $index => $parameter) {
         if ($parameter->isOptional()) {
             continue;
         }
         $parameterClass = $parameter->getClass();
         if ($parameterClass) {
             $parameters[$index] = $this->getClassDefinition($parameterClass);
         }
     }
     return $parameters;
 }
Exemple #30
0
 /**
  * Generates list of arguments using autowiring.
  * @param  Nette\Reflection\GlobalFunction|Nette\Reflection\Method
  * @return array
  */
 public static function autowireArguments(\ReflectionFunctionAbstract $method, array $arguments, $container)
 {
     $optCount = 0;
     $num = -1;
     $res = array();
     foreach ($method->getParameters() as $num => $parameter) {
         if (array_key_exists($num, $arguments)) {
             $res[$num] = $arguments[$num];
             unset($arguments[$num]);
             $optCount = 0;
         } elseif (array_key_exists($parameter->getName(), $arguments)) {
             $res[$num] = $arguments[$parameter->getName()];
             unset($arguments[$parameter->getName()]);
             $optCount = 0;
         } elseif ($class = $parameter->getClassName()) {
             // has object type hint
             $res[$num] = $container->getByType($class, FALSE);
             if ($res[$num] === NULL) {
                 if ($parameter->allowsNull()) {
                     $optCount++;
                 } elseif (class_exists($class) || interface_exists($class)) {
                     throw new ServiceCreationException("Service of type {$class} needed by {$method} not found. Did you register it in configuration file?");
                 } else {
                     throw new ServiceCreationException("Class {$class} needed by {$method} not found. Check type hint and 'use' statements.");
                 }
             } else {
                 if ($container instanceof ContainerBuilder) {
                     $res[$num] = '@' . $res[$num];
                 }
                 $optCount = 0;
             }
         } elseif ($parameter->isOptional()) {
             // PDO::__construct has optional parameter without default value (and isArray() and allowsNull() returns FALSE)
             $res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
             $optCount++;
         } else {
             throw new ServiceCreationException("Parameter {$parameter} has no type hint, so its value must be specified.");
         }
     }
     // extra parameters
     while (array_key_exists(++$num, $arguments)) {
         $res[$num] = $arguments[$num];
         unset($arguments[$num]);
         $optCount = 0;
     }
     if ($arguments) {
         throw new ServiceCreationException("Unable to pass specified arguments to {$method}.");
     }
     return $optCount ? array_slice($res, 0, -$optCount) : $res;
 }