/** * Displays available options and parameters this component offers. * * @apiMethod GET * @apiUri / * @return void */ public function indexTask() { // var to hold output $output = new stdClass(); $output->component = substr($this->_option, 4); $bits = explode('v', get_class($this)); $output->version = str_replace('_', '.', end($bits)); $output->tasks = array(); $output->errors = array(); // create reflection class of file $classReflector = new ReflectionClass($this); // loop through each method and process doc foreach ($classReflector->getMethods() as $method) { // create docblock object & make sure we have something $phpdoc = new \phpDocumentor\Reflection\DocBlock($method); // skip constructor if (substr($method->getName(), -4) != 'Task' || in_array($method->getName(), array('registerTask', 'unregisterTask'))) { continue; } // skip method in the parent class (already processed), /*if ($className != $method->getDeclaringClass()->getName()) { //continue; }*/ // skip if we dont have a short desc // but put in error if (!$phpdoc->getShortDescription()) { $output->errors[] = sprintf('Missing docblock for method "%s" in "%s"', $method->getName(), str_replace(PATH_ROOT, '', $classReflector->getFileName())); continue; } // create endpoint data array $endpoint = array('name' => substr($method->getName(), 0, -4), 'description' => preg_replace('/\\s+/', ' ', $phpdoc->getShortDescription()), 'method' => '', 'uri' => '', 'parameters' => array()); // loop through each tag foreach ($phpdoc->getTags() as $tag) { // get tag name and content $name = strtolower(str_replace('api', '', $tag->getName())); $content = $tag->getContent(); // handle parameters separately // json decode param input if ($name == 'parameter') { $parameter = json_decode($content); if (json_last_error() != JSON_ERROR_NONE) { $output->errors[] = sprintf('Unable to parse parameter info for method "%s" in "%s"', $method->getName(), str_replace(PATH_ROOT, '', $classReflector->getFileName())); continue; } $endpoint['parameters'][] = (array) $parameter; continue; } if ($name == 'uri' && $method->getName() == 'indexTask') { $content .= $output->component; } // add data to endpoint data $endpoint[$name] = $content; } // add endpoint to output $output->tasks[] = $endpoint; } if (count($output->errors) <= 0) { unset($output->errors); } $this->send($output); }
public static function getPropsAsStringByReflectionMethod(\ReflectionMethod $method, $withoutValues = false) { $props = array(); if ($method->getDocComment()) { $docComment = new \phpDocumentor\Reflection\DocBlock($method->getDocComment()); foreach ($docComment->getTags() as $tag) { /* @var $tag \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag */ if ($tag->getName() == "param") { $props[] = self::getInstance()->getValueByType($tag->getType(), $withoutValues); } } } else { foreach ($method->getParameters() as $parameter) { // var_dump($parameter->getDefaultValue()); // var_dump($parameter->getDefaultValueConstantName()); $param = \ReflectionParameter::export(array($parameter->getDeclaringClass()->name, $parameter->getDeclaringFunction()->name), $parameter->name, true); preg_match('/(\\[ )(<)(.*)(>)( )([^\\]=]+)/', $param, $matches); if (count($matches) === 7) { $t = explode(" ", trim($matches[6])); if (count($t) === 2) { if (strtolower($t[0]) == "array") { return 'array()'; } if (strtolower($t[0]) == "closure") { return 'function(){}'; } $props[] = '$this->getMock(\'' . str_replace('\\', '\\\\', $t[0]) . '\')'; } else { if ($parameter->isDefaultValueAvailable()) { $props[] = $parameter->getDefaultValue(); } else { $props[] = 1; } } } } } $props2 = array(); foreach ($props as $prop2) { if ($prop2 != "") { $props2[] = $prop2; } } return implode(",", $props2); }