123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?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);
- }
-
- private static function printTwig($file, $vars)
- {
- $loader = new Twig_Loader_Filesystem(__DIR__ . "/../Templates");
- $twig = new Twig_Environment($loader, array());
- $template = $twig->loadTemplate($file . ".twig");
- $content = $template->render($vars);
- echo $content;
- exit;
- }
-
- /**
- * 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";
- }
- else {
- $docParser = new DocBlock($description);
- $description = $docParser->description;
- }
- $business_tiwg["routes"][] = [
- "method" => $business->getMethod(),
- "url" => $business->getUrl(),
- "businessMethod" => $business->getBusinessMethod(),
- "description" => $description
- ];
- }
- $businesses_tiwg[] = $business_tiwg;
- }
-
- self::printTwig("index.html", array("businesses" => $businesses_tiwg));
- }
-
- /**
- * 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;
- }
- }
-
- $reflection = new ReflectionMethod($business->getBusinessClass(), $business->getBusinessMethod());
- $description = $reflection->getDocComment();
- $docParser = null;
- if ($description === false) {
- $description = "No description available";
- }
- else {
- $docParser = new DocBlock($description);
- $description = $docParser->description;
- }
-
- $parameters = [];
- foreach ($reflection->getParameters() as $param) {
- $p = [];
-
- $p["name"] = $param->getName();
- if (!is_null($docParser) && isset($docParser->all_params[$param->getName()])) {
- $p["description"] = $docParser->all_params[$param->getName()];
- $p["type"] = "Unknown";
- $p["annotations"] = "";
- }
- else {
- $p["description"] = $description;
- $p["type"] = "Unknown";
- $p["annotations"] = "";
- }
-
- $parameters[] = $p;
- }
-
- self::printTwig("method.html", array(
- "description" => str_replace("\n", "<br />", $description),
- "url" => $business->getUrl(),
- "method" => $business->getMethod(),
- "parameters" => $parameters));
- }
- }
|