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

Scaffolding Improvements #486

Merged
merged 13 commits into from
Apr 3, 2022
Merged
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
78 changes: 45 additions & 33 deletions modules/backend/console/CreateController.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php namespace Backend\Console;

use Winter\Storm\Scaffold\GeneratorCommand;
use System\Console\BaseScaffoldCommand;
use Winter\Storm\Support\Str;

class CreateController extends GeneratorCommand
/**
* @TODO:
* - Support creating related permissions and navigation items and injecting them into the plugin
*/
class CreateController extends BaseScaffoldCommand
{
/**
* The default command name for lazy loading.
*
* @var string|null
* @var string|null The default command name for lazy loading.
*/
protected static $defaultName = 'create:controller';

/**
* The name and signature of this command.
*
* @var string
* @var string The name and signature of this command.
*/
protected $signature = 'create:controller
{plugin : The name of the plugin. <info>(eg: Winter.Blog)</info>}
Expand All @@ -24,23 +24,22 @@ class CreateController extends GeneratorCommand
{--model= : Defines the model name to use. If not provided, the singular name of the controller is used.}';

/**
* The console command description.
*
* @var string
* @var string The console command description.
*/
protected $description = 'Creates a new controller.';

/**
* The type of class being generated.
*
* @var string
* @var string The type of class being generated.
*/
protected $type = 'Controller';

/**
* A mapping of stub to generated file.
*
* @var array
* @var string The argument that the generated class name comes from
*/
protected $nameFrom = 'controller';

/**
* @var array A mapping of stub to generated file.
*/
protected $stubs = [
'scaffold/controller/_list_toolbar.stub' => 'controllers/{{lower_name}}/_list_toolbar.htm',
Expand All @@ -55,33 +54,46 @@ class CreateController extends GeneratorCommand

/**
* Prepare variables for stubs.
*
* return @array
*/
protected function prepareVars()
protected function prepareVars(): array
{
$pluginCode = $this->argument('plugin');

$parts = explode('.', $pluginCode);
$plugin = array_pop($parts);
$author = array_pop($parts);

$controller = $this->argument('controller');

$vars = parent::prepareVars();
/*
* Determine the model name to use,
* either supplied or singular from the controller name.
*/
$model = $this->option('model');
if (!$model) {
$model = Str::singular($controller);
$model = Str::singular($vars['name']);
}
$vars['model'] = $model;

return $vars;
}

/**
* Adds controller & model lang helpers to the vars
*/
protected function processVars($vars): array
{
$vars = parent::processVars($vars);

$vars['controller_url'] = "{$vars['plugin_url']}/{$vars['lower_name']}";
$vars['model_lang_key_short'] = "models.{$vars['lower_model']}";
$vars['model_lang_key'] = "{$vars['plugin_id']}::lang.{$vars['model_lang_key_short']}";

return $vars;
}

/**
* Gets the localization keys and values to be stored in the plugin's localization files
* Can reference $this->vars and $this->laravel->getLocale() internally
*/
protected function getLangKeys(): array
{
return [
'name' => $controller,
'model' => $model,
'author' => $author,
'plugin' => $plugin
"{$this->vars['model_lang_key_short']}.label" => $this->vars['title_singular_name'],
"{$this->vars['model_lang_key_short']}.label_plural" => $this->vars['title_plural_name'],
];
}
}
31 changes: 7 additions & 24 deletions modules/backend/console/CreateFormWidget.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php namespace Backend\Console;

use Winter\Storm\Scaffold\GeneratorCommand;
use System\Console\BaseScaffoldCommand;

class CreateFormWidget extends GeneratorCommand
class CreateFormWidget extends BaseScaffoldCommand
{
/**
* The default command name for lazy loading.
Expand Down Expand Up @@ -35,6 +35,11 @@ class CreateFormWidget extends GeneratorCommand
*/
protected $type = 'FormWidget';

/**
* @var string The argument that the generated class name comes from
*/
protected $nameFrom = 'widget';

/**
* A mapping of stub to generated file.
*
Expand All @@ -46,26 +51,4 @@ class CreateFormWidget extends GeneratorCommand
'scaffold/formwidget/stylesheet.stub' => 'formwidgets/{{lower_name}}/assets/css/{{lower_name}}.css',
'scaffold/formwidget/javascript.stub' => 'formwidgets/{{lower_name}}/assets/js/{{lower_name}}.js',
];

/**
* Prepare variables for stubs.
*
* return @array
*/
protected function prepareVars()
{
$pluginCode = $this->argument('plugin');

$parts = explode('.', $pluginCode);
$plugin = array_pop($parts);
$author = array_pop($parts);

$widget = $this->argument('widget');

return [
'name' => $widget,
'author' => $author,
'plugin' => $plugin
];
}
}
31 changes: 7 additions & 24 deletions modules/backend/console/CreateReportWidget.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php namespace Backend\Console;

use Winter\Storm\Scaffold\GeneratorCommand;
use System\Console\BaseScaffoldCommand;

class CreateReportWidget extends GeneratorCommand
class CreateReportWidget extends BaseScaffoldCommand
{
/**
* The default command name for lazy loading.
Expand Down Expand Up @@ -35,6 +35,11 @@ class CreateReportWidget extends GeneratorCommand
*/
protected $type = 'ReportWidget';

/**
* @var string The argument that the generated class name comes from
*/
protected $nameFrom = 'widget';

/**
* A mapping of stub to generated file.
*
Expand All @@ -44,26 +49,4 @@ class CreateReportWidget extends GeneratorCommand
'scaffold/reportwidget/reportwidget.stub' => 'reportwidgets/{{studly_name}}.php',
'scaffold/reportwidget/widget.stub' => 'reportwidgets/{{lower_name}}/partials/_{{lower_name}}.htm',
];

/**
* Prepare variables for stubs.
*
* return @array
*/
protected function prepareVars()
{
$pluginCode = $this->argument('plugin');

$parts = explode('.', $pluginCode);
$plugin = array_pop($parts);
$author = array_pop($parts);

$widget = $this->argument('widget');

return [
'name' => $widget,
'author' => $author,
'plugin' => $plugin
];
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<div data-control="toolbar">
<a
href="<?= Backend::url('{{lower_author}}/{{lower_plugin}}/{{lower_name}}/create') ?>"
href="<?= Backend::url('{{ controller_url }}/create') ?>"
class="btn btn-primary wn-icon-plus">
New {{title_singular_name}}
<?= e(trans('backend::lang.form.create_title', ['name' => trans('{{ model_lang_key }}.label')])); ?>
</a>

<button
class="btn btn-danger wn-icon-trash-o"
disabled="disabled"
onclick="$(this).data('request-data', { checked: $('.control-list').listWidget('getChecked') })"
data-request="onDelete"
data-request-confirm="Are you sure you want to delete the selected {{title_plural_name}}?"
data-request-confirm="<?= e(trans('backend::lang.list.delete_selected_confirm')); ?>"
data-trigger-action="enable"
data-trigger=".control-list input[type=checkbox]"
data-trigger-condition="checked"
data-request-success="$(this).prop('disabled', 'disabled')"
data-stripe-load-indicator>
Delete selected
<?= e(trans('backend::lang.list.delete_selected')); ?>
</button>
</div>
16 changes: 8 additions & 8 deletions modules/backend/console/scaffold/controller/config_form.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
# ===================================

# Record name
name: {{title_singular_name}}
name: '{{ model_lang_key }}.label'

# Model Form Field configuration
form: $/{{lower_author}}/{{lower_plugin}}/models/{{lower_model}}/fields.yaml
form: $/{{ plugin_folder }}/models/{{ lower_model }}/fields.yaml

# Model Class name
modelClass: {{studly_author}}\{{studly_plugin}}\Models\{{studly_model}}
modelClass: {{ plugin_namespace }}\Models\{{ studly_model }}

# Default redirect location
defaultRedirect: {{lower_author}}/{{lower_plugin}}/{{lower_name}}
defaultRedirect: {{ controller_url }}

# Create page
create:
title: backend::lang.form.create_title
redirect: {{lower_author}}/{{lower_plugin}}/{{lower_name}}/update/:id
redirectClose: {{lower_author}}/{{lower_plugin}}/{{lower_name}}
redirect: {{ controller_url }}/update/:id
redirectClose: {{ controller_url }}

# Update page
update:
title: backend::lang.form.update_title
redirect: {{lower_author}}/{{lower_plugin}}/{{lower_name}}
redirectClose: {{lower_author}}/{{lower_plugin}}/{{lower_name}}
redirect: {{ controller_url }}
redirectClose: {{ controller_url }}

# Preview page
preview:
Expand Down
8 changes: 4 additions & 4 deletions modules/backend/console/scaffold/controller/config_list.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
# ===================================

# Model List Column configuration
list: $/{{lower_author}}/{{lower_plugin}}/models/{{lower_model}}/columns.yaml
list: $/{{ plugin_folder }}/models/{{ lower_model }}/columns.yaml

# Model Class name
modelClass: {{studly_author}}\{{studly_plugin}}\Models\{{studly_model}}
modelClass: {{ plugin_namespace }}\Models\{{ studly_model }}

# List Title
title: Manage {{title_plural_name}}
title: '{{ model_lang_key }}.label_plural'

# Link URL for each record
recordUrl: {{lower_author}}/{{lower_plugin}}/{{lower_name}}/update/:id
recordUrl: {{ controller_url }}/update/:id

# Message to display if the list is empty
noRecordsMessage: backend::lang.list.no_records
Expand Down
8 changes: 4 additions & 4 deletions modules/backend/console/scaffold/controller/controller.stub
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php namespace {{studly_author}}\{{studly_plugin}}\Controllers;
<?php namespace {{ plugin_namespace }}\Controllers;

use BackendMenu;
use Backend\Classes\Controller;

/**
* {{title_name}} Back-end Controller
* {{ title_name }} Backend Controller
*/
class {{studly_name}} extends Controller
class {{ studly_name }} extends Controller
{
/**
* @var array Behaviors that are implemented by this controller.
Expand All @@ -20,6 +20,6 @@ class {{studly_name}} extends Controller
{
parent::__construct();

BackendMenu::setContext('{{studly_author}}.{{studly_plugin}}', '{{lower_plugin}}', '{{lower_name}}');
BackendMenu::setContext('{{ plugin_code }}', '{{ lower_plugin }}', '{{ lower_name }}');
}
}
14 changes: 7 additions & 7 deletions modules/backend/console/scaffold/controller/create.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php Block::put('breadcrumb') ?>
<ul>
<li><a href="<?= Backend::url('{{lower_author}}/{{lower_plugin}}/{{lower_name}}') ?>">{{title_name}}</a></li>
<li><a href="<?= Backend::url('{{ controller_url }}') ?>"><?= e(trans('{{ model_lang_key }}.label_plural')); ?></a></li>
<li><?= e($this->pageTitle) ?></li>
</ul>
<?php Block::endPut() ?>
Expand All @@ -19,21 +19,21 @@
type="submit"
data-request="onSave"
data-hotkey="ctrl+s, cmd+s"
data-load-indicator="Creating {{singular_name}}..."
data-load-indicator="<?= e(trans('backend::lang.form.creating_name', ['name' => trans('{{ model_lang_key }}.label')])); ?>"
class="btn btn-primary">
Create
<?= e(trans('backend::lang.form.create')); ?>
</button>
<button
type="button"
data-request="onSave"
data-request-data="close:1"
data-hotkey="ctrl+enter, cmd+enter"
data-load-indicator="Creating {{singular_name}}..."
data-load-indicator="<?= e(trans('backend::lang.form.creating_name', ['name' => trans('{{ model_lang_key }}.label')])); ?>"
class="btn btn-default">
Create and Close
<?= e(trans('backend::lang.form.create_and_close')); ?>
</button>
<span class="btn-text">
or <a href="<?= Backend::url('{{lower_author}}/{{lower_plugin}}/{{lower_name}}') ?>">Cancel</a>
or <a href="<?= Backend::url('{{ controller_url }}') ?>"><?= e(trans('backend::lang.form.cancel')); ?></a>
</span>
</div>
</div>
Expand All @@ -43,6 +43,6 @@
<?php else: ?>

<p class="flash-message static error"><?= e($this->fatalError) ?></p>
<p><a href="<?= Backend::url('{{lower_author}}/{{lower_plugin}}/{{lower_name}}') ?>" class="btn btn-default">Return to {{lower_title_name}} list</a></p>
<p><a href="<?= Backend::url('{{ controller_url }}') ?>" class="btn btn-default"><?= e(trans('backend::lang.form.return_to_list')); ?></a></p>

<?php endif ?>
1 change: 0 additions & 1 deletion modules/backend/console/scaffold/controller/index.stub
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

<?= $this->listRender() ?>
4 changes: 2 additions & 2 deletions modules/backend/console/scaffold/controller/preview.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php Block::put('breadcrumb') ?>
<ul>
<li><a href="<?= Backend::url('{{lower_author}}/{{lower_plugin}}/{{lower_name}}') ?>">{{title_name}}</a></li>
<li><a href="<?= Backend::url('{{ controller_url }}') ?>"><?= e(trans('{{ model_lang_key }}.label_plural')); ?></a></li>
<li><?= e($this->pageTitle) ?></li>
</ul>
<?php Block::endPut() ?>
Expand All @@ -14,6 +14,6 @@
<?php else: ?>

<p class="flash-message static error"><?= e($this->fatalError) ?></p>
<p><a href="<?= Backend::url('{{lower_author}}/{{lower_plugin}}/{{lower_name}}') ?>" class="btn btn-default">Return to {{lower_title_name}} list</a></p>
<p><a href="<?= Backend::url('{{ controller_url }}') ?>" class="btn btn-default"><?= e(trans('backend::lang.form.return_to_list')); ?></a></p>

<?php endif ?>
Loading