/**
  * Modifies the Request object to apply configuration information found in
  * controllers annotations like the template to render or HTTP caching
  * configuration.
  *
  * @param FilterControllerEvent $event A FilterControllerEvent instance
  */
 public function onKernelController(FilterControllerEvent $event)
 {
     if (!is_array($controller = $event->getController())) {
         return;
     }
     $className = class_exists('Doctrine\\Common\\Util\\ClassUtils') ? ClassUtils::getClass($controller[0]) : get_class($controller[0]);
     $object = new \ReflectionClass($className);
     $method = $object->getMethod($controller[1]);
     $classConfigurations = $this->getConfigurations($this->reader->getClassAnnotations($object));
     $methodConfigurations = $this->getConfigurations($this->reader->getMethodAnnotations($method));
     $configurations = array();
     foreach (array_merge(array_keys($classConfigurations), array_keys($methodConfigurations)) as $key) {
         if (!array_key_exists($key, $classConfigurations)) {
             $configurations[$key] = $methodConfigurations[$key];
         } elseif (!array_key_exists($key, $methodConfigurations)) {
             $configurations[$key] = $classConfigurations[$key];
         } else {
             if (is_array($classConfigurations[$key])) {
                 if (!is_array($methodConfigurations[$key])) {
                     throw new \UnexpectedValueException('Configurations should both be an array or both not be an array');
                 }
                 $configurations[$key] = array_merge($classConfigurations[$key], $methodConfigurations[$key]);
             } else {
                 // method configuration overrides class configuration
                 $configurations[$key] = $methodConfigurations[$key];
             }
         }
     }
     $request = $event->getRequest();
     foreach ($configurations as $key => $attributes) {
         $request->attributes->set($key, $attributes);
     }
 }
Example #2
0
 /**
  * Load metadata class
  *
  * @param \ReflectionClass $class
  * @return ClassMetadata
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $classMetadata = new ClassMetadata($class->name);
     $classMetadata->fileResources[] = $class->getFileName();
     foreach ($this->reader->getClassAnnotations($class) as $annotation) {
         if ($annotation instanceof NamespaceNode) {
             $classMetadata->addGraphNamespace($annotation);
         }
         if ($annotation instanceof GraphNode) {
             $classMetadata->addGraphMetadata($annotation, new MetadataValue($annotation->value));
         }
     }
     foreach ($class->getProperties() as $property) {
         foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
             if ($annotation instanceof GraphNode) {
                 $classMetadata->addGraphMetadata($annotation, new PropertyMetadata($class->name, $property->name));
             }
         }
     }
     foreach ($class->getMethods() as $method) {
         foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
             if ($annotation instanceof GraphNode) {
                 $classMetadata->addGraphMetadata($annotation, new MethodMetadata($class->name, $method->name));
             }
         }
     }
     return $classMetadata;
 }
 public function onKernelController(FilterControllerEvent $event)
 {
     if (!is_array($controller = $event->getController())) {
         return;
     }
     $object = new \ReflectionObject($controller[0]);
     $method = $object->getMethod($controller[1]);
     $classConfigurations = $this->reader->getClassAnnotations($object);
     $methodConfigurations = $this->reader->getMethodAnnotations($method);
     foreach (array_merge($classConfigurations, $methodConfigurations) as $configuration) {
         if ($configuration instanceof OAuth2) {
             $token = $this->token_storage->getToken();
             // If no access token is found by the firewall, then returns an authentication error
             if (!$token instanceof OAuth2Token) {
                 $this->createAuthenticationException($event, 'OAuth2 authentication required');
                 return;
             }
             foreach ($this->getCheckers() as $checker) {
                 $result = $checker->check($token, $configuration);
                 if (null !== $result) {
                     $this->createAccessDeniedException($event, $result);
                     return;
                 }
             }
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function loadForClass($class, $group)
 {
     $reflectionClass = Reflection::loadClassReflection($class);
     $classAnnotations = $this->reader->getClassAnnotations($reflectionClass);
     $securityClassAnnotation = null;
     $rules = array();
     foreach ($classAnnotations as $classAnnotation) {
         if ($classAnnotation instanceof ClassSecurityAnnotation && $group == $classAnnotation->group) {
             if ($securityClassAnnotation) {
                 throw new \RuntimeException(sprintf('The @ClassSecurity annotation already defined in class "%s".', $reflectionClass->getName()));
             }
             $securityClassAnnotation = $classAnnotation;
         }
         if ($rule = $this->transformAnnotationToRule($classAnnotation, $group, $class)) {
             $rules[] = $rule;
         }
     }
     if (!$securityClassAnnotation && !count($rules)) {
         return null;
     }
     if ($securityClassAnnotation) {
         $strategy = $securityClassAnnotation->strategy;
     } else {
         $strategy = Security::STRATEGY_AFFIRMATIVE;
     }
     $securityClass = new ClassSecurity($reflectionClass->getName(), $strategy, $rules, $group);
     return $securityClass;
 }
 public function onPostSerialize(ObjectEvent $event)
 {
     /**
      * @var JsonSerializationVisitor $visitor
      */
     $visitor = $event->getVisitor();
     $object = $event->getObject();
     $annotations = $this->annotationReader->getClassAnnotations(new \ReflectionObject($object));
     $links = array();
     foreach ($annotations as $annotation) {
         if ($annotation instanceof Link) {
             if ($annotation->url) {
                 $uri = $this->evaluate($annotation->url, $object);
             } else {
                 $uri = $this->router->generate($annotation->route, $this->resolveParams($annotation->params, $object));
             }
             // allow a blank URI to be an optional link
             if ($uri) {
                 $links[$annotation->name] = $uri;
             }
         }
     }
     if ($links) {
         $visitor->addData('_links', $links);
     }
 }
 /**
  * Reads the "@Access" annotations from the controller stores them in the "access" route option.
  */
 public function onConfigureRoute($event, $route)
 {
     if (!$this->reader) {
         $this->reader = new SimpleAnnotationReader();
         $this->reader->addNamespace('Pagekit\\User\\Annotation');
     }
     if (!$route->getControllerClass()) {
         return;
     }
     $access = [];
     foreach (array_merge($this->reader->getClassAnnotations($route->getControllerClass()), $this->reader->getMethodAnnotations($route->getControllerMethod())) as $annot) {
         if (!$annot instanceof Access) {
             continue;
         }
         if ($expression = $annot->getExpression()) {
             $access[] = $expression;
         }
         if ($admin = $annot->getAdmin() !== null) {
             $route->setPath('admin' . rtrim($route->getPath(), '/'));
             $permission = 'system: access admin area';
             if ($admin) {
                 $access[] = $permission;
             } else {
                 if ($key = array_search($permission, $access)) {
                     unset($access[$key]);
                 }
             }
         }
     }
     if ($access) {
         $route->setDefault('_access', array_unique($access));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $annotations = $this->reader->getClassAnnotations($class);
     if (0 === count($annotations)) {
         return null;
     }
     $classMetadata = new ClassMetadata($class->getName());
     $classMetadata->fileResources[] = $class->getFileName();
     foreach ($annotations as $annotation) {
         if ($annotation instanceof Annotation\Resource) {
             // auto transform type from class name
             if (!$annotation->type) {
                 $annotation->type = String::dasherize($class->getShortName());
             }
             $classMetadata->setResource(new Resource($annotation->type, $annotation->showLinkSelf));
         }
     }
     $classProperties = $class->getProperties();
     foreach ($classProperties as $property) {
         $annotations = $this->reader->getPropertyAnnotations($property);
         foreach ($annotations as $annotation) {
             if ($annotation instanceof Annotation\Id) {
                 $classMetadata->setIdField($property->getName());
             } else {
                 if ($annotation instanceof Annotation\Relationship) {
                     $classMetadata->addRelationship(new Relationship($property->getName(), $annotation->includeByDefault, $annotation->showLinkSelf, $annotation->showLinkRelated));
                 }
             }
         }
     }
     return $classMetadata;
 }
Example #8
0
 public function onFilterController(FilterControllerEvent $event)
 {
     list($object, $method) = $event->getController();
     // the controller could be a proxy
     $className = ClassUtils::getClass($object);
     $reflectionClass = new \ReflectionClass($className);
     $reflectionMethod = $reflectionClass->getMethod($method);
     $allControllerAnnotations = $this->annotationReader->getClassAnnotations($reflectionClass);
     $allMethodAnnotations = $this->annotationReader->getMethodAnnotations($reflectionMethod);
     $guardAnnotationsFilter = function ($annotation) {
         return $annotation instanceof Guard;
     };
     $controllerGuardAnnotations = array_filter($allControllerAnnotations, $guardAnnotationsFilter);
     $methodGuardAnnotations = array_filter($allMethodAnnotations, $guardAnnotationsFilter);
     $guardAnnotations = array_merge($controllerGuardAnnotations, $methodGuardAnnotations);
     $permissions = [];
     foreach ($guardAnnotations as $guardAnnotation) {
         $value = $guardAnnotation->value;
         if (!is_array($value)) {
             $value = [$value];
         }
         $permissions = array_merge($value, $permissions);
     }
     $permissions = array_unique($permissions);
     if (!empty($permissions) && !$this->security->isGranted($permissions)) {
         $e = new PermissionRequiredException();
         $e->setRequiredPermissions($permissions)->setCurrentPermissions($this->security->getToken()->getUser()->getPermissions());
         throw $e;
     }
 }
Example #9
0
 /**
  * Get Annotations for class
  *
  * @param \ReflectionClass $class
  * @return array
  */
 public function getClassAnnotations(\ReflectionClass $class)
 {
     $annotations = array();
     foreach ($this->delegate->getClassAnnotations($class) as $annot) {
         $annotations[get_class($annot)] = $annot;
     }
     return $annotations;
 }
Example #10
0
 /**
  * {@inheritdoc}
  */
 public function isClassInjected(\ReflectionClass $class)
 {
     foreach ($this->reader->getClassAnnotations($class) as $annotation) {
         if ($annotation instanceof Inject) {
             return true;
         }
     }
     return false;
 }
Example #11
0
 /**
  * Annotations specifically related to Entity Transformation as defined in the TransformMapping class
  *
  * @param $class
  * @return null | TransformMapping
  */
 public function getTransformAnnotations($class)
 {
     $annotations = $this->reader->getClassAnnotations(new \ReflectionClass($class));
     foreach ($annotations as $annotation) {
         if ($annotation instanceof TransformMapping) {
             return $annotation;
         }
     }
     return null;
 }
 /**
  * Get the annotation from a class or null if it doesn't exists.
  *
  * @param EntityManagerInterface $em
  * @param mixed                  $entity
  * @return bool
  */
 public function isTracked(EntityManagerInterface $em, $entity)
 {
     $class = get_class($entity);
     $annotations = $this->reader->getClassAnnotations($em->getClassMetadata($class)->getReflectionClass());
     foreach ($annotations as $annotation) {
         if ($annotation instanceof Tracked) {
             return true;
         }
     }
     return false;
 }
 /**
  * Get object mapping annotation for class
  *
  * @param \ReflectionClass $refClass
  * @param string           $group
  *
  * @return Object|null
  */
 private function getObjectMappingAnnotation(\ReflectionClass $refClass, $group)
 {
     $classAnnotations = $this->reader->getClassAnnotations($refClass);
     foreach ($classAnnotations as $classAnnotation) {
         if ($classAnnotation instanceof Object) {
             if ($classAnnotation->group == $group) {
                 return $classAnnotation;
             }
         }
     }
     return null;
 }
Example #14
0
 /**
  * {@inheritDoc}
  */
 public function getClassAnnotations(\ReflectionClass $class)
 {
     $cacheKey = $class->getName();
     if (isset($this->loadedAnnotations[$cacheKey])) {
         return $this->loadedAnnotations[$cacheKey];
     }
     if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
         $annots = $this->delegate->getClassAnnotations($class);
         $this->saveToCache($cacheKey, $annots);
     }
     return $this->loadedAnnotations[$cacheKey] = $annots;
 }
 /**
  * Annotations specifically related to Entity Transformation as defined in the Proxy class
  *
  * @param $class
  * @return null | Proxy
  */
 public function getProxyAnnotations($class)
 {
     if ($this->doctrineProxyResolver->isDoctrineProxy($class)) {
         $class = $this->doctrineProxyResolver->unwrapDoctrineProxyClass($class);
     }
     $annotations = $this->reader->getClassAnnotations(new \ReflectionClass($class));
     foreach ($annotations as $annotation) {
         if ($annotation instanceof Proxy) {
             return $annotation;
         }
     }
     return null;
 }
Example #16
0
 /**
  * Modifies the Request object to apply configuration information found in
  * controllers annotations like the template to render or HTTP caching
  * configuration.
  *
  * @param FilterControllerEvent $event A FilterControllerEvent instance
  */
 public function onKernelController(FilterControllerEvent $event)
 {
     if (!is_array($controller = $event->getController())) {
         return;
     }
     $object = new \ReflectionObject($controller[0]);
     $method = $object->getMethod($controller[1]);
     $request = $event->getRequest();
     foreach (array_merge($this->reader->getClassAnnotations($object), $this->reader->getMethodAnnotations($method)) as $configuration) {
         if ($configuration instanceof ConfigurationInterface) {
             $request->attributes->set('_' . $configuration->getAliasName(), $configuration);
         }
     }
 }
 /**
  * @return array|null
  */
 public function getClassAnnotations()
 {
     if (null === $this->classAnnotations) {
         $this->classAnnotations = $this->reader->getClassAnnotations($this->reflectionClass);
     }
     return $this->classAnnotations;
 }
Example #18
0
 /**
  * {@inheritDoc}
  */
 public function loadMetadataForClass($className, ServiceMetadata $serviceMetadata)
 {
     // get all class annotations
     $reflClass = new \ReflectionClass($className);
     $classAnnotations = $this->reader->getClassAnnotations($reflClass);
     $classAnnotations = $this->indexAnnotationsByType($classAnnotations);
     // load headline service information
     $serviceAnnotation = $this->getAnnotation($classAnnotations, self::ANNOTATION_SERVICE);
     if ($serviceAnnotation === false) {
         throw Exception::classIsNotMappedService($className);
     }
     // process events
     foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
         $methodAnnotations = $this->reader->getMethodAnnotations($reflMethod);
         $methodAnnotations = $this->indexAnnotationsByType($methodAnnotations);
         // process listeners
         $listenerAnnotations = $this->getAnnotation($methodAnnotations, self::ANNOTATION_LISTENER);
         if ($listenerAnnotations !== false) {
             if (!is_array($listenerAnnotations)) {
                 $listenerAnnotations = array($listenerAnnotations);
             }
             foreach ($listenerAnnotations as $listenerAnnotation) {
                 // ensure we don't double-up listener entries
                 $existingListeners = $serviceMetadata->getListeners($listenerAnnotation->event);
                 if (!in_array($reflMethod->getName(), $existingListeners)) {
                     $serviceMetadata->addListener($listenerAnnotation->event, $reflMethod->getName());
                 }
             }
         }
     }
 }
 private static function readFormType(\Doctrine\Common\Annotations\Reader $reader, \ReflectionClass $rc, array &$out, array &$non_deep_fields)
 {
     $as = $reader->getClassAnnotations($rc);
     $annot = null;
     if ($as) {
         foreach ($as as $_annot) {
             if ($_annot instanceof \Symforce\AdminBundle\Compiler\Annotation\FormType) {
                 if (null !== $annot) {
                     throw new \Exception(sprintf("sf_admin.form.type ( class: %s ) has multi form annotation", $rc->getName()));
                 }
                 $annot = $_annot;
             }
         }
     }
     if ($annot) {
         foreach ($annot as $key => $value) {
             if (!isset($out[$key]) || null === $out[$key]) {
                 if (!$out['deep'] || !in_array($key, $non_deep_fields)) {
                     $out[$key] = $value;
                 }
             }
         }
     }
     $parent = $rc->getParentClass();
     if ($parent && !$parent->isAbstract()) {
         $out['deep']++;
         self::readFormType($reader, $parent, $out, $non_deep_fields);
     }
 }
 /**
  * Retrieves the attachments of the specified object
  *
  * @param mixed $object
  * @return WebDev\AttachmentBundle\Attachement\File[]
  */
 public function files($object)
 {
     if (!is_object($object)) {
         throw new Exception("Can't retrieve the attachments of a " . get_type($object));
     }
     $class = new ReflectionClass($object);
     $files = array();
     foreach ($this->reader->getClassAnnotations($class) as $annotation) {
         if (!$annotation instanceof FileAttachment) {
             continue;
         }
         $file = new File($annotation, $object, $this);
         $files[$file->getName()] = $file;
     }
     return $files;
 }
Example #21
0
 /**
  * {@inheritDoc}
  */
 public function loadMetadataForClass($className, ResourceMetadata $resourceMetadata)
 {
     // get all class annotations
     $reflClass = new \ReflectionClass($className);
     $classAnnotations = $this->reader->getClassAnnotations($reflClass);
     $classAnnotations = $this->indexAnnotationsByType($classAnnotations);
     $resourceAnnotation = $this->getAnnotation($classAnnotations, 'BedRest\\Resource\\Mapping\\Annotation\\Resource');
     if ($resourceAnnotation !== false) {
         if (!empty($resourceAnnotation->name)) {
             $resourceMetadata->setName($resourceAnnotation->name);
         } else {
             $resourceName = Inflector::tableize(substr($className, strrpos($className, '\\') + 1));
             $resourceMetadata->setName($resourceName);
         }
     }
     $handlerAnnotation = $this->getAnnotation($classAnnotations, 'BedRest\\Resource\\Mapping\\Annotation\\Handler');
     if ($handlerAnnotation !== false) {
         if (!empty($handlerAnnotation->service)) {
             $resourceMetadata->setService($handlerAnnotation->service);
         }
     }
     // properties
     $subResources = array();
     foreach ($reflClass->getProperties() as $reflProp) {
         $propAnnotations = $this->reader->getPropertyAnnotations($reflProp);
         $propAnnotations = $this->indexAnnotationsByType($propAnnotations);
         $subResourceAnnotation = $this->getAnnotation($propAnnotations, 'BedRest\\Resource\\Mapping\\Annotation\\SubResource');
         if ($subResourceAnnotation !== false) {
             $subResources[$subResourceAnnotation->name] = array('fieldName' => $reflProp->name, 'service' => $subResourceAnnotation->service);
         }
     }
     $resourceMetadata->setSubResources($subResources);
 }
Example #22
0
 public function getClassAnnotations(\ReflectionClass $class)
 {
     $cacheKey = $class->getName() . self::$CACHE_SALT;
     if (isset($this->loadedAnnotations[$cacheKey])) {
         return $this->loadedAnnotations[$cacheKey];
     }
     // Attempt to grab data from cache
     if (($data = $this->cache->fetch($cacheKey)) !== false) {
         if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
             return $data;
         }
     }
     $annots = $this->delegate->getClassAnnotations($class);
     $this->cache->save($cacheKey, $annots);
     $this->cache->save('[C]' . $cacheKey, time());
     return $this->loadedAnnotations[$cacheKey] = $annots;
 }
 /**
  * @param \ReflectionClass $class
  *
  * @return \Metadata\ClassMetadata
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $classMetadata = new ClassMetadata($class->name);
     $classMetadata->fileResources[] = $class->getFilename();
     foreach ($this->reader->getClassAnnotations($class) as $annotation) {
         if ($annotation instanceof Schema) {
             $classMetadata->schema = $annotation->name;
         }
         if ($annotation instanceof Inherit) {
             $classMetadata->inherit = $annotation->name;
         }
     }
     if (null !== $classMetadata->schema) {
         return $classMetadata;
     }
     return null;
 }
 /**
  * {@inheritdoc}
  */
 public function loadClassMetadata(ClassMetadata $metadata)
 {
     $reflClass = $metadata->getReflectionClass();
     $className = $reflClass->name;
     $success = false;
     foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
         if ($constraint instanceof GroupSequence) {
             $metadata->setGroupSequence($constraint->groups);
         } elseif ($constraint instanceof GroupSequenceProvider) {
             $metadata->setGroupSequenceProvider(true);
         } elseif ($constraint instanceof Constraint) {
             $metadata->addConstraint($constraint);
         }
         $success = true;
     }
     foreach ($reflClass->getProperties() as $property) {
         if ($property->getDeclaringClass()->name == $className) {
             foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
                 if ($constraint instanceof Constraint) {
                     $metadata->addPropertyConstraint($property->name, $constraint);
                 }
                 $success = true;
             }
         }
     }
     foreach ($reflClass->getMethods() as $method) {
         if ($method->getDeclaringClass()->name == $className) {
             foreach ($this->reader->getMethodAnnotations($method) as $constraint) {
                 if ($constraint instanceof Callback) {
                     $constraint->callback = $method->getName();
                     $constraint->methods = null;
                     $metadata->addConstraint($constraint);
                 } elseif ($constraint instanceof Constraint) {
                     if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
                         $metadata->addGetterConstraint(lcfirst($matches[2]), $constraint);
                     } else {
                         throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name));
                     }
                 }
                 $success = true;
             }
         }
     }
     return $success;
 }
 private function parseSingleClassFile($className)
 {
     //annotation on class
     $reflectionClass = new \ReflectionClass($className);
     $classAnnotations = $this->annotationReader->getClassAnnotations($reflectionClass);
     if (!$classAnnotations) {
         return;
     }
     foreach ($classAnnotations as $classAnnotation) {
         //name not set => bean name className?
         if (!isset($classAnnotation->name) or $classAnnotation->name == '') {
             $beanClassName = lcfirst(join('', array_slice(explode('\\', $className), -1)));
             $classAnnotation->name = $beanClassName;
         }
         $bean = array('name' => $classAnnotation->name, 'class' => $className, 'scope' => $classAnnotation->scope, 'lazyInit' => (string) $classAnnotation->lazyInit);
         //annotation on method
         $methods = $reflectionClass->getMethods();
         foreach ($methods as $method) {
             if ($method->name == self::CONSTRUCT_METHOD) {
                 //annotaiton on construct
                 $bean = $this->analyzeConstruct($className, $method, $bean);
             } else {
                 $reflectionMethod = new \ReflectionMethod($className, $method->name);
                 $methodAnnotations = $this->annotationReader->getMethodAnnotations($reflectionMethod);
                 if ($methodAnnotations) {
                     foreach ($methodAnnotations as $pathToClassFile) {
                         if (isset($method->name)) {
                             $bean[get_class($pathToClassFile)] = $method->name;
                         }
                     }
                 }
             }
         }
         //annotation on properties
         $properties = $reflectionClass->getProperties();
         $allProperties = array();
         $temp = array();
         foreach ($properties as $property) {
             $reflectionProperty = new \ReflectionProperty($className, $property->name);
             $propertyAnnotations = $this->annotationReader->getPropertyAnnotations($reflectionProperty);
             if ($propertyAnnotations) {
                 foreach ($propertyAnnotations as $pathToClassFile) {
                     $allProperties[$property->name] = array(get_class($pathToClassFile) => $pathToClassFile->name);
                     if ($pathToClassFile->ref) {
                         $temp[] = array('name' => $property->name, 'ref' => $pathToClassFile->ref);
                     } else {
                         $temp[] = array('name' => $property->name, 'value' => $this->replaceVariable($pathToClassFile->value), 'type' => $pathToClassFile->type);
                     }
                 }
             }
             if ($temp) {
                 $bean['properties'] = $temp;
             }
         }
         $this->context[$classAnnotation->name] = $bean;
     }
 }
Example #26
0
 /**
  * @param string $path Directory path to compile
  */
 public function compile($path)
 {
     $this->loader->setCallback(function ($class, $file) {
         $reflClass = new \ReflectionClass($class);
         //Ignore abstract classes
         if ($reflClass->isAbstract()) {
             return;
         }
         $classAnnotations = $this->reader->getClassAnnotations($reflClass);
         $this->compileClass($reflClass, $classAnnotations);
         foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
             /** @var $reflMethod \ReflectionMethod */
             $methodAnnotations = $this->reader->getMethodAnnotations($reflMethod);
             $this->compileMethod($reflClass, $reflMethod, $methodAnnotations);
         }
     });
     $this->loader->load($path);
 }
Example #27
0
 /**
  * @inheritDoc
  */
 public function get(\ReflectionFunctionAbstract $controllerReflector)
 {
     if (!$controllerReflector instanceof \ReflectionMethod) {
         throw new \InvalidArgumentException("Reading annotations from functions other than class methods is not supported yet");
     }
     $methodAnnots = $this->annotationReader->getMethodAnnotations($controllerReflector);
     $classAnnots = $this->annotationReader->getClassAnnotations($controllerReflector->getDeclaringClass());
     $annots = array_merge($classAnnots, $methodAnnots);
     $params = [];
     foreach ($annots as $annotation) {
         if (!$annotation instanceof Param) {
             continue;
         }
         $this->resolve($annotation);
         $params[$annotation->name] = $annotation;
     }
     return $params;
 }
 /**
  * @test
  */
 public function load_class_loads_metadata_correctly()
 {
     $reflClass = new \ReflectionClass('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\SimpleObject');
     $metadata = new ClassMetadata($reflClass);
     $this->reader->getClassAnnotations($reflClass)->willReturn([new ClassAnnot(), new NotHandledAnnotation()]);
     $this->reader->getMethodAnnotations($reflClass->getMethod('getAuthor'))->willReturn([new NotHandledAnnotation(), new MethodAnnotation1(), new MethodAnnotation2()]);
     $this->reader->getPropertyAnnotations($reflClass->getProperty('createdAt'))->willReturn([new NotHandledAnnotation()]);
     $this->reader->getPropertyAnnotations($reflClass->getProperty('author'))->willReturn([]);
     $this->processorFactory->getProcessor(Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\NotHandledAnnotation'))->willReturn();
     $classAnnotationProcessor = $this->prophesize('Kcs\\Metadata\\Loader\\Processor\\ProcessorInterface');
     $classAnnotationProcessor->process(Argument::type('Kcs\\Metadata\\MetadataInterface'), Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\ClassAnnot'))->shouldBeCalledTimes(1);
     $this->processorFactory->getProcessor(Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\ClassAnnot'))->willReturn($classAnnotationProcessor->reveal());
     $methodAnnotationProcessor = $this->prophesize('Kcs\\Metadata\\Loader\\Processor\\ProcessorInterface');
     $methodAnnotationProcessor->process(Argument::type('Kcs\\Metadata\\MetadataInterface'), Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\MethodAnnotation1'))->shouldBeCalledTimes(1);
     $methodAnnotationProcessor->process(Argument::type('Kcs\\Metadata\\MetadataInterface'), Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\MethodAnnotation2'))->shouldBeCalledTimes(1);
     $this->processorFactory->getProcessor(Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\MethodAnnotation1'))->willReturn($methodAnnotationProcessor->reveal());
     $this->processorFactory->getProcessor(Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\MethodAnnotation2'))->willReturn($methodAnnotationProcessor->reveal());
     $this->loader->loadClassMetadata($metadata);
 }
 /**
  * Whether the class with the specified name is transient. Only non-transient
  * classes, that is entities and mapped superclasses, should have their metadata loaded.
  * A class is non-transient if it is annotated with either @Entity or
  * @MappedSuperclass in the class doc block.
  *
  * @param string $className
  * @return boolean
  */
 public function isTransient($className)
 {
     $classAnnotations = $this->reader->getClassAnnotations(new \ReflectionClass($className));
     foreach ($classAnnotations as $annot) {
         if ($annot instanceof ODM\AbstractDocument) {
             return false;
         }
     }
     return true;
 }
 /**
  * Modifies the Request object to apply configuration information found in
  * controllers annotations like the template to render or HTTP caching
  * configuration.
  *
  * @param FilterControllerEvent $event A FilterControllerEvent instance
  */
 public function onKernelController(FilterControllerEvent $event)
 {
     if (!is_array($controller = $event->getController())) {
         return;
     }
     $className = class_exists('Doctrine\\Common\\Util\\ClassUtils') ? ClassUtils::getClass($controller[0]) : get_class($controller[0]);
     $object = new \ReflectionClass($className);
     $method = $object->getMethod($controller[1]);
     $classConfigurations = $this->getConfigurations($this->reader->getClassAnnotations($object));
     $methodConfigurations = $this->getConfigurations($this->reader->getMethodAnnotations($method));
     $configurations = array_merge($classConfigurations, $methodConfigurations);
     $request = $event->getRequest();
     foreach ($configurations as $key => $attributes) {
         if (is_array($attributes) && count($attributes) == 1) {
             $attributes = $attributes[0];
         }
         $request->attributes->set($key, $attributes);
     }
 }