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

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