Exemple #1
0
 /**
  * Extracts the resource from a method's method_info
  *
  * @history
  * 2014.04.29:
  *   (AT)  Initial implementation
  * 2014.05.29:
  *   (AT)  Added short description to the resource
  *
  * @version 2014.05.29
  * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**>
  *
  * @param array $method_info
  *   Method information
  * @return \Cougar\RestService\Models\Resource Resource object
  */
 protected function extractResource(array $method_info)
 {
     // Initialize the resource variable
     $resource = null;
     if ($method_info["resource"]) {
         // See if we have a class
         if ($method_info["resource"]["class"]) {
             // Create the resource
             $resource = new Resource();
             $resource->class = $method_info["resource"]["class"];
             // See if we have an alternate name
             if ($method_info["resource"]["name"]) {
                 // Set the alternate resource name
                 $resource->name = $method_info["resource"]["name"];
             } else {
                 // Set the resource name from the class name
                 $resource->name = $this->generateResourceName($method_info["resource"]["class"]);
             }
         }
     }
     // See if we had a resource
     if (!$resource) {
         // See if we have a return annotation
         if ($method_info["return"]) {
             // See if the return type is a primitive
             if (!$this->isPrimitive($method_info["return"]["type"])) {
                 // Create the resource from the return type
                 $resource = new Resource();
                 $resource->class = $method_info["return"]["type"];
                 $resource->name = $this->generateResourceName($resource->class);
             }
         } else {
             if (count($method_info["param"]) == 1 && $method_info["param"][0]["type"] && !$this->isPrimitive($method_info["param"][0]["type"])) {
                 // Get the resource name from the first and only parameter
                 $resource = new Resource();
                 $resource->class = $method_info["param"][0]["type"];
                 $resource->name = $this->generateResourceName($resource->class);
             }
         }
     }
     // If we still don't have a resource, create a new unnamed one
     if (!$resource) {
         $resource = new Resource();
         $resource->name = "Resource-" . (count($this->resources) + 1);
     }
     // Set the Resource ID from the name
     $resource->resourceId = $resource->name;
     // Set the short description from the class name
     if ($resource->class) {
         try {
             $class_annotations = Annotations::extractFromObject($resource->class);
             foreach ($class_annotations->class as $annotation) {
                 if ($annotation->name == "_comment") {
                     // Extract the first sentence
                     $first_period_pos = mb_strpos($annotation->value, ".");
                     if ($first_period_pos) {
                         // The description is the first sentence
                         $resource->shortDescription = substr($annotation->value, 0, $first_period_pos);
                     } else {
                         // The entire comment is the description
                         $resource->shortDescription = $annotation->value;
                     }
                     break;
                 }
             }
         } catch (\Exception $e) {
             // Ignore the error
         }
     }
     # Return the resource
     return $resource;
 }
Exemple #2
0
 /**
  * @covers Cougar\Util\Annotations::extractFromObject
  */
 public function testExtractFromClassInheritedClassNotAllMembers()
 {
     # Mock the cache
     $local_cache = $this->getMock("\\Cougar\\Cache\\Cache");
     $local_cache->expects($this->any())->method("get")->will($this->returnValue(false));
     $local_cache->expects($this->any())->method("set")->will($this->returnValue(false));
     Annotations::$cache = $local_cache;
     # Extract the annotations from the class name
     $annotations = Annotations::extractFromObject(__NAMESPACE__ . "\\ExtendedBasicAnnotationTest", false);
     $this->assertCount(1, $annotations->class);
     $this->assertEquals(new Annotation("ChildAnnotation", "Child 1"), $annotations->class[0]);
     $this->assertCount(2, $annotations->properties);
     $this->assertArrayHasKey("propertyB", $annotations->properties);
     $this->assertCount(2, $annotations->properties["propertyB"]);
     $this->assertEquals(new Annotation("PropertyB", "Annotation for Property B override"), $annotations->properties["propertyB"][0]);
     $this->assertEquals(new Annotation("var", "string Some value"), $annotations->properties["propertyB"][1]);
     $this->assertArrayHasKey("propertyC", $annotations->properties);
     $this->assertCount(2, $annotations->properties["propertyC"]);
     $this->assertEquals(new Annotation("PropertyC", "Annotation for Property C"), $annotations->properties["propertyC"][0]);
     $this->assertEquals(new Annotation("var", "string Some value"), $annotations->properties["propertyC"][1]);
     $this->assertCount(2, $annotations->methods);
     $this->assertArrayHasKey("methodX", $annotations->methods);
     $this->assertCount(1, $annotations->methods["methodX"]);
     $this->assertEquals(new Annotation("MethodX", "Annotation for Method X"), $annotations->methods["methodX"][0]);
     $this->assertArrayHasKey("methodY", $annotations->methods);
     $this->assertCount(1, $annotations->methods["methodY"]);
     $this->assertEquals(new Annotation("MethodY", "Annotation for Method Y override"), $annotations->methods["methodY"][0]);
 }