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

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