diff --git a/src/Commands/BaseCommand.php b/src/Commands/BaseCommand.php index 04b1decb1..07463e6a8 100755 --- a/src/Commands/BaseCommand.php +++ b/src/Commands/BaseCommand.php @@ -302,6 +302,7 @@ public function getOptions() ['localized', null, InputOption::VALUE_NONE, 'Localize files.'], ['repositoryPattern', null, InputOption::VALUE_REQUIRED, 'Repository Pattern'], ['connection', null, InputOption::VALUE_REQUIRED, 'Specify connection name'], + ['moduleName', null, InputOption::VALUE_REQUIRED, 'Generate files to this module & namespace (eg. Admin)'], ]; } diff --git a/src/Commands/RollbackGeneratorCommand.php b/src/Commands/RollbackGeneratorCommand.php index 7100cc4f3..f06ab0dfb 100755 --- a/src/Commands/RollbackGeneratorCommand.php +++ b/src/Commands/RollbackGeneratorCommand.php @@ -79,7 +79,7 @@ public function handle() $this->commandData = new CommandData($this, $this->argument('type')); $this->commandData->config->mName = $this->commandData->modelName = $this->argument('model'); - $this->commandData->config->init($this->commandData, ['tableName', 'prefix', 'plural', 'views']); + $this->commandData->config->init($this->commandData, ['tableName', 'prefix', 'plural', 'views', 'moduleName']); $views = $this->commandData->getOption('views'); if (!empty($views)) { @@ -164,6 +164,7 @@ public function getOptions() ['prefix', null, InputOption::VALUE_REQUIRED, 'Prefix for all files'], ['plural', null, InputOption::VALUE_REQUIRED, 'Plural Model name'], ['views', null, InputOption::VALUE_REQUIRED, 'Views to rollback'], + ['moduleName', null, InputOption::VALUE_REQUIRED, 'Generate files to this module & namespace (eg. Admin)'], ]; } diff --git a/src/Common/GeneratorConfig.php b/src/Common/GeneratorConfig.php index fa6c70694..864a810bf 100755 --- a/src/Common/GeneratorConfig.php +++ b/src/Common/GeneratorConfig.php @@ -94,6 +94,7 @@ class GeneratorConfig 'repositoryPattern', 'localized', 'connection', + 'moduleName', ]; public $tableName; @@ -390,6 +391,55 @@ public function prepareOptions(CommandData &$commandData) $this->addOns['datatables'] = false; } } + if (!empty($this->options['moduleName'])) { + if (!file_exists(config_path('modules.php'))) { + $commandData->commandError('Can\'t use modules since config doesn\'t exist in '.config_path('modules.php').'.'); + exit; + } + // change folder locations + $this->modules_path = config('modules.paths.modules', base_path('Modules')).'/'.$this->options['moduleName']; + $config_paths = config('infyom.laravel_generator.path'); + $new_config_paths = []; + foreach ($config_paths as $key => $path) { + if (preg_match('/migrations/', $path)) { + $path = str_replace('migrations', 'Migrations', $path); + } + if (strpos($path, app_path()) === 0) { + $new_config_paths[$key] = str_replace(app_path(), $this->modules_path, $path); + } else { + $new_config_paths[$key] = str_replace(base_path(), $this->modules_path, $path); + } + $new_config_paths[$key][strlen($this->modules_path) + 1] = strtoupper($new_config_paths[$key][strlen($this->modules_path) + 1]); + } + config(['infyom.laravel_generator.path' => $new_config_paths]); + + // change namespace + $this->module_namespace = config('modules.namespace', 'Modules').'\\'.$this->options['moduleName'].'\\'; + $config_namespaces = config('infyom.laravel_generator.namespace'); + $new_config_namespaces = []; + foreach ($config_namespaces as $key => $namespace) { + if (strpos($namespace, 'App\\') === 0) { + $namespace = str_replace('App\\', '', $namespace); + } + $new_config_namespaces[$key] = $this->module_namespace.$namespace; + } + config(['infyom.laravel_generator.namespace' => $new_config_namespaces]); + + // change prefix + $viewPrefix = $this->prefixes['view']; + config(['infyom.laravel_generator.prefixes.view' => strtolower($this->options['moduleName']).'::']); + $this->prefixes['view'] = config('infyom.laravel_generator.prefixes.view'); + if ($this->getOption('plural')) { + $this->mPlural = $this->getOption('plural'); + } else { + $this->mPlural = Str::plural($this->mName); + } + $this->mSnakePlural = Str::snake($this->mPlural); + $this->pathViews = config( + 'infyom.laravel_generator.path.views', + base_path('resources/views/') + ).$viewPrefix.$this->mSnakePlural.'/'; + } } public function preparePrefixes() diff --git a/src/Common/GeneratorField.php b/src/Common/GeneratorField.php index d43416f27..2b21a6e99 100644 --- a/src/Common/GeneratorField.php +++ b/src/Common/GeneratorField.php @@ -29,7 +29,6 @@ class GeneratorField public $inForm = true; public $inIndex = true; public $inView = true; - public $isNotNull = false; /** * @param Column $column diff --git a/src/Common/GeneratorFieldRelation.php b/src/Common/GeneratorFieldRelation.php index dba27ec3f..9729564ad 100644 --- a/src/Common/GeneratorFieldRelation.php +++ b/src/Common/GeneratorFieldRelation.php @@ -29,6 +29,7 @@ public static function parseRelation($relationInput) public function getRelationFunctionText($relationText = null) { + $relationText = str_replace('.', '_', $relationText); $singularRelation = (!empty($this->relationName)) ? $this->relationName : Str::camel($relationText); $pluralRelation = (!empty($this->relationName)) ? $this->relationName : Str::camel(Str::plural($relationText)); diff --git a/src/Generators/Scaffold/MenuGenerator.php b/src/Generators/Scaffold/MenuGenerator.php index ae9360ab1..964b90128 100755 --- a/src/Generators/Scaffold/MenuGenerator.php +++ b/src/Generators/Scaffold/MenuGenerator.php @@ -23,6 +23,9 @@ class MenuGenerator extends BaseGenerator /** @var string */ private $menuTemplate; + /** @var bool */ + private $dontGenerateMenu; + public function __construct(CommandData $commandData) { $this->commandData = $commandData; @@ -33,21 +36,29 @@ public function __construct(CommandData $commandData) ).$commandData->getAddOn('menu.menu_file'); $this->templateType = config('infyom.laravel_generator.templates', 'adminlte-templates'); - $this->menuContents = file_get_contents($this->path); + if (!file_exists($this->path)) { + $this->dontGenerateMenu = true; + } else { + $this->menuContents = file_get_contents($this->path); - $templateName = 'menu_template'; + $templateName = 'menu_template'; - if ($this->commandData->isLocalizedTemplates()) { - $templateName .= '_locale'; - } + if ($this->commandData->isLocalizedTemplates()) { + $templateName .= '_locale'; + } - $this->menuTemplate = get_template('scaffold.layouts.'.$templateName, $this->templateType); + $this->menuTemplate = get_template('scaffold.layouts.'.$templateName, $this->templateType); - $this->menuTemplate = fill_template($this->commandData->dynamicVars, $this->menuTemplate); + $this->menuTemplate = fill_template($this->commandData->dynamicVars, $this->menuTemplate); + } } public function generate() { + if ($this->dontGenerateMenu) { + return true; + } + $this->menuContents .= $this->menuTemplate.infy_nl(); $existingMenuContents = file_get_contents($this->path); if (Str::contains($existingMenuContents, ''.$this->commandData->config->mHumanPlural.'')) {