|
| 1 | +<?php |
| 2 | +/*========================================================================= |
| 3 | +MIDAS Server |
| 4 | +Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved. |
| 5 | +69328 Lyon, FRANCE. |
| 6 | +
|
| 7 | +See Copyright.txt for details. |
| 8 | +This software is distributed WITHOUT ANY WARRANTY; without even |
| 9 | +the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 10 | +PURPOSE. See the above copyright notices for more information. |
| 11 | +=========================================================================*/ |
| 12 | + |
| 13 | +/** |
| 14 | + * Notification class that allows the web api to be automatically |
| 15 | + * initialized. |
| 16 | + */ |
| 17 | +class ApiEnabled_Notification extends MIDAS_Notification |
| 18 | + { |
| 19 | + |
| 20 | + /** |
| 21 | + * This function is for getting the webapi methods defined in the API |
| 22 | + * component of the implementing class. To enable this add the following |
| 23 | + * line to your init function. |
| 24 | + * |
| 25 | + * $this->enableWebAPI(); |
| 26 | + * |
| 27 | + */ |
| 28 | + public function getWebApiMethods() |
| 29 | + { |
| 30 | + $methods = array(); |
| 31 | + $r = new ReflectionClass($this->ModuleComponent->Api); |
| 32 | + $meths = $r->getMethods(ReflectionMethod::IS_PUBLIC); |
| 33 | + foreach( $meths as $m ) |
| 34 | + { |
| 35 | + if(strpos($m->getDeclaringClass()->getName(),'ApiComponent')) |
| 36 | + { |
| 37 | + $realName = $m->getName(); |
| 38 | + $docString = $m->getDocComment(); |
| 39 | + $docString = trim($docString,'/'); |
| 40 | + $docAttributes = explode('@',$docString); |
| 41 | + $return = ''; |
| 42 | + $description = ''; |
| 43 | + $params = array(); |
| 44 | + $example = array(); |
| 45 | + foreach( $docAttributes as $docEntry ) |
| 46 | + { |
| 47 | + $explodedDoc = explode('*',$docEntry); |
| 48 | + array_walk($explodedDoc, |
| 49 | + create_function('&$val', '$val = trim($val);')); |
| 50 | + $doc = implode('',$explodedDoc); |
| 51 | + if(strpos($doc,'param') === 0) |
| 52 | + { |
| 53 | + $splitParam = explode(' ',$doc); |
| 54 | + $paramName = trim($splitParam[1]); |
| 55 | + $paramValue = trim(implode(' ',array_slice($splitParam,2))); |
| 56 | + $params[$paramName] = $paramValue; |
| 57 | + } |
| 58 | + elseif(strpos($doc,'return') === 0) |
| 59 | + { |
| 60 | + $return = trim(substr($doc,6)); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + $description = $doc; |
| 65 | + } |
| 66 | + } |
| 67 | + $name = strtolower($realName); |
| 68 | + $help = array(); |
| 69 | + $help['params'] = $params; |
| 70 | + $help['example'] = $example; |
| 71 | + $help['return'] = $return; |
| 72 | + $help['description'] = $description; |
| 73 | + $methods[] = array('name'=> $name, |
| 74 | + 'help' => $help, |
| 75 | + 'callbackObject' => &$this->ModuleComponent->Api, |
| 76 | + 'callbackFunction' => $realName); |
| 77 | + } |
| 78 | + } |
| 79 | + return $methods; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Add to your init function to enable the web api for your module. This will |
| 84 | + * work provided you've created an ApiComponent. |
| 85 | + */ |
| 86 | + public function enableWebAPI() |
| 87 | + { |
| 88 | + $this->addCallBack('CALLBACK_API_METHODS', 'getWebApiMethods'); |
| 89 | + } |
| 90 | +} |
0 commit comments