the param as a class name to be instantiated $arg = $this->make($definition[$name]); } elseif (($prefix = self::A_RAW . $name) && (isset($definition[$prefix]) || array_key_exists($prefix, $definition))) { // interpret the param as a raw value to be injected $arg = $definition[$prefix]; } elseif (($prefix = self::A_DELEGATE . $name) && isset($definition[$prefix])) { // interpret the param as an invokable delegate $arg = $this->buildArgFromDelegate($name, $definition[$prefix]); } elseif (($prefix = self::A_DEFINE . $name) && isset($definition[$prefix])) { // interpret the param as a class definition $arg = $this->buildArgFromParamDefineArr($definition[$prefix]); } elseif (!$arg = $this->buildArgFromTypeHint($reflFunc, $reflParam)) { $arg = $this->buildArgFromReflParam($reflParam, $className); if ($arg === null && PHP_VERSION_ID >= 50600 && $reflParam->isVariadic()) { // buildArgFromReflParam might return null in case the parameter is optional // in case of variadics, the parameter is optional, but null might not be allowed continue; } } $args[] = $arg; } return $args; } private function buildArgFromParamDefineArr($definition) { if (!is_array($definition)) { throw new InjectionException( $this->inProgressMakes // @TODO Add message ); } if (!isset($definition[0], $definition[1])) { throw new InjectionException( $this->inProgressMakes // @TODO Add message ); } list($class, $definition) = $definition; return $this->make($class, $definition); } private function buildArgFromDelegate($paramName, $callableOrMethodStr) { if ($this->isExecutable($callableOrMethodStr) === false) { throw InjectionException::fromInvalidCallable( $this->inProgressMakes, $callableOrMethodStr ); } $executable = $this->buildExecutable($callableOrMethodStr); return $executable($paramName, $this); } private function buildArgFromTypeHint(\ReflectionFunctionAbstract $reflFunc, \ReflectionParameter $reflParam) { $typeHint = $this->reflector->getParamTypeHint($reflFunc, $reflParam); if (!$typeHint) { $obj = null; } elseif ($reflParam->isDefaultValueAvailable()) { $normalizedName = $this->normalizeName($typeHint); // Injector has been told explicitly how to make this type if (isset($this->aliases[$normalizedName]) || isset($this->delegates[$normalizedName]) || isset($this->shares[$normalizedName])) { $obj = $this->make($typeHint); } else { $obj = $reflParam->getDefaultValue(); } } else { $obj = $this->make($typeHint); } return $obj; } private function buildArgFromReflParam(\ReflectionParameter $reflParam, $className = null) { if (array_key_exists($reflParam->name, $this->paramDefinitions)) { $arg = $this->paramDefinitions[$reflParam->name]; } elseif ($reflParam->isDefaultValueAvailable()) { $arg = $reflParam->getDefaultValue(); } elseif ($reflParam->isOptional()) { // This branch is required to work around PHP bugs where a parameter is optional // but has no default value available through reflection. Specifically, PDO exhibits // this behavior. $arg = null; } else { $reflFunc = $reflParam->getDeclaringFunction(); $classDeclare = ($reflFunc instanceof \ReflectionMethod) ? " declared in " . $reflFunc->getDeclaringClass()->name . "::" : ""; $classWord = ($reflFunc instanceof \ReflectionMethod) ? $className . '::' : ''; $funcWord = $classWord . $reflFunc->name; throw new InjectionException( $this->inProgressMakes, sprintf( self::M_UNDEFINED_PARAM, $reflParam->name, $reflParam->getPosition(), $funcWord, $classDeclare ), self::E_UNDEFINED_PARAM ); } return $arg; } private function prepareInstance($obj, $normalizedClass) { if (isset($this->prepares[$normalizedClass])) { $prepare = $this->prepares[$normalizedClass]; $executable = $this->buildExecutable($prepare); $result = $executable($obj, $this); if ($result instanceof $normalizedClass) { $obj = $result; } } $interfaces = @class_implements($obj); if ($interfaces === false) { throw new InjectionException( $this->inProgressMakes, sprintf( self::M_MAKING_FAILED, $normalizedClass, gettype($obj) ), self::E_MAKING_FAILED ); } if (empty($interfaces)) { return $obj; } $interfaces = array_flip(array_map(array($this, 'normalizeName'), $interfaces)); $prepares = array_intersect_key($this->prepares, $interfaces); foreach ($prepares as $interfaceName => $prepare) { $executable = $this->buildExecutable($prepare); $result = $executable($obj, $this); if ($result instanceof $normalizedClass) { $obj = $result; } } return $obj; } /** * Invoke the specified callable or class::method string, provisioning dependencies along the way * * @param mixed $callableOrMethodStr A valid PHP callable or a provisionable ClassName::methodName string * @param array $args Optional array specifying params with which to invoke the provisioned callable * @throws \Auryn\InjectionException * @return mixed Returns the invocation result returned from calling the generated executable */ public function execute($callableOrMethodStr, array $args = array()) { list($reflFunc, $invocationObj) = $this->buildExecutableStruct($callableOrMethodStr); $executable = new Executable($reflFunc, $invocationObj); $args = $this->provisionFuncArgs($reflFunc, $args, null, $invocationObj === null ? null : get_class($invocationObj)); return call_user_func_array(array($executable, '__invoke'), $args); } /** * Provision an Executable instance from any valid callable or class::method string * * @param mixed $callableOrMethodStr A valid PHP callable or a provisionable ClassName::methodName string * @return \Auryn\Executable */ public function buildExecutable($callableOrMethodStr) { try { list($reflFunc, $invocationObj) = $this->buildExecutableStruct($callableOrMethodStr); } catch (\ReflectionException $e) { throw InjectionException::fromInvalidCallable( $this->inProgressMakes, $callableOrMethodStr, $e ); } return new Executable($reflFunc, $invocationObj); } private function buildExecutableStruct($callableOrMethodStr) { if (is_string($callableOrMethodStr)) { $executableStruct = $this->buildExecutableStructFromString($callableOrMethodStr); } elseif ($callableOrMethodStr instanceof \Closure) { $callableRefl = new \ReflectionFunction($callableOrMethodStr); $executableStruct = array($callableRefl, null); } elseif (is_object($callableOrMethodStr) && is_callable($callableOrMethodStr)) { $invocationObj = $callableOrMethodStr; $callableRefl = $this->reflector->getMethod($invocationObj, '__invoke'); $executableStruct = array($callableRefl, $invocationObj); } elseif (is_array($callableOrMethodStr) && isset($callableOrMethodStr[0], $callableOrMethodStr[1]) && count($callableOrMethodStr) === 2 ) { $executableStruct = $this->buildExecutableStructFromArray($callableOrMethodStr); } else { throw InjectionException::fromInvalidCallable( $this->inProgressMakes, $callableOrMethodStr ); } return $executableStruct; } private function buildExecutableStructFromString($stringExecutable) { if (function_exists($stringExecutable)) { $callableRefl = $this->reflector->getFunction($stringExecutable); $executableStruct = array($callableRefl, null); } elseif (method_exists($stringExecutable, '__invoke')) { $invocationObj = $this->make($stringExecutable); $callableRefl = $this->reflector->getMethod($invocationObj, '__invoke'); $executableStruct = array($callableRefl, $invocationObj); } elseif (strpos($stringExecutable, '::') !== false) { list($class, $method) = explode('::', $stringExecutable, 2); $executableStruct = $this->buildStringClassMethodCallable($class, $method); } else { throw InjectionException::fromInvalidCallable( $this->inProgressMakes, $stringExecutable ); } return $executableStruct; } private function buildStringClassMethodCallable($class, $method) { $relativeStaticMethodStartPos = strpos($method, 'parent::'); if ($relativeStaticMethodStartPos === 0) { $childReflection = $this->reflector->getClass($class); $class = $childReflection->getParentClass()->name; $method = substr($method, $relativeStaticMethodStartPos + 8); } list($className, $normalizedClass) = $this->resolveAlias($class); $reflectionMethod = $this->reflector->getMethod($className, $method); if ($reflectionMethod->isStatic()) { return array($reflectionMethod, null); } $instance = $this->make($className); // If the class was delegated, the instance may not be of the type // $class but some other type. We need to get the reflection on the // actual class to be able to call the method correctly. $reflectionMethod = $this->reflector->getMethod($instance, $method); return array($reflectionMethod, $instance); } private function buildExecutableStructFromArray($arrayExecutable) { list($classOrObj, $method) = $arrayExecutable; if (is_object($classOrObj) && method_exists($classOrObj, $method)) { $callableRefl = $this->reflector->getMethod($classOrObj, $method); $executableStruct = array($callableRefl, $classOrObj); } elseif (is_string($classOrObj)) { $executableStruct = $this->buildStringClassMethodCallable($classOrObj, $method); } else { throw InjectionException::fromInvalidCallable( $this->inProgressMakes, $arrayExecutable ); } return $executableStruct; } }