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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /**
  64. * Print the help page
  65. */
  66. public static function index()
  67. {
  68. $businesses_ = self::getBusinesses();
  69. /**
  70. * @var $businesses LuRouteDbo[]
  71. */
  72. $businesses_tiwg = [];
  73. foreach ($businesses_ as $businesses) {
  74. $businessHyphen = self::getBusinessHyphen($businesses[0]->getBusinessClass());
  75. $businessName = self::getBusinessName($businesses[0]->getBusinessClass());
  76. $business_tiwg = [];
  77. $business_tiwg["name"] = $businessName;
  78. $business_tiwg["name_hyphen"] = $businessHyphen;
  79. $business_tiwg["routes"] = [];
  80. foreach ($businesses as $business) {
  81. $reflection = new ReflectionMethod($business->getBusinessClass(), $business->getBusinessMethod());
  82. $description = $reflection->getDocComment();
  83. if ($description === false) {
  84. $description = "No description available";
  85. }
  86. else {
  87. $docParser = new DocBlock($description);
  88. $description = $docParser->description;
  89. }
  90. $business_tiwg["routes"][] = [
  91. "method" => $business->getMethod(),
  92. "url" => $business->getUrl(),
  93. "businessMethod" => $business->getBusinessMethod(),
  94. "description" => $description
  95. ];
  96. }
  97. $businesses_tiwg[] = $business_tiwg;
  98. }
  99. $loader = new Twig_Loader_Filesystem(__DIR__ . "/../Templates");
  100. $twig = new Twig_Environment($loader, array());
  101. $template = $twig->loadTemplate("index.html.twig");
  102. $content = $template->render(array("businesses" => $businesses_tiwg));
  103. echo $content;
  104. exit;
  105. }
  106. /**
  107. * Print the help page for the selected business method.
  108. * The business must have the full namespace, hyphen separated
  109. * eg: App-Http-Business-MyBusinessClass
  110. * @param $businessHyphen string The business to print help
  111. * @param $method string The method to print help
  112. */
  113. public static function method($businessHyphen, $method)
  114. {
  115. $businesses_ = self::getBusinesses();
  116. $businessName = self::getBusinessFromHyphen($businessHyphen);
  117. /**
  118. * @var $businesses LuRouteDbo[]
  119. */
  120. $businesses = $businesses_[$businessName];
  121. $business = null;
  122. foreach ($businesses as $b) {
  123. if ($b->getBusinessMethod() == $method) {
  124. $business = $b;
  125. }
  126. }
  127. echo "<h1>" . $business->getMethod() . " " . $business->getUrl() . "</h1>";
  128. $reflection = new ReflectionMethod($businessName, $method);
  129. echo str_replace("\n", "<br />", $reflection->getDocComment());
  130. exit;
  131. }
  132. }