public function generateAll() { $resources = APIResource::where('route', true)->get(); $namespaces = $this->generateNamespaces($resources); $commands = []; foreach ($namespaces as $namespace => $controllers) { $command = 'apidoc -i ' . app_path() . '/Http/Controllers/' . $namespace . ' '; foreach ($controllers as $controller) { $command .= '-f "' . $controller . '" '; } $command .= '-o ' . public_path() . '/docs/' . $namespace; $command .= ' -t ' . realpath(__DIR__ . '/../../templateDoc'); $commands[] = ['namespace' => $namespace, 'command' => $command, 'index' => public_path() . '/docs/' . $namespace . '/index.html']; } $return = []; $responses = []; foreach ($commands as $command) { exec($command['command'], $return, $status); if ($status != 0) { $response = ['success' => false, 'error' => 'Cannot generate documentation for namespace ' . $command['namespace'] . ' in: ' . public_path() . '/docs/' . $command['namespace']]; } else { $find = '<base href="#" />'; $replace = '<base href="/docs/' . $command['namespace'] . '/" />'; File::put($command['index'], str_replace($find, $replace, file_get_contents($command['index']))); $response = ['success' => true, 'message' => 'Documentation generated for namespace ' . $command['namespace'] . ' in: ' . public_path() . '/docs/' . $command['namespace']]; } $responses[] = $response; } return $responses; }
public function generateAll() { $resources = APIResource::where('route', true)->get(); $router = $this->generateRouter($resources); $file = app_path() . '/Http/routes.php'; $delimiter = '/** GENERATE BY iramgutierrez/laravel-resource-api DO NOT REMOVE **/'; $contentRoutes = ""; $contentRoutes .= $delimiter; $contentRoutes .= "\n"; $contentRoutes .= "\n"; /* NAMESPACES */ foreach ($router['namespaces'] as $namespace => $ns) { $contentRoutes .= "Route::group(['namespace' => '" . $namespace . "'"; $contentRoutes .= '] , function() {'; $contentRoutes .= "\n"; /* NAMESPACES PREFIXES */ foreach ($ns['prefixes'] as $prefix => $p) { $contentRoutes .= " Route::group(['prefix' => '" . $prefix . "'"; $contentRoutes .= '] , function() {'; $contentRoutes .= "\n"; /* NAMESPACES PREFIXES MIDDLEWARE */ foreach ($p['middlewares'] as $middleware => $mw) { $middleware = "['" . str_replace(',', "','", $middleware) . "']"; $contentRoutes .= " Route::group(['middleware' => " . $middleware; $contentRoutes .= '] , function() {'; $contentRoutes .= "\n"; /* NAMESPACES PREFIXES MIDDLEWARE ROUTES */ foreach ($mw['routes'] as $r => $route) { $contentRoutes .= " Route::resource('" . $route['path'] . "' , '" . $route['controller'] . "');"; $contentRoutes .= "\n"; } /* NAMESPACES PREFIXES MIDDLEWARE ROUTES */ $contentRoutes .= ' });'; $contentRoutes .= "\n"; } /* NAMESPACES PREFIXES MIDDLEWARE */ /* NAMESPACES PREFIXES ROUTES */ foreach ($p['routes'] as $r => $route) { $contentRoutes .= " Route::resource('" . $route['path'] . "' , '" . $route['controller'] . "');"; $contentRoutes .= "\n"; } /* NAMESPACES PREFIXES ROUTES */ $contentRoutes .= ' });'; $contentRoutes .= "\n"; } /* NAMESPACES PREFIXES */ /* NAMESPACES MIDDLEWARES */ foreach ($ns['middlewares'] as $middleware => $mw) { $middleware = "['" . str_replace(',', "','", $middleware) . "']"; $contentRoutes .= " Route::group(['middleware' => " . $middleware; $contentRoutes .= '] , function() {'; $contentRoutes .= "\n"; /* NAMESPACES MIDDLEWARES ROUTES */ foreach ($mw['routes'] as $r => $route) { $contentRoutes .= " Route::resource('" . $route['path'] . "' , '" . $route['controller'] . "');"; $contentRoutes .= "\n"; } /* NAMESPACES MIDDLEWARES ROUTES */ $contentRoutes .= ' });'; $contentRoutes .= "\n"; } /* NAMESPACES MIDDLEWARES */ /* NAMESPACES ROUTES */ foreach ($ns['routes'] as $r => $route) { $contentRoutes .= " Route::resource('" . $route['path'] . "' , '" . $route['controller'] . "');"; $contentRoutes .= "\n"; } /* NAMESPACES ROUTES */ $contentRoutes .= '});'; $contentRoutes .= "\n"; } /* NAMESPACES */ /* PREFIXES */ foreach ($router['prefixes'] as $prefix => $p) { $contentRoutes .= "Route::group(['prefix' => '" . $prefix . "'"; $contentRoutes .= '] , function() {'; $contentRoutes .= "\n"; /* PREFIXES MIDDLEWARES */ foreach ($p['middlewares'] as $middleware => $mw) { $middleware = "['" . str_replace(',', "','", $middleware) . "']"; $contentRoutes .= " Route::group(['middleware' => " . $middleware; $contentRoutes .= '] , function() {'; $contentRoutes .= "\n"; /* PREFIXES MIDDLEWARES ROUTES */ foreach ($mw['routes'] as $r => $route) { $contentRoutes .= " Route::resource('" . $route['path'] . "' , '" . $route['controller'] . "');"; $contentRoutes .= "\n"; } /* PREFIXES MIDDLEWARES ROUTES */ $contentRoutes .= ' });'; $contentRoutes .= "\n"; } /* PREFIXES MIDDLEWARES */ /* PREFIXES ROUTES */ foreach ($p['routes'] as $r => $route) { $contentRoutes .= " Route::resource('" . $route['path'] . "' , '" . $route['controller'] . "');"; $contentRoutes .= "\n"; } /* PREFIXES ROUTES */ $contentRoutes .= '});'; $contentRoutes .= "\n"; } /* PREFIXES */ /* MIDDLEWARES */ foreach ($router['middlewares'] as $middleware => $mw) { $middleware = "['" . str_replace(',', "','", $middleware) . "']"; $contentRoutes .= "Route::group(['middleware' => " . $middleware; $contentRoutes .= '] , function() {'; $contentRoutes .= "\n"; /* MIDDLEWARES ROUTES */ foreach ($mw['routes'] as $r => $route) { $contentRoutes .= " Route::resource('" . $route['path'] . "' , '" . $route['controller'] . "');"; $contentRoutes .= "\n"; } /* MIDDLEWARES ROUTES */ $contentRoutes .= '});'; $contentRoutes .= "\n"; } /* MIDDLEWARES */ /* ROUTES */ foreach ($router['routes'] as $r => $route) { $contentRoutes .= "Route::resource('" . $route['path'] . "' , '" . $route['controller'] . "');"; $contentRoutes .= "\n"; } /* ROUTES */ $contentRoutes .= "\n"; $contentRoutes .= $delimiter; $contentRoutes .= "\n"; $lines = file($file); $init = false; $preInit = false; $end = false; $content = ""; foreach ($lines as $l => $line) { if ($preInit) { $init = true; } if (strpos($line, $delimiter) !== false) { $preInit = true; } if (($init || $preInit) && !$end) { $content .= $line; } if (strpos($line, $delimiter) !== false && $init) { $end = true; } } if ($init && $end) { return File::put($file, str_replace($content, $contentRoutes, file_get_contents($file))); } else { return File::append($file, $contentRoutes); } }