123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 10/22/15
- * Time: 8:24 PM
- */
-
- namespace Luticate\Doc\Business;
-
- use Luticate\Utils\LuRoute;
- use Luticate\Utils\LuRouteDbo;
- use ReflectionMethod;
- use Twig_Environment;
- use Twig_Loader_Filesystem;
-
- class LuDocBusiness
- {
- /**
- * @param $prefix string
- */
- public static function setupRoutes($prefix = "/luticate")
- {
- $ns = 'Luticate\Doc\Business\\';
-
- $route = LuRoute::getInstance();
- $route->get("$prefix/doc", "${ns}LuDocBusiness", "index");
- $route->get("$prefix/doc/{businessHyphen}/{method}", "${ns}LuDocBusiness", "method");
- }
-
- private static function getBusinesses()
- {
- $route = LuRoute::getInstance();
-
- $businesses = [];
- foreach ($route->getRoutes() as $r) {
-
- if (!isset($businesses[$r->getBusinessClass()])) {
- $businesses[$r->getBusinessClass()] = [];
- }
- $businesses[$r->getBusinessClass()][] = $r;
- }
- return $businesses;
- }
-
- /**
- * @param $business string
- * @return string
- */
- private static function getBusinessName($business)
- {
- $tab = explode("\\", $business);
- return array_pop($tab);
- }
-
- /**
- * @param $business string
- * @return string
- */
- private static function getBusinessHyphen($business)
- {
- return str_replace("\\", "-", $business);
- }
-
- /**
- * @param $business string
- * @return string
- */
- private static function getBusinessFromHyphen($business)
- {
- return str_replace("-", "\\", $business);
- }
-
- /**
- * Print the help page
- */
- public static function index()
- {
- $businesses_ = self::getBusinesses();
- /**
- * @var $businesses LuRouteDbo[]
- */
- $businesses_tiwg = [];
- foreach ($businesses_ as $businesses) {
- $businessHyphen = self::getBusinessHyphen($businesses[0]->getBusinessClass());
- $businessName = self::getBusinessName($businesses[0]->getBusinessClass());
- $business_tiwg = [];
- $business_tiwg["name"] = $businessName;
- $business_tiwg["name_hyphen"] = $businessHyphen;
- $business_tiwg["routes"] = [];
-
- foreach ($businesses as $business) {
- $reflection = new ReflectionMethod($business->getBusinessClass(), $business->getBusinessMethod());
- $description = $reflection->getDocComment();
- if ($description === false) {
- $description = "No description available";
- }
- $business_tiwg["routes"][] = [
- "method" => $business->getMethod(),
- "url" => $business->getUrl(),
- "businessMethod" => $business->getBusinessMethod(),
- "description" => $description
- ];
- }
- $businesses_tiwg[] = $business_tiwg;
- }
-
- $loader = new Twig_Loader_Filesystem(__DIR__ . "/../Templates");
- $twig = new Twig_Environment($loader, array());
- $template = $twig->loadTemplate("index.html.twig");
- $content = $template->render(array("businesses" => $businesses_tiwg));
- echo $content;
- exit;
- }
-
- /**
- * Print the help page for the selected business method.
- * The business must have the full namespace, hyphen separated
- * eg: App-Http-Business-MyBusinessClass
- * @param $businessHyphen string The business to print help
- * @param $method string The method to print help
- */
- public static function method($businessHyphen, $method)
- {
- $businesses_ = self::getBusinesses();
- $businessName = self::getBusinessFromHyphen($businessHyphen);
- /**
- * @var $businesses LuRouteDbo[]
- */
- $businesses = $businesses_[$businessName];
- $business = null;
- foreach ($businesses as $b) {
- if ($b->getBusinessMethod() == $method) {
- $business = $b;
- }
- }
- echo "<h1>" . $business->getMethod() . " " . $business->getUrl() . "</h1>";
-
- $reflection = new ReflectionMethod($businessName, $method);
- echo str_replace("\n", "<br />", $reflection->getDocComment());
- exit;
- }
- }
|