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); } }
/** * Execute the console command. * * @return mixed */ public function handle() { $this->base = ucfirst(camel_case(str_singular($this->argument('entity')))); $this->table = $this->ask('Table name', snake_case(str_plural($this->base))); $this->path = ucfirst($this->ask('Namespace', config('resource_api.path', 'API'))); $this->prefix = $this->ask('Prefix route', false); $this->generateDocumentation = $this->confirm('Generate documentation? (Require apidocjs)', 'yes'); $createLayers = []; foreach ($this->layers as $layer) { $createLayer = $this->checkIfCreate($layer); if (!$createLayer) { $this->error('Aborted!'); exit; } $createLayers[$layer] = true; } $this->info("The following files will be created:"); foreach ($createLayers as $layer => $create) { $file = $this->{$layer}->getFilename(); $this->info($file); } if ($this->confirm('Continue?', 'yes')) { $migration = false; $run_migration = false; $route = false; $middlewares = ''; foreach ($createLayers as $layer => $create) { $generate = $this->{$layer}->generate(); $this->info($generate); } if ($this->confirm('Generate migration?', 'yes')) { $createMigration = $this->migration->generate($this->path, snake_case(str_plural($this->base)), $this->table); if ($createMigration['success']) { $migration = true; $this->info($createMigration['message']); if ($this->confirm('Run migration?', 'yes')) { $run_migration = true; try { $this->call('migrate', ['--path' => 'database/migrations/' . $this->path . '/' . snake_case(str_plural($this->base))]); } catch (\Exception $e) { $this->error($e->getMessage()); } } } else { $this->error($createMigration['error']); } } if ($this->confirm('Add routes resource?', 'yes')) { //$path = snake_case(str_plural($this->base)); $middlewares = $this->ask('Middlewares or middleware groups (comma separated)', false); $mdws = []; if ($middlewares) { $mdws = explode(',', str_replace(' ', '', $middlewares)); } $middlewares = implode(',', $mdws); $route = true; } $api_resource = APIResource::firstOrNew(['base' => $this->base, 'namespace' => $this->path]); $api_resource->base = $this->base; $api_resource->namespace = $this->path; $api_resource->table = $this->table; $api_resource->prefix = $this->prefix ? $this->prefix : ''; $api_resource->documentation = $this->generateDocumentation; $api_resource->migration = $migration; $api_resource->run_migration = $run_migration; $api_resource->route = $route; $api_resource->middlewares = $middlewares ? $middlewares : ''; $api_resource->save(); $this->route->generateAll(); if ($this->generateDocumentation) { $docs = $this->documentation->generateAll(); foreach ($docs as $doc) { if ($doc['success']) { $this->info($doc['message']); } else { $this->info($doc['error']); } } } } else { $this->error('Aborted!'); exit; } $this->info('Finish!'); exit; }