Browse Source

init

tags/0.1.0
Robin Thoni 8 years ago
commit
963556b25c
4 changed files with 270 additions and 0 deletions
  1. 4
    0
      .gitignore
  2. 23
    0
      composer.json
  3. 110
    0
      src/Doc/Business/DocBlock.php
  4. 133
    0
      src/Doc/Business/LuDocBusiness.php

+ 4
- 0
.gitignore View File

@@ -0,0 +1,4 @@
1
+/.idea
2
+/vendor
3
+/composer.lock
4
+/.env

+ 23
- 0
composer.json View File

@@ -0,0 +1,23 @@
1
+{
2
+    "name": "luticate/doc",
3
+    "description": "Luticate documentation generator",
4
+    "authors": [
5
+        {
6
+            "name": "Robin THONI",
7
+            "email": "robin@rthoni.com"
8
+        }
9
+    ],
10
+    "repositories": [{
11
+      "type": "vcs",
12
+      "url":  "https://git.rthoni.com/luticate/utils.git"
13
+    }],
14
+    "require": {
15
+      "luticate/utils": "*"
16
+    },
17
+    "autoload": {
18
+      "psr-4": {
19
+        "Luticate\\": "src/"
20
+      }
21
+    },
22
+    "minimum-stability": "dev"
23
+}

+ 110
- 0
src/Doc/Business/DocBlock.php View File

@@ -0,0 +1,110 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 10/22/15
6
+ * Time: 9:59 PM
7
+ */
8
+
9
+namespace Luticate\Doc\Business;
10
+
11
+use Exception;
12
+
13
+class DocBlock {
14
+
15
+    public $docblock,
16
+        $description = null,
17
+        $all_params = array();
18
+
19
+    /**
20
+     * Parses a docblock;
21
+     */
22
+    function __construct($docblock) {
23
+        if( !is_string($docblock) ) {
24
+            throw new Exception("DocBlock expects first parameter to be a string");
25
+        }
26
+
27
+        $this->docblock = $docblock;
28
+        $this->parse_block();
29
+    }
30
+
31
+    /**
32
+     * An alias to __call();
33
+     * allows a better DSL
34
+     *
35
+     * @param string $param_name
36
+     * @return mixed
37
+     */
38
+    public function __get($param_name) {
39
+        return $this->$param_name();
40
+    }
41
+
42
+    /**
43
+     * Checks if the param exists
44
+     *
45
+     * @param string $param_name
46
+     * @return mixed
47
+     */
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;
58
+            }
59
+        }
60
+
61
+        return null;
62
+    }
63
+
64
+    /**
65
+     * Parse each line in the docblock
66
+     * and store the params in `$this->all_params`
67
+     * and the rest in `$this->description`
68
+     */
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
+                    preg_match('/@(\w+)/', $info, $matches);
92
+                    $param_name = $matches[1];
93
+
94
+                    // remove the param from the string
95
+                    $value = str_replace("@$param_name ", '', $info);
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;
104
+
105
+                    continue;
106
+                }
107
+            }
108
+        }
109
+    }
110
+}

+ 133
- 0
src/Doc/Business/LuDocBusiness.php View File

@@ -0,0 +1,133 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 10/22/15
6
+ * Time: 8:24 PM
7
+ */
8
+
9
+namespace Luticate\Doc\Business;
10
+
11
+use Luticate\Utils\LuRoute;
12
+use Luticate\Utils\LuRouteDbo;
13
+use ReflectionMethod;
14
+
15
+class LuDocBusiness
16
+{
17
+    private static function getBusinesses()
18
+    {
19
+        $route = LuRoute::getInstance();
20
+
21
+        $businesses = [];
22
+        foreach ($route->getRoutes() as $r) {
23
+
24
+            if (!isset($businesses[$r->getBusinessClass()])) {
25
+                $businesses[$r->getBusinessClass()] = [];
26
+            }
27
+            $businesses[$r->getBusinessClass()][] = $r;
28
+        }
29
+        return $businesses;
30
+    }
31
+
32
+    /**
33
+     * @param $business string
34
+     * @return string
35
+     */
36
+    private static function getBusinessName($business)
37
+    {
38
+        $tab = explode("\\", $business);
39
+        return array_pop($tab);
40
+    }
41
+
42
+    /**
43
+     * @param $business string
44
+     * @return string
45
+     */
46
+    private static function getBusinessHyphen($business)
47
+    {
48
+        return str_replace("\\", "-", $business);
49
+    }
50
+
51
+    /**
52
+     * @param $business string
53
+     * @return string
54
+     */
55
+    private static function getBusinessFromHyphen($business)
56
+    {
57
+        return str_replace("-", "\\", $business);
58
+    }
59
+
60
+    /**
61
+     * Print the help page
62
+     */
63
+    public static function index()
64
+    {
65
+        $businesses_ = self::getBusinesses();
66
+        /**
67
+         * @var $businesses LuRouteDbo[]
68
+         */
69
+        foreach ($businesses_ as $businesses) {
70
+            $businessHyphen = self::getBusinessHyphen($businesses[0]->getBusinessClass());
71
+            $businessName = self::getBusinessName($businesses[0]->getBusinessClass());
72
+            echo '<a href="doc/' . $businessHyphen . '"><h1>' . $businessName . "</h1></a>";
73
+
74
+            foreach ($businesses as $business) {
75
+                echo '<a href="doc/' . $businessHyphen . '/' . $business->getBusinessMethod()
76
+                    . '">' . $business->getMethod() . " " . $business->getUrl() . "</a><br />";
77
+            }
78
+        }
79
+        exit;
80
+    }
81
+
82
+
83
+    /**
84
+     * Print the help page for the selected business.
85
+     * The business must have the full namespace, hyphen separated
86
+     * eg: App-Http-Business-MyBusinessClass
87
+     * @param $businessHyphen string The business to print help
88
+     */
89
+    public static function business($businessHyphen)
90
+    {
91
+        $businesses_ = self::getBusinesses();
92
+        /**
93
+         * @var $businesses LuRouteDbo[]
94
+         */
95
+        $businesses = $businesses_[self::getBusinessFromHyphen($businessHyphen)];
96
+        $businessName = self::getBusinessName($businesses[0]->getBusinessClass());
97
+        echo '<a href="' . $businessHyphen . '"><h1>' . $businessName . "</h1></a>";
98
+
99
+        foreach ($businesses as $business) {
100
+            echo '<a href="' . $businessHyphen . '/' . $business->getBusinessMethod()
101
+                . '">' . $business->getMethod() . " " . $business->getUrl() . "</a><br />";
102
+        }
103
+        exit;
104
+    }
105
+
106
+    /**
107
+     * Print the help page for the selected business method.
108
+     * The business must have the full namespace, hyphen separated
109
+     * eg: App-Http-Business-MyBusinessClass
110
+     * @param $businessHyphen string The business to print help
111
+     * @param $method string The method to print help
112
+     */
113
+    public static function method($businessHyphen, $method)
114
+    {
115
+        $businesses_ = self::getBusinesses();
116
+        $businessName = self::getBusinessFromHyphen($businessHyphen);
117
+        /**
118
+         * @var $businesses LuRouteDbo[]
119
+         */
120
+        $businesses = $businesses_[$businessName];
121
+        $business = null;
122
+        foreach ($businesses as $b) {
123
+            if ($b->getBusinessMethod() == $method) {
124
+                $business = $b;
125
+            }
126
+        }
127
+        echo "<h1>" . $business->getMethod() . " " . $business->getUrl() . "</h1>";
128
+
129
+        $reflection = new ReflectionMethod($businessName, $method);
130
+        echo str_replace("\n", "<br />", $reflection->getDocComment());
131
+        exit;
132
+    }
133
+}

Loading…
Cancel
Save