diff options
author | d-skora <d.skora@clearcode.cc> | 2014-10-03 18:12:35 +0400 |
---|---|---|
committer | d-skora <d.skora@clearcode.cc> | 2014-10-15 14:44:36 +0400 |
commit | b8596f0b68f56b0890c61cd0c58e9c79420aff57 (patch) | |
tree | 3257effb57b4a3ad156468358e43e9f064dfd646 /core | |
parent | 2d06c9a753b001a99c0b787e9735dc1e1de9ba87 (diff) |
hide annotation with tests
Diffstat (limited to 'core')
-rw-r--r-- | core/API/DocumentationGenerator.php | 117 | ||||
-rw-r--r-- | core/API/Proxy.php | 46 |
2 files changed, 154 insertions, 9 deletions
diff --git a/core/API/DocumentationGenerator.php b/core/API/DocumentationGenerator.php index f9cc779982..f1e49cf856 100644 --- a/core/API/DocumentationGenerator.php +++ b/core/API/DocumentationGenerator.php @@ -12,10 +12,10 @@ use Exception; use Piwik\Common; use Piwik\Piwik; use Piwik\Url; +use ReflectionClass; class DocumentationGenerator { - protected $modulesToHide = array('CoreAdminHome', 'DBStats'); protected $countPluginsLoaded = 0; /** @@ -49,21 +49,20 @@ class DocumentationGenerator } $str = $toc = ''; - $parametersToSet = array( - 'idSite' => Common::getRequestVar('idSite', 1, 'int'), - 'period' => Common::getRequestVar('period', 'day', 'string'), - 'date' => Common::getRequestVar('date', 'today', 'string') - ); foreach (Proxy::getInstance()->getMetadata() as $class => $info) { $moduleName = Proxy::getInstance()->getModuleNameFromClassName($class); + $rClass = new ReflectionClass($class); - if (in_array($moduleName, $this->modulesToHide)) { + if (!Piwik::hasUserSuperUserAccess() && $this->checkIfClassCommentContainsHideAnnotation($rClass)) { continue; } - $toc .= "<a href='#$moduleName'>$moduleName</a><br/>"; - $str .= $this->getInterfaceString($moduleName, $class, $info, $parametersToSet, $outputExampleUrls, $prefixUrls); + $toDisplay = $this->prepareModulesAndMethods($info, $moduleName); + foreach ($toDisplay as $moduleName => $methods) { + $toc .= $this->prepareModuleToDisplay($moduleName); + $str .= $this->prepareMethodToDisplay($moduleName, $info, $methods, $class, $outputExampleUrls, $prefixUrls); + } } $str = "<h2 id='topApiRef' name='topApiRef'>Quick access to APIs</h2> @@ -73,6 +72,106 @@ class DocumentationGenerator return $str; } + public function prepareModuleToDisplay($moduleName) + { + return "<a href='#$moduleName'>$moduleName</a><br/>"; + } + + public function prepareMethodToDisplay($moduleName, $info, $methods, $class, $outputExampleUrls, $prefixUrls) + { + $str = ''; + $str .= "\n<a name='$moduleName' id='$moduleName'></a><h2>Module " . $moduleName . "</h2>"; + $info['__documentation'] = $this->checkDocumentation($info['__documentation']); + $str .= "<div class='apiDescription'> " . $info['__documentation'] . " </div>"; + foreach ($methods as $methodName) { + $params = $this->getParametersString($class, $methodName); + + $str .= "\n <div class='apiMethod'>- <b>$moduleName.$methodName </b>" . $params . ""; + $str .= '<small>'; + if ($outputExampleUrls) { + $str .= $this->addExamples($class, $methodName, $prefixUrls); + } + $str .= '</small>'; + $str .= "</div>\n"; + } + + return $str; + } + + public function prepareModulesAndMethods($info, $moduleName) + { + $toDisplay = array(); + + foreach ($info as $methodName => $infoMethod) { + if ($methodName == '__documentation') { + continue; + } + $toDisplay[$moduleName][] = $methodName; + } + + return $toDisplay; + } + + public function addExamples($class, $methodName, $prefixUrls) + { + $token_auth = "&token_auth=" . Piwik::getCurrentUserTokenAuth(); + $parametersToSet = array( + 'idSite' => Common::getRequestVar('idSite', 1, 'int'), + 'period' => Common::getRequestVar('period', 'day', 'string'), + 'date' => Common::getRequestVar('date', 'today', 'string') + ); + $str = ''; +// used when we include this output in the Piwik official documentation for example + $str .= "<span class=\"example\">"; + $exampleUrl = $this->getExampleUrl($class, $methodName, $parametersToSet); + if ($exampleUrl !== false) { + $lastNUrls = ''; + if (preg_match('/(&period)|(&date)/', $exampleUrl)) { + $exampleUrlRss = $prefixUrls . $this->getExampleUrl($class, $methodName, array('date' => 'last10', 'period' => 'day') + $parametersToSet); + $lastNUrls = ", RSS of the last <a target=_blank href='$exampleUrlRss&format=rss$token_auth&translateColumnNames=1'>10 days</a>"; + } + $exampleUrl = $prefixUrls . $exampleUrl; + $str .= " [ Example in + <a target=_blank href='$exampleUrl&format=xml$token_auth'>XML</a>, + <a target=_blank href='$exampleUrl&format=JSON$token_auth'>Json</a>, + <a target=_blank href='$exampleUrl&format=Tsv$token_auth&translateColumnNames=1'>Tsv (Excel)</a> + $lastNUrls + ]"; + } else { + $str .= " [ No example available ]"; + } + $str .= "</span>"; + return $str; + } + + /** + * Check if Class contains @hide + * + * @param ReflectionClass $rClass instance of ReflectionMethod + * @return bool + */ + public function checkIfClassCommentContainsHideAnnotation(ReflectionClass $rClass) + { + if (strstr($rClass->getDocComment(), '@hide') === false) { + return false; + } + return true; + } + + /** + * Check if documentation contains @hide annotation and deletes it + * + * @param $moduleToCheck + * @return mixed + */ + public function checkDocumentation($moduleToCheck) + { + if (strpos($moduleToCheck, '@hide') == true) { + $moduleToCheck = str_replace(strtok(strstr($moduleToCheck, '@hide'), "\n"), "", $moduleToCheck); + } + return $moduleToCheck; + } + private function getInterfaceString($moduleName, $class, $info, $parametersToSet, $outputExampleUrls, $prefixUrls) { $str = ''; diff --git a/core/API/Proxy.php b/core/API/Proxy.php index daaa5c9f42..1a87352abe 100644 --- a/core/API/Proxy.php +++ b/core/API/Proxy.php @@ -362,6 +362,51 @@ class Proxy extends Singleton } /** + * Check if method contains @hide + * + * @param ReflectionMethod $method instance of ReflectionMethod + * @return bool + */ + public function checkIfMethodContainsHideAnnotation($method) + { + $docComment = $method->getDocComment(); + $hideLine = strstr($docComment, '@hide'); + if($hideLine) { + $hideString = trim(str_replace("@hide", "", strtok($hideLine, "\n"))); + if($hideString) { + $response = false; + $hideArray = explode(" ", $hideString); + $hideString = $hideArray[0]; + /** + * Triggered to check if plugin should be hidden from the API for the current user. + * + * This event exists for checking if the user should be able to see the plugin API. + * If &$response is set to false then the user will be able to see the plugin API. + * If &$response is set to true then the plugin API will be hidden for the user. + * + * **Example** + * + * public function checkIfNotSuperUser(&$response) + * { + * try { + * Piwik::checkUserHasSuperUserAccess(); + * $response = false; + * } catch (\Exception $e) { + * $response = true; + * } + * } + * + * @param bool &$response Boolean value containing information + * if the plugin API should be hidden from the current user. + */ + Piwik::postEvent(sprintf('API.DocumentationGenerator.hide%s', $hideString), array(&$response)); + return $response; + } + } + return false; + } + + /** * Returns an array containing the values of the parameters to pass to the method to call * * @param array $requiredParameters array of (parameter name, default value) @@ -433,6 +478,7 @@ class Proxy extends Singleton && $method->getName() != 'getInstance' && false === strstr($method->getDocComment(), '@deprecated') && (!$this->hideIgnoredFunctions || false === strstr($method->getDocComment(), '@ignore')) + && (Piwik::hasUserSuperUserAccess() || false === $this->checkIfMethodContainsHideAnnotation($method)) ) { $name = $method->getName(); $parameters = $method->getParameters(); |