TripalDocService_v0_1.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. class TripalDocService_v0_1 extends TripalWebService {
  3. /**
  4. * The human-readable label for this web service.
  5. */
  6. public static $label = 'API Documentation';
  7. /**
  8. * A bit of text to describe what this service provides.
  9. */
  10. public static $description = 'Provides Hydra style documenation to make this RESTful webservice discoverable.';
  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 = 'doc';
  17. /**
  18. * The list of web services.
  19. */
  20. private $services;
  21. /**
  22. * Constructor for the TripalDocService_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('doc/' . $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. 'term' => 'vocab:EntryPoint',
  72. 'title' => 'EntryPoint',
  73. 'description' => 'The main entry point or homepage of the API',
  74. 'subClassOf' => NULL,
  75. );
  76. // Add each service as a property.
  77. $properties = array();
  78. foreach ($this->services as $service_class) {
  79. $service = new $service_class($this->base_path);
  80. // Create a WebServiceResource for the hydra:Link type.
  81. $link = new TripalWebServiceResource($this->base_path);
  82. $link->setID('vocab:EntryPoint/' . $service::$type);
  83. $link->setType('hydra:Link');
  84. $link->addContextItem('domain', array(
  85. "@id" => "rdfs:domain",
  86. "@type" => "@id"
  87. ));
  88. $link->addContextItem('range', array(
  89. "@id" => "rdfs:range",
  90. "@type" => "@id"
  91. ));
  92. $link->addContextItem('readable', 'hydra:readable');
  93. $link->addContextItem('writeable', 'hydra:writeable');
  94. $link->addContextItem('required', 'hydra:required');
  95. $link->addContextItem('description', 'rdfs:comment');
  96. $link->addContextItem('label', 'rdfs:label');
  97. $link->addProperty('hydra:title', $service_class::$label);
  98. $link->addProperty('hydra:description', $service_class::$description);
  99. // $link->addProperty('domain', $service_path . '#EntryPoint');
  100. // $link->addProperty('range', $service_class::$label);
  101. $ops = array();
  102. $op = new TripalWebServiceResource($this->base_path);
  103. $op->setID('_:' . $service::$type . '_retrieve');
  104. $op->setType('hydra:Operation');
  105. $op->addContextItem('method', 'hydra:method');
  106. $op->addContextItem('label', 'rdfs:label');
  107. $op->addContextItem('description', 'rdfs:comment');
  108. $op->addContextItem('expects', array(
  109. "@id" => "hydra:expects",
  110. "@type" => "@id"
  111. ));
  112. $op->addContextItem('returns', array(
  113. "@id" => "hydra:returns",
  114. "@type" => "@id"
  115. ));
  116. $op->addContextItem('statusCodes', 'hydra:statusCodes');
  117. $op->addProperty('method', "GET");
  118. $op->addProperty('label', 'Retrieves the ' . $service_class::$label . ' resource.');
  119. $op->addProperty('description', NULL);
  120. $op->addProperty('expects', NULL);
  121. $op->addProperty('returns', 'local:EntryPoint/' . $service::$type);
  122. $op->addProperty('statusCodes', array());
  123. $ops[] = $op;
  124. $link->addContextItem('supportedOperation', 'hydra:supportedOperation');
  125. $link->addProperty('supportedOperation', $ops);
  126. $property = array(
  127. 'type' => $link,
  128. 'title' => $service_class::$label,
  129. 'description' => $service_class::$description,
  130. 'domain', 'vocab:EntryPoint',
  131. 'range', $service->getServicePath(),
  132. );
  133. $properties[] = $property;
  134. }
  135. $operations = array();
  136. $operations['GET'] = array(
  137. 'label' => "The APIs main entry point.",
  138. 'description' => NULL,
  139. 'expects' => NULL,
  140. 'returns' => $service_path . '#EntryPoint',
  141. 'type' => '_:entry_point_retrieve'
  142. );
  143. $this->addDocClass($details, $operations, $properties);
  144. }
  145. }