]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/CreateDoxygen.php
Add Console classes
[friendica.git] / src / Core / Console / CreateDoxygen.php
1 <?php\r
2 \r
3 namespace Friendica\Core\Console;\r
4 \r
5 /**\r
6  * Description of CreateDoxygen\r
7  *\r
8  * @author Hypolite Petovan <mrpetovan@gmail.com>\r
9  */\r
10 class CreateDoxygen extends \Asika\SimpleConsole\Console\r
11 {\r
12         protected $helpOptions = ['h', 'help', '?'];\r
13 \r
14         protected function getHelp()\r
15         {\r
16                 $help = <<<HELP\r
17 console createdoxygen - Generate Doxygen headers\r
18 Usage\r
19         bin/console createdoxygen <file> [-h|--help|-?] [-v]\r
20 \r
21 Description\r
22         Outputs the provided file with added Doxygen headers to functions\r
23 \r
24 Options\r
25     -h|--help|-? Show help information\r
26     -v           Show more debug information.\r
27 HELP;\r
28                 return $help;\r
29         }\r
30 \r
31         protected function doExecute()\r
32         {\r
33                 if ($this->getOption('v')) {\r
34                         $this->out('Class: ' . __CLASS__);\r
35                         $this->out('Arguments: ' . var_export($this->args, true));\r
36                         $this->out('Options: ' . var_export($this->options, true));\r
37                 }\r
38 \r
39                 if (count($this->args) == 0) {\r
40                         $this->out($this->getHelp());\r
41                         return 0;\r
42                 }\r
43 \r
44                 if (count($this->args) > 1) {\r
45                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');\r
46                 }\r
47 \r
48                 $file = $this->getArgument(0);\r
49                 if (!file_exists($file)) {\r
50                         throw new \RuntimeException('Unable to find specified file.');\r
51                 }\r
52 \r
53                 $data = file_get_contents($file);\r
54 \r
55                 $lines = explode("\n", $data);\r
56 \r
57                 $previous = "";\r
58 \r
59                 foreach ($lines AS $line) {\r
60                         $line = rtrim(trim($line, "\r"));\r
61 \r
62                         if (strstr(strtolower($line), "function")) {\r
63                                 $detect = strtolower(trim($line));\r
64                                 $detect = implode(" ", explode(" ", $detect));\r
65 \r
66                                 $found = false;\r
67 \r
68                                 if (substr($detect, 0, 9) == "function ") {\r
69                                         $found = true;\r
70                                 }\r
71 \r
72                                 if (substr($detect, 0, 19) == "protected function ") {\r
73                                         $found = true;\r
74                                 }\r
75 \r
76                                 if (substr($detect, 0, 17) == "private function ") {\r
77                                         $found = true;\r
78                                 }\r
79 \r
80                                 if (substr($detect, 0, 23) == "public static function ") {\r
81                                         $found = true;\r
82                                 }\r
83 \r
84                                 if (substr($detect, 0, 24) == "private static function ") {\r
85                                         $found = true;\r
86                                 }\r
87 \r
88                                 if (substr($detect, 0, 10) == "function (") {\r
89                                         $found = false;\r
90                                 }\r
91 \r
92                                 if ($found && ( trim($previous) == "*/")) {\r
93                                         $found = false;\r
94                                 }\r
95 \r
96                                 if ($found) {\r
97                                         $this->out($this->addDocumentation($line));\r
98                                 }\r
99                         }\r
100                         $this->out($line);\r
101                         $previous = $line;\r
102                 }\r
103 \r
104                 return 0;\r
105         }\r
106 \r
107         /**\r
108          * @brief Adds a doxygen header\r
109          *\r
110          * @param string $line The current line of the document\r
111          *\r
112          * @return string added doxygen header\r
113          */\r
114         private function addDocumentation($line)\r
115         {\r
116                 $trimmed = ltrim($line);\r
117                 $length = strlen($line) - strlen($trimmed);\r
118                 $space = substr($line, 0, $length);\r
119 \r
120                 $block = $space . "/**\n" .\r
121                         $space . " * @brief \n" .\r
122                         $space . " *\n"; /**/\r
123 \r
124 \r
125                 $left = strpos($line, "(");\r
126                 $line = substr($line, $left + 1);\r
127 \r
128                 $right = strpos($line, ")");\r
129                 $line = trim(substr($line, 0, $right));\r
130 \r
131                 if ($line != "") {\r
132                         $parameters = explode(",", $line);\r
133                         foreach ($parameters AS $parameter) {\r
134                                 $parameter = trim($parameter);\r
135                                 $splitted = explode("=", $parameter);\r
136 \r
137                                 $block .= $space . " * @param " . trim($splitted[0], "& ") . "\n";\r
138                         }\r
139                         if (count($parameters) > 0) $block .= $space . " *\n";\r
140                 }\r
141 \r
142                 $block .= $space . " * @return \n" .\r
143                         $space . " */\n";\r
144 \r
145                 return $block;\r
146         }\r
147 \r
148 }\r