public function testReflectionAnalysisErrors() { $analysis = new ReflectionAnalysis(); $analysis->of('Adadgio\\GearBundle\\Tests\\Component\\Reflection\\DummyController'); $nonExistentArgName = $analysis->findTypeHintedArgName('Request'); $this->assertEquals($nonExistentArgName, false); }
/** * Fired during any controller call. Responsible for injecting * an \ApiRequest in the controller when annotation is found and * a type hint "ApiRequest" is found. Also throw exceptions on * invlid authentication * * @param object FilterControllerEvent\Event */ public function onKernelController(FilterControllerEvent $event) { $request = $event->getRequest(); $controller = $event->getController(); // return if this is not a controller if (!is_array($controller)) { return; } // prepare \Reflection objects to find their respective annotations // $reflectionClass = new \ReflectionClass($controller[0]); // actual controller called $reflectionObject = new \ReflectionObject($controller[0]); // actual controller called $reflectionMethod = $reflectionObject->getMethod($controller[1]); // actual method called // will be a merge of class and methods annotations foreach ($this->reader->getMethodAnnotations($reflectionMethod) as $annotation) { // do nothing if our anontation is not found if (!$annotation instanceof Api\Annotation\Api) { continue; } // let the \ApiCoreService create an \ApiRequest object internally and set internal // variable to do his own sauce later on (it needs annotation params of course) $this->apiCore->handleRequest($request, $annotation); // also dont listen to anything else than json requests if (!$this->apiCore->getApiRequest()->isJson()) { return; } // let the ApiCoreService do check ups and pass // authentication to other dedicated security services if (false === $this->apiCore->isSecured()) { $error = $this->apiCore->getError(); throw new Api\ApiException($error['message'], $error['code']); } // a priori, // if (false === $this->apicore->isSecured()) { // $error = $this->apicore->getError(); // throw new ApiException($error->message, $error->code); // } // make sure the controller has a ApiEngine\ApiHandler instanceof type hinted // argument somewhere in its param. This is found using the full controller // name (and namespace), method and PHP \Reflection class. // Example: indexAction(Request $request, ApiHandler $api){...} $analysis = new ReflectionAnalysis(); $analysis->of($request->attributes->get('_controller')); $argumentName = $analysis->findTypeHintedArgName(Api\ApiRequest::class); // ReflectionAnalysis::ofController($request->attributes->get('_controller')); // $argumentName = ReflectionAnalysis::getArgumentTypeHintedWith('ApiHandler'); if (!$argumentName) { // no type hinted argument was // declared in the controller method return; } // hum, whats that? if ($request->attributes->has($argumentName)) { var_dump('It has! But when does this happen?'); // the request attribute already has that argument, we // cant override it. the developer messed things up! return; } // retrieve a \ApiRequest object from the \ApiCoreService and pass // it as argument to the controller where the argument was found $apiRequest = $this->apiCore->getApiRequest(); $request->attributes->set($argumentName, $apiRequest); return; } }