/** * Specific annotation evaluation. * * @param Request $request Request * @param Annotation $annotation Annotation * @param ReflectionMethod $method Method * * @return FormAnnotationResolver self Object */ public function evaluateAnnotation(Request $request, Annotation $annotation, ReflectionMethod $method) { /** * Annotation is only laoded if is typeof WorkAnnotation */ if ($annotation instanceof AnnotationForm) { /** * Once loaded Annotation info, we just instanced Service name */ $annotationValue = $annotation->getClass(); /** * Get FormType object given a service name */ $type = class_exists($annotationValue) ? new $annotationValue() : $this->formRegistry->getType($annotationValue)->getInnerType(); /** * Get the parameter name. If not defined, is set as $form */ $parameterName = $annotation->getName() ?: $this->defaultName; $parameterClass = $this->getParameterType($method, $parameterName, 'Symfony\\Component\\Form\\FormInterface'); /** * Requiring result with calling getBuiltObject(), set as request * attribute desired element */ $request->attributes->set($parameterName, $this->getBuiltObject($request, $this->formFactory, $annotation, $parameterClass, $type)); } return $this; }
/** * Tests field name * * @param string $name Name * @param string $resultName Result name * * @dataProvider dataName */ public function testName($name, $resultName) { $this->annotation->expects($this->any())->method('getClass')->will($this->returnValue('Mmoreram\\ControllerExtraBundle\\Tests\\FakeBundle\\Entity\\Fake')); $this->annotation->expects($this->any())->method('getName')->will($this->returnValue($name)); $this->attributes->expects($this->once())->method('set')->with($this->equalTo($resultName), $this->isInstanceOf('Mmoreram\\ControllerExtraBundle\\Tests\\FakeBundle\\Entity\\Fake')); $this->entityAnnotationResolver->evaluateAnnotation($this->request, $this->annotation, $this->reflectionMethod); }
/** * Specific annotation evaluation. * * @param Request $request Request * @param Annotation $annotation Annotation * @param ReflectionMethod $method Method * * @return EntityAnnotationResolver self Object * * @throws EntityNotFoundException */ public function evaluateAnnotation(Request $request, Annotation $annotation, ReflectionMethod $method) { /** * Annotation is only laoded if is typeof AnnotationEntity */ if ($annotation instanceof AnnotationObjectManager) { /** * Creating new instance of desired entity */ $entity = $this->entityProvider->provide($annotation->getClass()); $objectManager = $this->doctrine->getManagerForClass(get_class($entity)); /** * Get the parameter name. If not defined, is set as defined in * parameters */ $parameterName = $annotation->getName() ?: $this->defaultName; $request->attributes->set($parameterName, $objectManager); } return $this; }
/** * Tests execute setting in evaluateAnnotation method * * @param string $defaultExecute Default execute * @param string $execute Execute * @param string $resultExecute Result execute * * @dataProvider dataEvaluateAnnotationExecute */ public function testEvaluateAnnotationExecute($defaultExecute, $execute, $resultExecute) { $this->annotation->expects($this->any())->method('getExecute')->will($this->returnValue($execute)); /** * @var LogAnnotationResolver $logAnnotationResolver */ $logAnnotationResolver = $this->getMockBuilder('Mmoreram\\ControllerExtraBundle\\Resolver\\LogAnnotationResolver')->disableOriginalConstructor()->setMethods(array('getDefaultExecute', 'logMessage', 'getLogger'))->getMock(); $logAnnotationResolver->expects($this->any())->method('getDefaultExecute')->will($this->returnValue($defaultExecute)); $logger = $this->getMockBuilder('Psr\\Log\\LoggerInterface')->disableOriginalConstructor()->getMock(); $logAnnotationResolver->expects($this->any())->method('getLogger')->will($this->returnValue($logger)); $logAnnotationResolver->evaluateAnnotation($this->request, $this->annotation, $this->reflectionMethod); $this->assertEquals($logAnnotationResolver->getExecute(), $resultExecute); }
/** * Specific annotation evaluation. * * This method must be implemented in every single EventListener * with specific logic * * All method code will executed only if specific active flag is true * * @param Request $request Request * @param Annotation $annotation Annotation * @param ReflectionMethod $method Method * * @return AnnotationResolverInterface self Object */ public function evaluateAnnotation(Request $request, Annotation $annotation, ReflectionMethod $method) { /** * Annotation is only loaded if is typeof AnnotationEntity */ if ($annotation instanceof Post) { $param = $request->request->get($annotation->getPath(), $annotation->getDefault(), $annotation->isDeep()); $annotationParameterName = $annotation->getName(); $parameterName = is_null($annotationParameterName) ? $annotation->getPath() : $annotationParameterName; $request->attributes->set($parameterName, $param); } }
/** * Specific annotation evaluation. * * @param Request $request Request * @param Annotation $annotation Annotation * @param ReflectionMethod $method Method * * @return LogAnnotationResolver self Object */ public function evaluateAnnotation(Request $request, Annotation $annotation, ReflectionMethod $method) { /** * Annotation is only laoded if is typeof AnnotationLog */ if ($annotation instanceof AnnotationLog) { $this->level = $annotation->getLevel() ? $annotation->getLevel() : $this->getDefaultLevel(); $this->execute = $annotation->getExecute() ? $annotation->getExecute() : $this->getDefaultExecute(); $this->mustLog = true; $this->value = $annotation->getValue(); /** * Only logs before controller execution if EXEC_PRE or EXEC_BOTH */ if (in_array($this->getExecute(), array(AnnotationLog::EXEC_PRE, AnnotationLog::EXEC_BOTH))) { $this->logMessage($this->getLogger(), $this->getLevel(), $this->getValue()); } } return $this; }
/** * Specific annotation evaluation * * All method code will executed only if specific active flag is true * * @param Request $request Request * @param Annotation $annotation Annotation * @param ReflectionMethod $method Method * * @return AnnotationResolverInterface|string self Object or dql */ public function evaluateAnnotation(Request $request, Annotation $annotation, ReflectionMethod $method) { /** * Annotation is only loaded if is type-of AnnotationEntity */ if ($annotation instanceof AnnotationPaginator) { /** * Creating new instance of desired entity */ $entity = $this->entityProvider->provide($annotation->getClass()); /** * We create a basic query builder */ $queryBuilder = $this->createQueryBuilder($entity); /** * Every paginator evaluator is evaluated in this code. * * Every evaluated is defined using a Dependency Injection tag, * and accumulated in a Collector. * This collector evaluator, evaluates each one injected previously * by the DI Component */ $this->paginatorEvaluatorCollector->evaluate($queryBuilder, $annotation); $paginator = new Paginator($queryBuilder, true); /** * Calculating limit of elements per page. Value can be evaluated * using as reference a Request attribute value */ $limitPerPage = (int) $this->requestParameterProvider->getParameterValue($annotation->getLimit() ?: $this->defaultLimitPerPage); /** * Calculating page to fetch. Value can be evaluated using as * reference a Request attribute value */ $page = (int) $this->requestParameterProvider->getParameterValue($annotation->getPage() ?: $this->defaultPage); /** * If attributes is not null, this bundle will place in the method * parameter named as defined a new PaginatorAttributes Value Object * with all needed data */ $this->evaluateAttributes($request, $annotation, $paginator, $limitPerPage, $page); /** * Calculating offset, given number per pageand page */ $offset = $limitPerPage * ($page - 1); /** * Retrieving the Paginator iterator */ $paginator->getQuery()->setFirstResult($offset)->setMaxResults($limitPerPage); /** * Get the parameter name. If not defined, is set as defined in * parameters */ $parameterName = $annotation->getName() ?: $this->defaultName; $parameterType = $this->getParameterType($method, $parameterName); $dql = $paginator->getQuery()->getDQL(); $paginator = $this->decidePaginatorFormat($paginator, $parameterType, $limitPerPage, $page); $request->attributes->set($parameterName, $paginator); return $dql; } return $this; }
/** * Specific annotation evaluation. * * @param Request $request Request * @param Annotation $annotation Annotation * @param ReflectionMethod $method Method * * @return EntityAnnotationResolver self Object * * @throws EntityNotFoundException */ public function evaluateAnnotation(Request $request, Annotation $annotation, ReflectionMethod $method) { /** * Annotation is only loaded if is typeof AnnotationEntity */ if ($annotation instanceof AnnotationEntity) { /** * Creating new instance of desired entity */ $entity = $this->entityProvider->provide($annotation->getClass()); /** * Tries to get a mapped instance of this entity. * If not mapped, just return old new created */ $entity = $this->evaluateMapping($annotation, $entity); /** * Persists entity if defined */ $this->resolvePersist($annotation, $entity); /** * If is decided this entity has to be persisted into manager */ $this->evaluateSetters($request->attributes, $entity, $annotation->getSetters()); /** * Get the parameter name. If not defined, is set as defined in * parameters */ $parameterName = $annotation->getName() ?: $this->defaultName; $request->attributes->set($parameterName, $entity); } return $this; }
/** * Specific annotation evaluation. * * @param Request $request Request * @param Annotation $annotation Annotation * @param ReflectionMethod $method Method * * @return FlushAnnotationResolver self Object */ public function evaluateAnnotation(Request $request, Annotation $annotation, ReflectionMethod $method) { /** * Annotation is only laoded if is typeof AnnotationFlush */ if ($annotation instanceof AnnotationFlush) { $managerName = $annotation->getManager() ?: $this->getDefaultManager(); /** * Loading locally desired Doctrine manager */ $this->manager = $this->getDoctrine()->getManager($managerName); /** * Set locally entities to flush. If null, flush all */ $this->entities = new ArrayCollection(); $entity = $annotation->getEntity(); $entities = is_array($entity) ? $entity : array($entity); /** * For every entity defined, we try to get it from Request Attributes */ foreach ($entities as $entityName) { if ($request->attributes->has($entityName)) { $this->entities[] = $request->attributes->get($entityName); } } /** * If we have not found any entity to flush, or any has been defined. * In this case, flush all */ if ($this->entities->isEmpty()) { $this->entities = null; } /** * In this case, manager must be flushed after controller logic */ $this->mustFlush = true; } return $this; }
/** * Specific annotation evaluation. * * @param Request $request Request * @param Annotation $annotation Annotation * @param ReflectionMethod $method Method * * @return JsonResponseAnnotationResolver self Object */ public function evaluateAnnotation(Request $request, Annotation $annotation, ReflectionMethod $method) { /** * Annotation is only laoded if is typeof WorkAnnotation */ if ($annotation instanceof AnnotationJsonResponse) { /** * If JsonResponse annotation, set to true for future events * * Also saves all needed info from annotation */ $this->returnJson = true; $this->status = $annotation->getStatus() ?: $this->getDefaultStatus(); $this->headers = $annotation->getHeaders() ?: $this->getDefaultHeaders(); } return $this; }