Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LuDocBusiness.php 4.2KB

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