Skip to content

Commit

Permalink
Adding --with options to controller console create
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaldreams committed Dec 3, 2018
1 parent 79eb4b1 commit 5629071
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/lara-crud/Console/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@

use Illuminate\Console\Command;
use LaraCrud\Crud\Controller as ControllerCrud;
use LaraCrud\Crud\Policy;
use LaraCrud\Crud\RequestController as RequestControllerCrud;
use LaraCrud\Helpers\Helper;
use LaraCrud\Crud\RequestResource as RequestResourceCrud;


class Controller extends Command
{
use Helper;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = "laracrud:controller {model} {name?} {--only=} {--api} {--parent=}";
protected $signature = "laracrud:controller {model} {name?} {--only=} {--api} {--parent=} {--with=}";

/**
* The console command description.
Expand All @@ -40,13 +45,41 @@ public function handle()
$name = $this->argument('name');
$only = $this->option('only');
$api = $this->option('api');
$with = $this->option('with');
$withArr = !empty($with) ? explode(",", $with) : [];
$onlyArr = !empty($only) ? explode(",", $only) : '';
$parent = $this->option('parent');

if (in_array("request", $withArr)) {
$modelFullName = $this->modelFullName($model);
if (class_exists($modelFullName)) {
$modelObj = new $modelFullName;
$requestResource = new RequestResourceCrud($modelObj->getTable(), false, $api);
$requestResource->save();
$this->info('Request controller classes created successfully');
}
}

$controllerCrud = new ControllerCrud($model, $name, $onlyArr, $api, $parent);
$controllerCrud->save();
$this->info('Controller class successfully created');

if (in_array("policy", $withArr)) {
$policyCrud = new Policy($model, $controllerCrud->getFullName());
$policyCrud->save();
$this->info('Policy class created successfully');
}
} catch (\Exception $ex) {
$this->error($ex->getMessage() . ' on line ' . $ex->getLine() . ' in ' . $ex->getFile());
}
}

private function modelFullName($model)
{
$modelNamespace = $this->getFullNS(config('laracrud.model.namespace', 'App'));
if (!class_exists($model)) {
return $modelNamespace . '\\' . $model;
}
return false;
}
}
9 changes: 9 additions & 0 deletions src/lara-crud/Crud/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,13 @@ private function setParent($parent)
}
}

/**
* Get full newly created fully qualified Class namespace
*/
public function getFullName()
{
$fileName = !empty($this->fileName) ? $this->getFileName($this->fileName) : $this->controllerName . 'Controller';
return $this->namespace . '\\' . $fileName;
}

}
8 changes: 8 additions & 0 deletions src/lara-crud/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,12 @@ protected function makeNamespaceUseString()
return $retStr;
}

/**
* Get full newly created fully qualified Class namespace
*/
public function getFullName()
{
return $this->namespace . '\\' . $this->fileName;
}

}

0 comments on commit 5629071

Please sign in to comment.