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

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