浏览代码

detailed page template update

tags/0.1.0
Robin Thoni 9 年前
父节点
当前提交
7a75855787
共有 3 个文件被更改,包括 90 次插入163 次删除
  1. 12
    12
      src/Doc/Business/DocBlock.php
  2. 76
    149
      src/Doc/Business/LuDocBusiness.php
  3. 2
    2
      src/Doc/Templates/method.html.twig

+ 12
- 12
src/Doc/Business/DocBlock.php 查看文件

@@ -88,21 +88,21 @@ class DocBlock {
88 88
                     continue;
89 89
                 }else {
90 90
                     // get the name of the param
91
-                    preg_match('/@(\w+)/', $info, $matches);
92
-                    $param_name = $matches[1];
91
+                    if (preg_match('/@param +\\$([^ ]+) +([^ ]+) +(.*)$/', $info, $matches) == 1) {
92
+                        $param_name = $matches[1];
93
+                        $param_type = $matches[2];
94
+                        $value = $matches[3];
93 95
 
94
-                    // remove the param from the string
95
-                    $value = str_replace("@$param_name ", '', $info);
96 96
 
97
-                    // if the param hasn't been added yet, create a key for it
98
-                    if( !isset($this->all_params[$param_name]) ) {
99
-                        $this->all_params[$param_name] = array();
100
-                    }
101
-
102
-                    // push the param value into place
103
-                    $this->all_params[$param_name][] = $value;
97
+                        // push the param value into place
98
+                        $this->all_params[$param_name] = [
99
+                            "name" => $param_name,
100
+                            "param_type" => $param_type,
101
+                            "description" => $value
102
+                        ];
104 103
 
105
-                    continue;
104
+                        continue;
105
+                    }
106 106
                 }
107 107
             }
108 108
         }

+ 76
- 149
src/Doc/Business/LuDocBusiness.php 查看文件

@@ -3,181 +3,108 @@
3 3
  * Created by PhpStorm.
4 4
  * User: robin
5 5
  * Date: 10/22/15
6
- * Time: 8:24 PM
6
+ * Time: 9:59 PM
7 7
  */
8 8
 
9 9
 namespace Luticate\Doc\Business;
10 10
 
11
-use Luticate\Utils\LuRoute;
12
-use Luticate\Utils\LuRouteDbo;
13
-use ReflectionMethod;
14
-use Twig_Environment;
15
-use Twig_Loader_Filesystem;
11
+use Exception;
16 12
 
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
-    }
13
+class DocBlock {
30 14
 
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
-    }
15
+    public $docblock,
16
+        $description = null,
17
+        $all_params = array();
45 18
 
46 19
     /**
47
-     * @param $business string
48
-     * @return string
20
+     * Parses a docblock;
49 21
      */
50
-    private static function getBusinessName($business)
51
-    {
52
-        $tab = explode("\\", $business);
53
-        return array_pop($tab);
54
-    }
22
+    function __construct($docblock) {
23
+        if( !is_string($docblock) ) {
24
+            throw new Exception("DocBlock expects first parameter to be a string");
25
+        }
55 26
 
56
-    /**
57
-     * @param $business string
58
-     * @return string
59
-     */
60
-    private static function getBusinessHyphen($business)
61
-    {
62
-        return str_replace("\\", "-", $business);
27
+        $this->docblock = $docblock;
28
+        $this->parse_block();
63 29
     }
64 30
 
65 31
     /**
66
-     * @param $business string
67
-     * @return string
32
+     * An alias to __call();
33
+     * allows a better DSL
34
+     *
35
+     * @param string $param_name
36
+     * @return mixed
68 37
      */
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;
38
+    public function __get($param_name) {
39
+        return $this->$param_name();
82 40
     }
83 41
 
84 42
     /**
85
-     * Print the help page
43
+     * Checks if the param exists
44
+     *
45
+     * @param string $param_name
46
+     * @return mixed
86 47
      */
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
-                ];
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;
118 58
             }
119
-            $businesses_tiwg[] = $business_tiwg;
120 59
         }
121 60
 
122
-        self::printTwig("index.html", array("businesses" => $businesses_tiwg));
61
+        return null;
123 62
     }
124 63
 
125 64
     /**
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
65
+     * Parse each line in the docblock
66
+     * and store the params in `$this->all_params`
67
+     * and the rest in `$this->description`
131 68
      */
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
-            $p["name"] = $param->getName();
163
-            if (!is_null($docParser) && isset($docParser->all_params[$param->getName()])) {
164
-                $p["description"] = $docParser->all_params[$param->getName()];
165
-                $p["type"] = "Unknown";
166
-                $p["annotations"] = "";
167
-            }
168
-            else {
169
-                $p["description"] = $description;
170
-                $p["type"] = "Unknown";
171
-                $p["annotations"] = "";
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
+                }
172 107
             }
173
-
174
-            $parameters[] = $p;
175 108
         }
176
-
177
-        self::printTwig("method.html", array(
178
-            "description" => str_replace("\n", "<br />", $description),
179
-            "url" => $business->getUrl(),
180
-            "method" => $business->getMethod(),
181
-            "parameters" => $parameters));
182 109
     }
183 110
 }

+ 2
- 2
src/Doc/Templates/method.html.twig 查看文件

@@ -155,7 +155,7 @@
155 155
     <section class="content-wrapper main-content clear-fix">
156 156
 
157 157
         <h1>{{ method }} {{ url }}</h1>
158
-        <p>{{ description }}</p>
158
+        <p>{{ description | raw }}</p>
159 159
         <h3>Parameters</h3>
160 160
 
161 161
         <table class="help-page-table">
@@ -167,7 +167,7 @@
167 167
             <tr>
168 168
                 <td class="parameter-name">{{ param.name }}</td>
169 169
                 <td class="parameter-documentation">
170
-                    <p>{{ param.description }}</p>
170
+                    <p>{{ param.description | raw }}</p>
171 171
                 </td>
172 172
                 <td class="parameter-type">
173 173
                     {{ param.type }}

正在加载...
取消
保存