Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow generation inside module #691

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)'],
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/Commands/RollbackGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)'],
];
}

Expand Down
50 changes: 50 additions & 0 deletions src/Common/GeneratorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class GeneratorConfig
'repositoryPattern',
'localized',
'connection',
'moduleName',
];

public $tableName;
Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion src/Common/GeneratorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class GeneratorField
public $inForm = true;
public $inIndex = true;
public $inView = true;
public $isNotNull = false;

/**
* @param Column $column
Expand Down
1 change: 1 addition & 0 deletions src/Common/GeneratorFieldRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
25 changes: 18 additions & 7 deletions src/Generators/Scaffold/MenuGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class MenuGenerator extends BaseGenerator
/** @var string */
private $menuTemplate;

/** @var bool */
private $dontGenerateMenu;

public function __construct(CommandData $commandData)
{
$this->commandData = $commandData;
Expand All @@ -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, '<span>'.$this->commandData->config->mHumanPlural.'</span>')) {
Expand Down