/** * @param string $pattern * @param array $array * @return static */ public static function FromArray(array $array, $pattern = null) { $def = new static(); $def->pattern = $pattern; $def->ns = isset($array['ns']) ? $array['ns'] : ''; $def->assert = isset($array['assert']) ? $array['assert'] : []; $def->convert = isset($array['convert']) ? $array['convert'] : []; $def->defaults = isset($array['defaults']) ? $array['defaults'] : []; if (isset($array['methods'])) { if (!is_array($array['methods']) and !$array['methods'] instanceof \Traversable) { throw new \RuntimeException('Definition of "methods" should be array or traversable on route: ' . $pattern); } foreach ($array as $method => $definition) { $methodDefinition = array_merge(['method' => strtoupper($method)], $definition); $def->methods[$methodDefinition['method']] = MethodDefinition::FromArray($methodDefinition, $pattern); } } else { if (!isset($array['action'])) { throw new \RuntimeException('No "action" defined for method "' . strtoupper(isset($array['method']) ? $array['method'] : 'get') . '" on route: ' . $pattern); } $def->method = strtoupper(isset($array['method']) ? $array['method'] : 'get'); $def->action = $array['action']; $def->bind = isset($array['bind']) ? $array['bind'] : null; } // identifying sub-routes: foreach ($array as $index => $value) { if ($index[0] === '/') { $def->addSubroute(static::FromArray($value, $index)); } } return $def; }
/** * @param \ReflectionParameter $parameter * @param MethodDefinition $methodDef * * @return mixed * * @throws InjectorException */ private function getArgument(\ReflectionParameter $parameter, MethodDefinition $methodDef = null) { $paramName = $parameter->getName(); // Has a value been set for this parameter? if ($methodDef !== null && $methodDef->hasArgument($paramName)) { $argument = $methodDef->getArgument($paramName); return $this->resolveArgument($argument, $parameter); } // Is the argument an object? if (($reflectionClass = $parameter->getClass()) !== null) { try { $className = $reflectionClass->getName(); if ($reflectionClass->isInterface() || $reflectionClass->isAbstract()) { $className = isset($this->mappings[$className]) ? $this->mappings[$className] : null; if ($className !== null) { return $this->make($className); } if ($reflectionClass->isInterface()) { throw new InjectorException(sprintf(self::EX_UNMAPPED_INTERFACE, $reflectionClass->getName())); } if ($reflectionClass->isAbstract()) { throw new InjectorException(sprintf(self::EX_UNMAPPED_ABSTRACT_CLASS, $reflectionClass->getName())); } } return $this->make($className); } catch (InjectorException $ex) { // If this parameter has a default value return it. if ($parameter->isOptional()) { return $parameter->getDefaultValue(); } throw $ex; } } // No argument value has been found for this parameter and it cannot be automatically // resolved because it's not an object so if it has a default value then return it. if ($parameter->isOptional()) { return $parameter->getDefaultValue(); } $className = $parameter->getDeclaringClass()->getName(); $methodName = $parameter->getDeclaringFunction()->getName(); throw new InjectorException(sprintf(self::EX_ARGUMENT_NOT_FOUND, $className, $methodName, $paramName)); }