Completed
Push — master ( 30f758...6ad044 )
by Sherif
09:12
created
src/Modules/Core/Console/Commands/GenerateDoc.php 1 patch
Indentation   +243 added lines, -243 removed lines patch added patch discarded remove patch
@@ -6,275 +6,275 @@
 block discarded – undo
6 6
 
7 7
 class GenerateDoc extends Command
8 8
 {
9
-    /**
10
-     * The name and signature of the console command.
11
-     *
12
-     * @var string
13
-     */
14
-    protected $signature = 'doc:generate';
9
+	/**
10
+	 * The name and signature of the console command.
11
+	 *
12
+	 * @var string
13
+	 */
14
+	protected $signature = 'doc:generate';
15 15
 
16
-    /**
17
-     * The console command description.
18
-     *
19
-     * @var string
20
-     */
21
-    protected $description = 'Generate api documentation';
16
+	/**
17
+	 * The console command description.
18
+	 *
19
+	 * @var string
20
+	 */
21
+	protected $description = 'Generate api documentation';
22 22
 
23
-    /**
24
-     * Create a new command instance.
25
-     *
26
-     * @return void
27
-     */
28
-    public function __construct()
29
-    {
30
-        parent::__construct();
31
-    }
23
+	/**
24
+	 * Create a new command instance.
25
+	 *
26
+	 * @return void
27
+	 */
28
+	public function __construct()
29
+	{
30
+		parent::__construct();
31
+	}
32 32
 
33
-    /**
34
-     * Execute the console command.
35
-     *
36
-     * @return mixed
37
-     */
38
-    public function handle()
39
-    {
40
-        $docData           = [];
41
-        $docData['models'] = [];
42
-        $routes            = $this->getRoutes();
43
-        foreach ($routes as $route) 
44
-        {
45
-            if ($route) 
46
-            {
47
-                $actoinArray = explode('@', $route['action']);
48
-                if(array_get($actoinArray, 1, false))
49
-                {
50
-                    $controller       = $actoinArray[0];
51
-                    $method           = $actoinArray[1];
52
-                    $route['name']    = $method !== 'index' ? $method : 'list';
33
+	/**
34
+	 * Execute the console command.
35
+	 *
36
+	 * @return mixed
37
+	 */
38
+	public function handle()
39
+	{
40
+		$docData           = [];
41
+		$docData['models'] = [];
42
+		$routes            = $this->getRoutes();
43
+		foreach ($routes as $route) 
44
+		{
45
+			if ($route) 
46
+			{
47
+				$actoinArray = explode('@', $route['action']);
48
+				if(array_get($actoinArray, 1, false))
49
+				{
50
+					$controller       = $actoinArray[0];
51
+					$method           = $actoinArray[1];
52
+					$route['name']    = $method !== 'index' ? $method : 'list';
53 53
                     
54
-                    $reflectionClass  = new \ReflectionClass($controller);
55
-                    $reflectionMethod = $reflectionClass->getMethod($method);
56
-                    $classProperties  = $reflectionClass->getDefaultProperties();
57
-                    $skipLoginCheck   = array_key_exists('skipLoginCheck', $classProperties) ? $classProperties['skipLoginCheck'] : false;
58
-                    $validationRules  = array_key_exists('validationRules', $classProperties) ? $classProperties['validationRules'] : false;
54
+					$reflectionClass  = new \ReflectionClass($controller);
55
+					$reflectionMethod = $reflectionClass->getMethod($method);
56
+					$classProperties  = $reflectionClass->getDefaultProperties();
57
+					$skipLoginCheck   = array_key_exists('skipLoginCheck', $classProperties) ? $classProperties['skipLoginCheck'] : false;
58
+					$validationRules  = array_key_exists('validationRules', $classProperties) ? $classProperties['validationRules'] : false;
59 59
 
60
-                    $this->processDocBlock($route, $reflectionMethod);
61
-                    $this->getHeaders($route, $method, $skipLoginCheck);
62
-                    $this->getPostData($route, $reflectionMethod, $validationRules);
60
+					$this->processDocBlock($route, $reflectionMethod);
61
+					$this->getHeaders($route, $method, $skipLoginCheck);
62
+					$this->getPostData($route, $reflectionMethod, $validationRules);
63 63
 
64
-                    $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']);
64
+					$route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']);
65 65
 
66
-                    preg_match('/api\/([^#]+)\//iU', $route['uri'], $module);
67
-                    $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/' . $module[1] . '/') - 1)][] = $route;
66
+					preg_match('/api\/([^#]+)\//iU', $route['uri'], $module);
67
+					$docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/' . $module[1] . '/') - 1)][] = $route;
68 68
 
69
-                    $this->getModels($classProperties['model'], $docData);   
70
-                }
71
-            }
72
-        }
69
+					$this->getModels($classProperties['model'], $docData);   
70
+				}
71
+			}
72
+		}
73 73
         
74
-        $docData['errors'] = $this->getErrors();
75
-        \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData));
76
-    }
74
+		$docData['errors'] = $this->getErrors();
75
+		\File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData));
76
+	}
77 77
 
78
-    /**
79
-     * Get list of all registered routes.
80
-     * 
81
-     * @return collection
82
-     */
83
-    protected function getRoutes()
84
-    {
85
-        return collect(\Route::getRoutes())->map(function ($route) {
86
-            if (strpos($route->uri(), 'api') !== false) 
87
-            {
88
-                return [
89
-                    'method' => $route->methods()[0],
90
-                    'uri'    => $route->uri(),
91
-                    'action' => $route->getActionName(),
92
-                    'prefix' => $route->getPrefix()
93
-                ];
94
-            }
95
-            return false;
96
-        })->all();
97
-    }
78
+	/**
79
+	 * Get list of all registered routes.
80
+	 * 
81
+	 * @return collection
82
+	 */
83
+	protected function getRoutes()
84
+	{
85
+		return collect(\Route::getRoutes())->map(function ($route) {
86
+			if (strpos($route->uri(), 'api') !== false) 
87
+			{
88
+				return [
89
+					'method' => $route->methods()[0],
90
+					'uri'    => $route->uri(),
91
+					'action' => $route->getActionName(),
92
+					'prefix' => $route->getPrefix()
93
+				];
94
+			}
95
+			return false;
96
+		})->all();
97
+	}
98 98
 
99
-    /**
100
-     * Generate headers for the given route.
101
-     * 
102
-     * @param  array  &$route
103
-     * @param  string $method
104
-     * @param  array  $skipLoginCheck
105
-     * @return void
106
-     */
107
-    protected function getHeaders(&$route, $method, $skipLoginCheck)
108
-    {
109
-        $route['headers'] = [
110
-        'Accept'       => 'application/json',
111
-        'Content-Type' => 'application/json',
112
-        'locale'       => 'The language of the returned data: ar, en or all.',
113
-        'time-zone'    => 'Your locale time zone',
114
-        ];
99
+	/**
100
+	 * Generate headers for the given route.
101
+	 * 
102
+	 * @param  array  &$route
103
+	 * @param  string $method
104
+	 * @param  array  $skipLoginCheck
105
+	 * @return void
106
+	 */
107
+	protected function getHeaders(&$route, $method, $skipLoginCheck)
108
+	{
109
+		$route['headers'] = [
110
+		'Accept'       => 'application/json',
111
+		'Content-Type' => 'application/json',
112
+		'locale'       => 'The language of the returned data: ar, en or all.',
113
+		'time-zone'    => 'Your locale time zone',
114
+		];
115 115
 
116 116
 
117
-        if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) 
118
-        {
119
-            $route['headers']['Authorization'] = 'Bearer {token}';
120
-        }
121
-    }
117
+		if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) 
118
+		{
119
+			$route['headers']['Authorization'] = 'Bearer {token}';
120
+		}
121
+	}
122 122
 
123
-    /**
124
-     * Generate description and params for the given route
125
-     * based on the docblock.
126
-     * 
127
-     * @param  array  &$route
128
-     * @param  object $reflectionMethod]
129
-     * @return void
130
-     */
131
-    protected function processDocBlock(&$route, $reflectionMethod)
132
-    {
133
-        $factory                 = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
134
-        $docblock                = $factory->create($reflectionMethod->getDocComment());
135
-        $route['description']    = trim(preg_replace('/\s+/', ' ', $docblock->getSummary()));
136
-        $params                  = $docblock->getTagsByName('param');
137
-        $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName();
138
-        foreach ($params as $param) 
139
-        {
140
-            $name = $param->getVariableName();
141
-            if ($name !== 'request') 
142
-            {
143
-                $route['parametars'][$param->getVariableName()] = $param->getDescription()->render();
144
-            }
145
-        }
146
-    }
123
+	/**
124
+	 * Generate description and params for the given route
125
+	 * based on the docblock.
126
+	 * 
127
+	 * @param  array  &$route
128
+	 * @param  object $reflectionMethod]
129
+	 * @return void
130
+	 */
131
+	protected function processDocBlock(&$route, $reflectionMethod)
132
+	{
133
+		$factory                 = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
134
+		$docblock                = $factory->create($reflectionMethod->getDocComment());
135
+		$route['description']    = trim(preg_replace('/\s+/', ' ', $docblock->getSummary()));
136
+		$params                  = $docblock->getTagsByName('param');
137
+		$route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName();
138
+		foreach ($params as $param) 
139
+		{
140
+			$name = $param->getVariableName();
141
+			if ($name !== 'request') 
142
+			{
143
+				$route['parametars'][$param->getVariableName()] = $param->getDescription()->render();
144
+			}
145
+		}
146
+	}
147 147
 
148
-    /**
149
-     * Generate post body for the given route.
150
-     * 
151
-     * @param  array  &$route
152
-     * @param  object $reflectionMethod
153
-     * @param  array  $validationRules
154
-     * @return void
155
-     */
156
-    protected function getPostData(&$route, $reflectionMethod, $validationRules)
157
-    {
158
-        if ($route['method'] == 'POST') 
159
-        {
160
-            $body = $this->getMethodBody($reflectionMethod);
148
+	/**
149
+	 * Generate post body for the given route.
150
+	 * 
151
+	 * @param  array  &$route
152
+	 * @param  object $reflectionMethod
153
+	 * @param  array  $validationRules
154
+	 * @return void
155
+	 */
156
+	protected function getPostData(&$route, $reflectionMethod, $validationRules)
157
+	{
158
+		if ($route['method'] == 'POST') 
159
+		{
160
+			$body = $this->getMethodBody($reflectionMethod);
161 161
 
162
-            preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match);
163
-            if (count($match)) 
164
-            {
165
-                if ($match[1] == '$this->validationRules')
166
-                {
167
-                    $route['body'] = $validationRules;
168
-                }
169
-                else
170
-                {
171
-                    $route['body'] = eval('return ' . str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]) . ';');
172
-                }
162
+			preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match);
163
+			if (count($match)) 
164
+			{
165
+				if ($match[1] == '$this->validationRules')
166
+				{
167
+					$route['body'] = $validationRules;
168
+				}
169
+				else
170
+				{
171
+					$route['body'] = eval('return ' . str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]) . ';');
172
+				}
173 173
 
174
-                foreach ($route['body'] as &$rule) 
175
-                {
176
-                    if(strpos($rule, 'unique'))
177
-                    {
178
-                        $rule = substr($rule, 0, strpos($rule, 'unique') + 6);
179
-                    }
180
-                    elseif(strpos($rule, 'exists'))
181
-                    {
182
-                        $rule = substr($rule, 0, strpos($rule, 'exists') - 1);
183
-                    }
184
-                }
185
-            }
186
-            else
187
-            {
188
-                $route['body'] = 'conditions';
189
-            }
190
-        }
191
-    }
174
+				foreach ($route['body'] as &$rule) 
175
+				{
176
+					if(strpos($rule, 'unique'))
177
+					{
178
+						$rule = substr($rule, 0, strpos($rule, 'unique') + 6);
179
+					}
180
+					elseif(strpos($rule, 'exists'))
181
+					{
182
+						$rule = substr($rule, 0, strpos($rule, 'exists') - 1);
183
+					}
184
+				}
185
+			}
186
+			else
187
+			{
188
+				$route['body'] = 'conditions';
189
+			}
190
+		}
191
+	}
192 192
 
193
-    /**
194
-     * Generate application errors.
195
-     * 
196
-     * @return array
197
-     */
198
-    protected function getErrors()
199
-    {
200
-        $errors          = [];
201
-        $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler');
202
-        foreach ($reflectionClass->getMethods() as $method) 
203
-        {
204
-            $methodName       = $method->getName();
205
-            $reflectionMethod = $reflectionClass->getMethod($methodName);
206
-            $body             = $this->getMethodBody($reflectionMethod);
193
+	/**
194
+	 * Generate application errors.
195
+	 * 
196
+	 * @return array
197
+	 */
198
+	protected function getErrors()
199
+	{
200
+		$errors          = [];
201
+		$reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler');
202
+		foreach ($reflectionClass->getMethods() as $method) 
203
+		{
204
+			$methodName       = $method->getName();
205
+			$reflectionMethod = $reflectionClass->getMethod($methodName);
206
+			$body             = $this->getMethodBody($reflectionMethod);
207 207
 
208
-            preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match);
208
+			preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match);
209 209
 
210
-            if (count($match)) 
211
-            {
212
-                $errors[$match[1]][] = $methodName;
213
-            }
214
-        }
210
+			if (count($match)) 
211
+			{
212
+				$errors[$match[1]][] = $methodName;
213
+			}
214
+		}
215 215
 
216
-        return $errors;
217
-    }
216
+		return $errors;
217
+	}
218 218
 
219
-    /**
220
-     * Get the given method body code.
221
-     * 
222
-     * @param  object $reflectionMethod
223
-     * @return string
224
-     */
225
-    protected function getMethodBody($reflectionMethod)
226
-    {
227
-        $filename   = $reflectionMethod->getFileName();
228
-        $start_line = $reflectionMethod->getStartLine() - 1;
229
-        $end_line   = $reflectionMethod->getEndLine();
230
-        $length     = $end_line - $start_line;         
231
-        $source     = file($filename);
232
-        $body       = implode("", array_slice($source, $start_line, $length));
233
-        $body       = trim(preg_replace('/\s+/', '', $body));
219
+	/**
220
+	 * Get the given method body code.
221
+	 * 
222
+	 * @param  object $reflectionMethod
223
+	 * @return string
224
+	 */
225
+	protected function getMethodBody($reflectionMethod)
226
+	{
227
+		$filename   = $reflectionMethod->getFileName();
228
+		$start_line = $reflectionMethod->getStartLine() - 1;
229
+		$end_line   = $reflectionMethod->getEndLine();
230
+		$length     = $end_line - $start_line;         
231
+		$source     = file($filename);
232
+		$body       = implode("", array_slice($source, $start_line, $length));
233
+		$body       = trim(preg_replace('/\s+/', '', $body));
234 234
 
235
-        return $body;
236
-    }
235
+		return $body;
236
+	}
237 237
 
238
-    /**
239
-     * Get example object of all availble models.
240
-     * 
241
-     * @param  string $modelName
242
-     * @param  array  $docData
243
-     * @return string
244
-     */
245
-    protected function getModels($modelName, &$docData)
246
-    {
247
-        if ($modelName && ! array_key_exists($modelName, $docData['models'])) 
248
-        {
249
-            $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass;
250
-            $model      = factory($modelClass)->make();
251
-            $modelArr   = $model->toArray();
238
+	/**
239
+	 * Get example object of all availble models.
240
+	 * 
241
+	 * @param  string $modelName
242
+	 * @param  array  $docData
243
+	 * @return string
244
+	 */
245
+	protected function getModels($modelName, &$docData)
246
+	{
247
+		if ($modelName && ! array_key_exists($modelName, $docData['models'])) 
248
+		{
249
+			$modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass;
250
+			$model      = factory($modelClass)->make();
251
+			$modelArr   = $model->toArray();
252 252
 
253
-            if ( $model->trans && ! $model->trans->count()) 
254
-            {
255
-                $modelArr['trans'] = [
256
-                    'en' => factory($modelClass . 'Translation')->make()->toArray()
257
-                ];
258
-            }
253
+			if ( $model->trans && ! $model->trans->count()) 
254
+			{
255
+				$modelArr['trans'] = [
256
+					'en' => factory($modelClass . 'Translation')->make()->toArray()
257
+				];
258
+			}
259 259
 
260
-            $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT);
261
-        }
262
-    }
260
+			$docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT);
261
+		}
262
+	}
263 263
 
264
-    /**
265
-     * Get the route response object type.
266
-     * 
267
-     * @param  string $modelName
268
-     * @param  string $method
269
-     * @param  string $returnDocBlock
270
-     * @return array
271
-     */
272
-    protected function getResponseObject($modelName, $method, $returnDocBlock)
273
-    {
274
-        $config    = \CoreConfig::getConfig();
275
-        $relations = array_key_exists($modelName, $config['relations']) ? array_key_exists($method, $config['relations'][$modelName]) ? $config['relations'][$modelName] : false : false;
276
-        $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName;
264
+	/**
265
+	 * Get the route response object type.
266
+	 * 
267
+	 * @param  string $modelName
268
+	 * @param  string $method
269
+	 * @param  string $returnDocBlock
270
+	 * @return array
271
+	 */
272
+	protected function getResponseObject($modelName, $method, $returnDocBlock)
273
+	{
274
+		$config    = \CoreConfig::getConfig();
275
+		$relations = array_key_exists($modelName, $config['relations']) ? array_key_exists($method, $config['relations'][$modelName]) ? $config['relations'][$modelName] : false : false;
276
+		$modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName;
277 277
 
278
-        return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false;
279
-    }
278
+		return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false;
279
+	}
280 280
 }
Please login to merge, or discard this patch.