TripalVocabService_v0_1.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. class TripalVocabService_v0_1 extends TripalWebService {
  3. /**
  4. * The human-readable label for this web service.
  5. */
  6. public static $label = 'Vocabulary';
  7. /**
  8. * A bit of text to describe what this service provides.
  9. */
  10. public static $description = 'Provides access to vocabulary terms that are in use by this site.';
  11. /**
  12. * A machine-readable type for this service. This name must be unique
  13. * among all Tripal web services and is used to form the URL to access
  14. * this service.
  15. */
  16. public static $type = 'vocab';
  17. /**
  18. * The list of web services.
  19. */
  20. private $services;
  21. /**
  22. * Constructor for the TripalVocabService_v0_1 class.
  23. */
  24. public function __construct($base_path) {
  25. parent::__construct($base_path);
  26. // Get the list of services provided by this Tripal site.
  27. $this->services = tripal_get_web_services();
  28. foreach ($this->services as $sindex => $service_class) {
  29. // Load the class code.
  30. tripal_load_include_web_service_class($service_class);
  31. // Remove this class from the list of services.
  32. if ($service_class::$type == 'vocab') {
  33. unset($this->services[$sindex]);
  34. }
  35. }
  36. }
  37. /**
  38. * @see TripalWebService::handleRequest()
  39. */
  40. public function handleRequest() {
  41. // Create the vocabulary resource.
  42. $this->resource->addContextItem('vocab', $this->getServicePath() . '#');
  43. $this->resource->addContextItem('apiDocumentation', 'hydra:apiDocumentation');
  44. $this->resource->addContextItem('supportedClass', 'hydra:supportedClass');
  45. $this->resource->setType('apiDocumentation');
  46. $this->resource->setID('vocab/' . $this->getVersion());
  47. // Add the EntryPoint class.
  48. $this->addEntryPointClass();
  49. foreach ($this->documentation as $supported) {
  50. $this->resource->addProperty('supportedClass', $supported);
  51. }
  52. // Iterate through all of the web services and build their documentation
  53. foreach ($this->services as $service_class) {
  54. $service = new $service_class($this->base_path);
  55. $supported_classes = $service->getDocumentation();
  56. foreach ($supported_classes as $supported) {
  57. $this->resource->addProperty('supportedClass', $supported);
  58. }
  59. }
  60. }
  61. /**
  62. * Generates the EntryPoint class for the API documents.
  63. *
  64. * @return TripalWebServiceResource
  65. *
  66. */
  67. private function addEntryPointClass(){
  68. $service_path = $this->getServicePath();
  69. $details = array(
  70. 'id' => $service_path . '#EntryPoint',
  71. 'title' => 'EntryPoint',
  72. 'description' => 'The main entry point or homepage of the API',
  73. 'subClassOf' => NULL,
  74. );
  75. // Add each service as a property.
  76. $properties = array();
  77. foreach ($this->services as $service_class) {
  78. $service = new $service_class($this->base_path);
  79. // Create a WebServiceResource for the hydra:Link type.
  80. $link = new TripalWebServiceResource($this->base_path);
  81. $link->setID('vocab:EntryPoint/' . $service::$type);
  82. $link->setType('hydra:Link');
  83. $link->addContextItem('domain', array(
  84. "@id" => "rdfs:domain",
  85. "@type" => "@id"
  86. ));
  87. $link->addContextItem('range', array(
  88. "@id" => "rdfs:range",
  89. "@type" => "@id"
  90. ));
  91. $link->addContextItem('readable', 'hydra:readable');
  92. $link->addContextItem('writeable', 'hydra:writeable');
  93. $link->addContextItem('required', 'hydra:required');
  94. $link->addContextItem('description', 'rdfs:comment');
  95. $link->addContextItem('label', 'rdfs:label');
  96. $link->addProperty('hydra:title', $service_class::$label);
  97. $link->addProperty('hydra:description', $service_class::$description);
  98. // $link->addProperty('domain', $service_path . '#EntryPoint');
  99. // $link->addProperty('range', $service_class::$label);
  100. $ops = array();
  101. $op = new TripalWebServiceResource($this->base_path);
  102. $op->setID('_:' . $service::$type . '_retrieve');
  103. $op->setType('hydra:Operation');
  104. $op->addContextItem('method', 'hydra:method');
  105. $op->addContextItem('label', 'rdfs:label');
  106. $op->addContextItem('description', 'rdfs:comment');
  107. $op->addContextItem('expects', array(
  108. "@id" => "hydra:expects",
  109. "@type" => "@id"
  110. ));
  111. $op->addContextItem('returns', array(
  112. "@id" => "hydra:returns",
  113. "@type" => "@id"
  114. ));
  115. $op->addContextItem('statusCodes', 'hydra:statusCodes');
  116. $op->addProperty('method', "GET");
  117. $op->addProperty('label', 'Retrieves the ' . $service_class::$label . ' resource.');
  118. $op->addProperty('description', NULL);
  119. $op->addProperty('expects', NULL);
  120. $op->addProperty('returns', 'vocab:EntryPoint/' . $service::$type);
  121. $op->addProperty('statusCodes', array());
  122. $ops[] = $op;
  123. $link->addContextItem('supportedOperation', 'hydra:supportedOperation');
  124. $link->addProperty('supportedOperation', $ops);
  125. $property = array(
  126. 'type' => $link,
  127. 'title' => $service_class::$label,
  128. 'description' => $service_class::$description,
  129. 'domain', 'vocab:EntryPoint',
  130. 'range', $service->getServicePath(),
  131. );
  132. $properties[] = $property;
  133. }
  134. $operations = array();
  135. $operations['GET'] = array(
  136. 'label' => "The APIs main entry point.",
  137. 'description' => NULL,
  138. 'expects' => NULL,
  139. 'returns' => $service_path . '#EntryPoint',
  140. 'type' => '_:entry_point_retrieve'
  141. );
  142. $this->addDocClass($details, $operations, $properties);
  143. }
  144. }