Browse Source

detailed page template update

tags/0.1.0
Robin Thoni 8 years ago
parent
commit
5cf26db66c
1 changed files with 147 additions and 76 deletions
  1. 147
    76
      src/Doc/Business/LuDocBusiness.php

+ 147
- 76
src/Doc/Business/LuDocBusiness.php View File

@@ -3,108 +3,179 @@
3 3
  * Created by PhpStorm.
4 4
  * User: robin
5 5
  * Date: 10/22/15
6
- * Time: 9:59 PM
6
+ * Time: 8:24 PM
7 7
  */
8 8
 
9 9
 namespace Luticate\Doc\Business;
10 10
 
11
-use Exception;
11
+use Luticate\Utils\LuRoute;
12
+use Luticate\Utils\LuRouteDbo;
13
+use ReflectionMethod;
14
+use Twig_Environment;
15
+use Twig_Loader_Filesystem;
12 16
 
13
-class DocBlock {
17
+class LuDocBusiness
18
+{
19
+    /**
20
+     * @param $prefix string
21
+     */
22
+    public static function setupRoutes($prefix = "/luticate")
23
+    {
24
+        $ns = 'Luticate\Doc\Business\\';
25
+
26
+        $route = LuRoute::getInstance();
27
+        $route->get("$prefix/doc", "${ns}LuDocBusiness", "index");
28
+        $route->get("$prefix/doc/{businessHyphen}/{method}", "${ns}LuDocBusiness", "method");
29
+    }
14 30
 
15
-    public $docblock,
16
-        $description = null,
17
-        $all_params = array();
31
+    private static function getBusinesses()
32
+    {
33
+        $route = LuRoute::getInstance();
34
+
35
+        $businesses = [];
36
+        foreach ($route->getRoutes() as $r) {
37
+
38
+            if (!isset($businesses[$r->getBusinessClass()])) {
39
+                $businesses[$r->getBusinessClass()] = [];
40
+            }
41
+            $businesses[$r->getBusinessClass()][] = $r;
42
+        }
43
+        return $businesses;
44
+    }
18 45
 
19 46
     /**
20
-     * Parses a docblock;
47
+     * @param $business string
48
+     * @return string
21 49
      */
22
-    function __construct($docblock) {
23
-        if( !is_string($docblock) ) {
24
-            throw new Exception("DocBlock expects first parameter to be a string");
25
-        }
50
+    private static function getBusinessName($business)
51
+    {
52
+        $tab = explode("\\", $business);
53
+        return array_pop($tab);
54
+    }
26 55
 
27
-        $this->docblock = $docblock;
28
-        $this->parse_block();
56
+    /**
57
+     * @param $business string
58
+     * @return string
59
+     */
60
+    private static function getBusinessHyphen($business)
61
+    {
62
+        return str_replace("\\", "-", $business);
29 63
     }
30 64
 
31 65
     /**
32
-     * An alias to __call();
33
-     * allows a better DSL
34
-     *
35
-     * @param string $param_name
36
-     * @return mixed
66
+     * @param $business string
67
+     * @return string
37 68
      */
38
-    public function __get($param_name) {
39
-        return $this->$param_name();
69
+    private static function getBusinessFromHyphen($business)
70
+    {
71
+        return str_replace("-", "\\", $business);
72
+    }
73
+
74
+    private static function printTwig($file, $vars)
75
+    {
76
+        $loader = new Twig_Loader_Filesystem(__DIR__ . "/../Templates");
77
+        $twig = new Twig_Environment($loader, array());
78
+        $template = $twig->loadTemplate($file . ".twig");
79
+        $content = $template->render($vars);
80
+        echo $content;
81
+        exit;
40 82
     }
41 83
 
42 84
     /**
43
-     * Checks if the param exists
44
-     *
45
-     * @param string $param_name
46
-     * @return mixed
85
+     * Print the help page
47 86
      */
48
-    public function __call($param_name, $values = null) {
49
-        if( $param_name == "description" ) {
50
-            return $this->description;
51
-        }else if( isset($this->all_params[$param_name]) ) {
52
-            $params = $this->all_params[$param_name];
53
-
54
-            if( count($params) == 1 ) {
55
-                return $params[0];
56
-            }else {
57
-                return $params;
87
+    public static function index()
88
+    {
89
+        $businesses_ = self::getBusinesses();
90
+        /**
91
+         * @var $businesses LuRouteDbo[]
92
+         */
93
+        $businesses_tiwg = [];
94
+        foreach ($businesses_ as $businesses) {
95
+            $businessHyphen = self::getBusinessHyphen($businesses[0]->getBusinessClass());
96
+            $businessName = self::getBusinessName($businesses[0]->getBusinessClass());
97
+            $business_tiwg = [];
98
+            $business_tiwg["name"] = $businessName;
99
+            $business_tiwg["name_hyphen"] = $businessHyphen;
100
+            $business_tiwg["routes"] = [];
101
+
102
+            foreach ($businesses as $business) {
103
+                $reflection = new ReflectionMethod($business->getBusinessClass(), $business->getBusinessMethod());
104
+                $description = $reflection->getDocComment();
105
+                if ($description === false) {
106
+                    $description = "No description available";
107
+                }
108
+                else {
109
+                    $docParser = new DocBlock($description);
110
+                    $description = $docParser->description;
111
+                }
112
+                $business_tiwg["routes"][] = [
113
+                    "method" => $business->getMethod(),
114
+                    "url" => $business->getUrl(),
115
+                    "businessMethod" => $business->getBusinessMethod(),
116
+                    "description" => $description
117
+                ];
58 118
             }
119
+            $businesses_tiwg[] = $business_tiwg;
59 120
         }
60 121
 
61
-        return null;
122
+        self::printTwig("index.html", array("businesses" => $businesses_tiwg));
62 123
     }
63 124
 
64 125
     /**
65
-     * Parse each line in the docblock
66
-     * and store the params in `$this->all_params`
67
-     * and the rest in `$this->description`
126
+     * Print the help page for the selected business method.
127
+     * The business must have the full namespace, hyphen separated
128
+     * eg: App-Http-Business-MyBusinessClass
129
+     * @param $businessHyphen string The business to print help
130
+     * @param $method string The method to print help
68 131
      */
69
-    private function parse_block() {
70
-        // split at each line
71
-        foreach(preg_split("/(\r?\n)/", $this->docblock) as $line){
72
-
73
-            // if starts with an asterisk
74
-            if( preg_match('/^(?=\s+?\*[^\/])(.+)/', $line, $matches) ) {
75
-
76
-                $info = $matches[1];
77
-
78
-                // remove wrapping whitespace
79
-                $info = trim($info);
80
-
81
-                // remove leading asterisk
82
-                $info = preg_replace('/^(\*\s+?)/', '', $info);
83
-
84
-                // if it doesn't start with an "@" symbol
85
-                // then add to the description
86
-                if( $info[0] !== "@" ) {
87
-                    $this->description .= "\n$info";
88
-                    continue;
89
-                }else {
90
-                    // get the name of the param
91
-                    if (preg_match('/@param +\\$([^ ]+) +([^ ]+) +(.*)$/', $info, $matches) == 1) {
92
-                        $param_name = $matches[1];
93
-                        $param_type = $matches[2];
94
-                        $value = $matches[3];
95
-
96
-
97
-                        // push the param value into place
98
-                        $this->all_params[$param_name] = [
99
-                            "name" => $param_name,
100
-                            "type" => $param_type,
101
-                            "description" => $value
102
-                        ];
103
-
104
-                        continue;
105
-                    }
106
-                }
132
+    public static function method($businessHyphen, $method)
133
+    {
134
+        $businesses_ = self::getBusinesses();
135
+        $businessName = self::getBusinessFromHyphen($businessHyphen);
136
+        /**
137
+         * @var $businesses LuRouteDbo[]
138
+         */
139
+        $businesses = $businesses_[$businessName];
140
+        $business = null;
141
+        foreach ($businesses as $b) {
142
+            if ($b->getBusinessMethod() == $method) {
143
+                $business = $b;
144
+            }
145
+        }
146
+
147
+        $reflection = new ReflectionMethod($business->getBusinessClass(), $business->getBusinessMethod());
148
+        $description = $reflection->getDocComment();
149
+        $docParser = null;
150
+        if ($description === false) {
151
+            $description = "No description available";
152
+        }
153
+        else {
154
+            $docParser = new DocBlock($description);
155
+            $description = $docParser->description;
156
+        }
157
+
158
+        $parameters = [];
159
+        foreach ($reflection->getParameters() as $param) {
160
+            $p = [];
161
+
162
+            if (!is_null($docParser) && isset($docParser->all_params[$param->getName()])) {
163
+                $p = $docParser->all_params[$param->getName()];
164
+                $p["annotations"] = "";
107 165
             }
166
+            else {
167
+                $p["description"] = "No description available";
168
+                $p["type"] = "Unknown";
169
+                $p["annotations"] = "";
170
+            }
171
+
172
+            $parameters[] = $p;
108 173
         }
174
+
175
+        self::printTwig("method.html", array(
176
+            "description" => str_replace("\n", "<br />", $description),
177
+            "url" => $business->getUrl(),
178
+            "method" => $business->getMethod(),
179
+            "parameters" => $parameters));
109 180
     }
110 181
 }

Loading…
Cancel
Save