You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuDocBusiness.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 10/22/15
  6. * Time: 8:24 PM
  7. */
  8. namespace Luticate\Doc\Business;
  9. use Luticate\Utils\LuRoute;
  10. use Luticate\Utils\LuRouteDbo;
  11. use ReflectionMethod;
  12. use Twig_Environment;
  13. use Twig_Loader_Filesystem;
  14. class LuDocBusiness
  15. {
  16. /**
  17. * @param $prefix string
  18. */
  19. public static function setupRoutes($prefix = "/luticate")
  20. {
  21. $ns = 'Luticate\Doc\Business\\';
  22. $route = LuRoute::getInstance();
  23. $route->get("$prefix/doc", "${ns}LuDocBusiness", "index");
  24. $route->get("$prefix/doc/{businessHyphen}/{method}", "${ns}LuDocBusiness", "method");
  25. }
  26. private static function getBusinesses()
  27. {
  28. $route = LuRoute::getInstance();
  29. $businesses = [];
  30. foreach ($route->getRoutes() as $r) {
  31. if (!isset($businesses[$r->getBusinessClass()])) {
  32. $businesses[$r->getBusinessClass()] = [];
  33. }
  34. $businesses[$r->getBusinessClass()][] = $r;
  35. }
  36. return $businesses;
  37. }
  38. /**
  39. * @param $business string
  40. * @return string
  41. */
  42. private static function getBusinessName($business)
  43. {
  44. $tab = explode("\\", $business);
  45. return array_pop($tab);
  46. }
  47. /**
  48. * @param $business string
  49. * @return string
  50. */
  51. private static function getBusinessHyphen($business)
  52. {
  53. return str_replace("\\", "-", $business);
  54. }
  55. /**
  56. * @param $business string
  57. * @return string
  58. */
  59. private static function getBusinessFromHyphen($business)
  60. {
  61. return str_replace("-", "\\", $business);
  62. }
  63. private static function printTwig($file, $vars)
  64. {
  65. $loader = new Twig_Loader_Filesystem(__DIR__ . "/../Templates");
  66. $twig = new Twig_Environment($loader, array());
  67. $template = $twig->loadTemplate($file . ".twig");
  68. $content = $template->render($vars);
  69. echo $content;
  70. exit;
  71. }
  72. /**
  73. * Print the help page
  74. */
  75. public static function index()
  76. {
  77. $businesses_ = self::getBusinesses();
  78. /**
  79. * @var $businesses LuRouteDbo[]
  80. */
  81. $businesses_tiwg = [];
  82. foreach ($businesses_ as $businesses) {
  83. $businessHyphen = self::getBusinessHyphen($businesses[0]->getBusinessClass());
  84. $businessName = self::getBusinessName($businesses[0]->getBusinessClass());
  85. $business_tiwg = [];
  86. $business_tiwg["name"] = $businessName;
  87. $business_tiwg["name_hyphen"] = $businessHyphen;
  88. $business_tiwg["routes"] = [];
  89. foreach ($businesses as $business) {
  90. $reflection = new ReflectionMethod($business->getBusinessClass(), $business->getBusinessMethod());
  91. $description = $reflection->getDocComment();
  92. if ($description === false) {
  93. $description = "No description available";
  94. }
  95. else {
  96. $docParser = new DocBlock($description);
  97. $description = $docParser->description;
  98. }
  99. $business_tiwg["routes"][] = [
  100. "method" => $business->getMethod(),
  101. "url" => $business->getUrl(),
  102. "businessMethod" => $business->getBusinessMethod(),
  103. "description" => $description
  104. ];
  105. }
  106. $businesses_tiwg[] = $business_tiwg;
  107. }
  108. self::printTwig("index.html", array("businesses" => $businesses_tiwg));
  109. }
  110. /**
  111. * Print the help page for the selected business method.
  112. * The business must have the full namespace, hyphen separated
  113. * eg: App-Http-Business-MyBusinessClass
  114. * @param $businessHyphen string The business to print help
  115. * @param $method string The method to print help
  116. */
  117. public static function method($businessHyphen, $method)
  118. {
  119. $businesses_ = self::getBusinesses();
  120. $businessName = self::getBusinessFromHyphen($businessHyphen);
  121. /**
  122. * @var $businesses LuRouteDbo[]
  123. */
  124. $businesses = $businesses_[$businessName];
  125. $business = null;
  126. foreach ($businesses as $b) {
  127. if ($b->getBusinessMethod() == $method) {
  128. $business = $b;
  129. }
  130. }
  131. $reflection = new ReflectionMethod($business->getBusinessClass(), $business->getBusinessMethod());
  132. $description = $reflection->getDocComment();
  133. $docParser = null;
  134. if ($description === false) {
  135. $description = "No description available";
  136. }
  137. else {
  138. $docParser = new DocBlock($description);
  139. if (trim($docParser->description) != "") {
  140. $description = $docParser->description;
  141. }
  142. else {
  143. $description = "No description available";
  144. }
  145. }
  146. $parameters = [];
  147. foreach ($reflection->getParameters() as $param) {
  148. $p = [];
  149. if (!is_null($docParser) && isset($docParser->all_params[$param->getName()])) {
  150. $p = $docParser->all_params[$param->getName()];
  151. }
  152. else {
  153. $p["description"] = "No description available";
  154. $p["type"] = "Unknown";
  155. }
  156. $p["name"] = $param->getName();
  157. $p["annotations"] = "";
  158. if ($param->isOptional()) {
  159. $p["annotations"] = "Optional. Default value: " . $param->getDefaultValue();
  160. }
  161. if (strpos($p["name"], "_") !== 0) {
  162. $parameters[] = $p;
  163. }
  164. }
  165. self::printTwig("method.html", array(
  166. "description" => str_replace("\n", "<br />", $description),
  167. "url" => $business->getUrl(),
  168. "method" => $business->getMethod(),
  169. "parameters" => $parameters));
  170. }
  171. }