Ejemplo n.º 1
0
 /**
  * Load metadata for a class name
  * @param  object|string         $class - Pass in either the class name, or an instance of that class
  * @return Mapping\ClassMetaData $metaData - return null if metadata couldn't be populated from annotations
  * @throws DrestException
  */
 public function loadMetadataForClass($class)
 {
     if (is_string($class)) {
         $metadata = new Mapping\ClassMetaData(new \ReflectionClass($class));
     } else {
         $metadata = new Mapping\ClassMetaData($class);
     }
     if (!isset($this->classes[$class])) {
         throw new \RuntimeException('The class is not set: ' . $class);
     }
     $resource = $this->classes[$class];
     if ($resource['routes'] === null) {
         throw DrestException::annotatedResourceRequiresAtLeastOneServiceDefinition($resource['name']);
     }
     if (is_array($resource['representations'])) {
         $metadata->addRepresentations($resource['representations']);
     }
     $this->processRoutes($resource['routes'], $metadata);
     $this->processMethods($resource, $metadata);
     $this->checkHandleCalls($metadata->getRoutesMetaData());
     return $metadata;
 }
Ejemplo n.º 2
0
 /**
  * Load metadata for a class name
  * @param  object|string         $class - Pass in either the class name, or an instance of that class
  * @return Mapping\ClassMetaData $metaData - return null if metadata couldn't be populated from annotations
  * @throws DrestException
  */
 public function loadMetadataForClass($class)
 {
     $resourceFound = false;
     if (is_string($class)) {
         $class = new \ReflectionClass($class);
     }
     $metadata = new Mapping\ClassMetaData($class);
     foreach ($this->reader->getClassAnnotations($class) as $annotatedObject) {
         if ($annotatedObject instanceof Annotation\Resource) {
             $resourceFound = true;
             if ($annotatedObject->routes === null) {
                 throw DrestException::annotatedResourceRequiresAtLeastOneServiceDefinition($class->name);
             }
             if (is_array($annotatedObject->representations)) {
                 $metadata->addRepresentations($annotatedObject->representations);
             }
             $this->processRoutes($annotatedObject->routes, $metadata);
             $this->processMethods($class->getMethods(), $metadata);
             $this->checkHandleCalls($metadata->getRoutesMetaData());
         }
     }
     return $resourceFound ? $metadata : null;
 }
Ejemplo n.º 3
0
 /**
  * Load metadata for a class name
  * @param  object|string         $className - Pass in either the class name, or an instance of that class
  * @return Mapping\ClassMetaData $metaData - return null if metadata couldn't be populated from annotations
  * @throws DrestException
  */
 public function loadMetadataForClass($className)
 {
     if (!$this->classesLoaded) {
         $this->getAllClassNames();
         $this->classesLoaded = true;
     }
     $class = new \ReflectionClass($className);
     $metadata = new Mapping\ClassMetaData($class);
     if (!isset($this->classes[$className])) {
         return null;
     }
     $resource = $this->classes[$className];
     if ($resource['routes'] === null) {
         throw DrestException::annotatedResourceRequiresAtLeastOneServiceDefinition($resource['name']);
     }
     if (is_array($resource['representations'])) {
         $metadata->addRepresentations($resource['representations']);
     }
     $this->processRoutes($resource['routes'], $metadata);
     $this->processMethods($resource, $metadata);
     $this->checkHandleCalls($metadata->getRoutesMetaData());
     return $metadata;
 }
Ejemplo n.º 4
0
 /**
  * Load metadata for a class name
  * @param  object|string         $class - Pass in either the class name, or an instance of that class
  * @return Mapping\ClassMetaData $metaData - return null if metadata couldn't be populated from annotations
  * @throws DrestException
  */
 public function loadMetadataForClass($class)
 {
     $resourceFound = false;
     if (is_string($class)) {
         $class = new \ReflectionClass($class);
     }
     $metadata = new Mapping\ClassMetaData($class);
     foreach ($this->reader->getClassAnnotations($class) as $annotatedObject) {
         if ($annotatedObject instanceof Annotation\Resource) {
             $resourceFound = true;
             $originFound = false;
             if ($annotatedObject->routes === null) {
                 throw DrestException::annotatedResourceRequiresAtLeastOneServiceDefinition($class->name);
             }
             $metadata->addRepresentations($annotatedObject->representations);
             foreach ($annotatedObject->routes as $route) {
                 $routeMetaData = new Mapping\RouteMetaData();
                 // Set name
                 $route->name = preg_replace("/[^a-zA-Z0-9_\\s]/", "", $route->name);
                 if ($route->name == '') {
                     throw DrestException::routeNameIsEmpty();
                 }
                 if ($metadata->getRoutesMetaData($route->name) !== false) {
                     throw DrestException::routeAlreadyDefinedWithName($class->name, $route->name);
                 }
                 $routeMetaData->setName($route->name);
                 // Set verbs (will throw if invalid)
                 if (isset($route->verbs)) {
                     $routeMetaData->setVerbs($route->verbs);
                 }
                 if (isset($route->collection)) {
                     $routeMetaData->setCollection($route->collection);
                 }
                 // Add the route pattern
                 $routeMetaData->setRoutePattern($route->routePattern);
                 if (is_array($route->routeConditions)) {
                     $routeMetaData->setRouteConditions($route->routeConditions);
                 }
                 // Set the exposure array
                 if (is_array($route->expose)) {
                     $routeMetaData->setExpose($route->expose);
                 }
                 // Set the allow options value
                 if (isset($route->allowOptions)) {
                     $routeMetaData->setAllowedOptionRequest($route->allowOptions);
                 }
                 // Add action class
                 if (isset($route->action)) {
                     $routeMetaData->setActionClass($route->action);
                 }
                 // If the origin flag is set, set the name on the class meta data
                 if (!is_null($route->origin)) {
                     if ($originFound) {
                         throw DrestException::resourceCanOnlyHaveOneRouteSetAsOrigin();
                     }
                     $metadata->originRouteName = $route->name;
                     $originFound = true;
                 }
                 $metadata->addRouteMetaData($routeMetaData);
             }
             // Set the handle calls
             foreach ($class->getMethods() as $method) {
                 /* @var \ReflectionMethod $method */
                 if ($method->isPublic()) {
                     foreach ($this->reader->getMethodAnnotations($method) as $methodAnnotation) {
                         if ($methodAnnotation instanceof Annotation\Handle) {
                             // Make sure the for is not empty
                             if (empty($methodAnnotation->for) || !is_string($methodAnnotation->for)) {
                                 throw DrestException::handleForCannotBeEmpty();
                             }
                             if (($routeMetaData = $metadata->getRoutesMetaData($methodAnnotation->for)) === false) {
                                 throw DrestException::handleAnnotationDoesntMatchRouteName($methodAnnotation->for);
                             }
                             if ($routeMetaData->hasHandleCall()) {
                                 // There is already a handle set for this route
                                 throw DrestException::alreadyHandleDefinedForRoute($routeMetaData);
                             }
                             $routeMetaData->setHandleCall($method->getName());
                             $routeMetaData->setInjectRequestIntoHandle($methodAnnotation->injectRequest);
                         }
                     }
                 }
             }
             // Error for any push metadata routes that don't have a handle
             foreach ($metadata->getRoutesMetaData() as $routeMetaData) {
                 /* @var RouteMetaData $routeMetaData */
                 if ($routeMetaData->needsHandleCall() && !$routeMetaData->hasHandleCall()) {
                     throw DrestException::routeRequiresHandle($routeMetaData->getName());
                 }
             }
         }
     }
     return $resourceFound ? $metadata : null;
 }