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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. class LuDocBusiness
  13. {
  14. /**
  15. * @param $prefix string
  16. */
  17. public static function setupRoutes($prefix = "/luticate")
  18. {
  19. $ns = 'Luticate\Doc\Business\\';
  20. $route = LuRoute::getInstance();
  21. $route->get("$prefix/doc", "${ns}LuDocBusiness", "index");
  22. $route->get("$prefix/doc/{businessHyphen}", "${ns}LuDocBusiness", "business");
  23. $route->get("$prefix/doc/{businessHyphen}/{method}", "${ns}LuDocBusiness", "method");
  24. }
  25. private static function getBusinesses()
  26. {
  27. $route = LuRoute::getInstance();
  28. $businesses = [];
  29. foreach ($route->getRoutes() as $r) {
  30. if (!isset($businesses[$r->getBusinessClass()])) {
  31. $businesses[$r->getBusinessClass()] = [];
  32. }
  33. $businesses[$r->getBusinessClass()][] = $r;
  34. }
  35. return $businesses;
  36. }
  37. /**
  38. * @param $business string
  39. * @return string
  40. */
  41. private static function getBusinessName($business)
  42. {
  43. $tab = explode("\\", $business);
  44. return array_pop($tab);
  45. }
  46. /**
  47. * @param $business string
  48. * @return string
  49. */
  50. private static function getBusinessHyphen($business)
  51. {
  52. return str_replace("\\", "-", $business);
  53. }
  54. /**
  55. * @param $business string
  56. * @return string
  57. */
  58. private static function getBusinessFromHyphen($business)
  59. {
  60. return str_replace("-", "\\", $business);
  61. }
  62. /**
  63. * Print the help page
  64. */
  65. public static function index()
  66. {
  67. $businesses_ = self::getBusinesses();
  68. /**
  69. * @var $businesses LuRouteDbo[]
  70. */
  71. foreach ($businesses_ as $businesses) {
  72. $businessHyphen = self::getBusinessHyphen($businesses[0]->getBusinessClass());
  73. $businessName = self::getBusinessName($businesses[0]->getBusinessClass());
  74. echo '<a href="doc/' . $businessHyphen . '"><h1>' . $businessName . "</h1></a>";
  75. foreach ($businesses as $business) {
  76. echo '<a href="doc/' . $businessHyphen . '/' . $business->getBusinessMethod()
  77. . '">' . $business->getMethod() . " " . $business->getUrl() . "</a><br />";
  78. }
  79. }
  80. exit;
  81. }
  82. /**
  83. * Print the help page for the selected business.
  84. * The business must have the full namespace, hyphen separated
  85. * eg: App-Http-Business-MyBusinessClass
  86. * @param $businessHyphen string The business to print help
  87. */
  88. public static function business($businessHyphen)
  89. {
  90. $businesses_ = self::getBusinesses();
  91. /**
  92. * @var $businesses LuRouteDbo[]
  93. */
  94. $businesses = $businesses_[self::getBusinessFromHyphen($businessHyphen)];
  95. $businessName = self::getBusinessName($businesses[0]->getBusinessClass());
  96. echo '<a href="' . $businessHyphen . '"><h1>' . $businessName . "</h1></a>";
  97. foreach ($businesses as $business) {
  98. echo '<a href="' . $businessHyphen . '/' . $business->getBusinessMethod()
  99. . '">' . $business->getMethod() . " " . $business->getUrl() . "</a><br />";
  100. }
  101. exit;
  102. }
  103. /**
  104. * Print the help page for the selected business method.
  105. * The business must have the full namespace, hyphen separated
  106. * eg: App-Http-Business-MyBusinessClass
  107. * @param $businessHyphen string The business to print help
  108. * @param $method string The method to print help
  109. */
  110. public static function method($businessHyphen, $method)
  111. {
  112. $businesses_ = self::getBusinesses();
  113. $businessName = self::getBusinessFromHyphen($businessHyphen);
  114. /**
  115. * @var $businesses LuRouteDbo[]
  116. */
  117. $businesses = $businesses_[$businessName];
  118. $business = null;
  119. foreach ($businesses as $b) {
  120. if ($b->getBusinessMethod() == $method) {
  121. $business = $b;
  122. }
  123. }
  124. echo "<h1>" . $business->getMethod() . " " . $business->getUrl() . "</h1>";
  125. $reflection = new ReflectionMethod($businessName, $method);
  126. echo str_replace("\n", "<br />", $reflection->getDocComment());
  127. exit;
  128. }
  129. }