Example #1
0
 /**
  * Attempts to translate this route's target to a function within a controller class.
  *
  * @param Context $context
  * @return array
  * @throws RoutingException
  */
 private function translateToClassCallable(Context $context)
 {
     // Load the class and create an instance of it
     $targetParts = explode('@', strval($this->getTarget()), 2);
     $targetClass = $targetParts[0];
     $targetFuncName = count($targetParts) > 1 ? $targetParts[1] : 'action';
     if (!class_exists($targetClass, true)) {
         throw new RoutingException('Could not locate class: ' . $targetClass);
     }
     $classObj = null;
     // Invoke constructor with dependency injection
     $parameterList = $context->determineParamValuesForConstructor($targetClass);
     try {
         $reflection = new \ReflectionClass($targetClass);
         $classObj = $reflection->newInstanceArgs($parameterList);
     } catch (\TypeError $ex) {
         throw new RoutingException('Type error thrown when calling constructor on ' . $targetClass, 0, $ex);
     }
     // Verify target function and return a callable
     $targetFunc = [$classObj, $targetFuncName];
     if (!is_callable($targetFunc)) {
         throw new RoutingException('Route target function is not callable: ' . $this->getTarget());
     }
     return $targetFunc;
 }