Skip to content

Commit

Permalink
#50 first commit for basic data table
Browse files Browse the repository at this point in the history
  • Loading branch information
mitulgolakiya committed Apr 2, 2016
1 parent 1fdf182 commit 798061e
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 27 deletions.
10 changes: 8 additions & 2 deletions config/laravel_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

'model' => app_path('Models/'),

'datatables' => app_path('DataTables/'),

'repository' => app_path('Repositories/'),

'routes' => app_path('Http/routes.php'),
Expand Down Expand Up @@ -53,6 +55,8 @@

'model' => 'App\Models',

'datatables' => 'App\DataTables',

'repository' => 'App\Repositories',

'controller' => 'App\Http\Controllers',
Expand Down Expand Up @@ -116,9 +120,11 @@

'add_on' => [

'swagger' => true,
'swagger' => true,

'tests' => true,

'tests' => true,
'datatables' => true,
],

/*
Expand Down
8 changes: 8 additions & 0 deletions src/Common/GeneratorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GeneratorConfig
public $nsApp;
public $nsRepository;
public $nsModel;
public $nsDataTables;
public $nsModelExtend;

public $nsApiController;
Expand All @@ -22,6 +23,7 @@ class GeneratorConfig
/* Path variables */
public $pathRepository;
public $pathModel;
public $pathDataTables;

public $pathApiController;
public $pathApiRequest;
Expand Down Expand Up @@ -71,6 +73,7 @@ public function loadNamespaces(CommandData &$commandData)
$this->nsApp = $commandData->commandObj->getLaravel()->getNamespace();
$this->nsRepository = config('infyom.laravel_generator.namespace.repository', 'App\Repositories').$prefix;
$this->nsModel = config('infyom.laravel_generator.namespace.model', 'App\Models').$prefix;
$this->nsDataTables = config('infyom.laravel_generator.namespace.datatables', 'App\DataTables').$prefix;
$this->nsModelExtend = config(
'infyom.laravel_generator.model_extend_class',
'Illuminate\Database\Eloquent\Model'
Expand Down Expand Up @@ -104,6 +107,8 @@ public function loadPaths()

$this->pathModel = config('infyom.laravel_generator.path.model', app_path('Models/')).$prefixTitle;

$this->pathDataTables = config('infyom.laravel_generator.path.datatables', app_path('DataTables/')).$prefixTitle;

$this->pathApiController = config(
'infyom.laravel_generator.path.api_controller',
app_path('Http/Controllers/API/')
Expand Down Expand Up @@ -140,6 +145,8 @@ public function loadDynamicVariables(CommandData &$commandData)
$commandData->addDynamicVariable('$NAMESPACE_APP$', $this->nsApp);
$commandData->addDynamicVariable('$NAMESPACE_REPOSITORY$', $this->nsRepository);
$commandData->addDynamicVariable('$NAMESPACE_MODEL$', $this->nsModel);
$commandData->addDynamicVariable('$NAMESPACE_MODEL$', $this->nsModel);
$commandData->addDynamicVariable('$NAMESPACE_DATATABLES$', $this->nsDataTables);
$commandData->addDynamicVariable('$NAMESPACE_MODEL_EXTEND$', $this->nsModelExtend);

$commandData->addDynamicVariable('$NAMESPACE_API_CONTROLLER$', $this->nsApiController);
Expand Down Expand Up @@ -245,5 +252,6 @@ public function prepareAddOns()
{
$this->addOns['swagger'] = config('infyom.laravel_generator.add_on.swagger', false);
$this->addOns['tests'] = config('infyom.laravel_generator.add_on.tests', false);
$this->addOns['datatables'] = config('infyom.laravel_generator.add_on.datatables', false);
}
}
38 changes: 30 additions & 8 deletions src/Generators/Scaffold/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,45 @@ public function __construct(CommandData $commandData)

public function generate()
{
$templateData = TemplateUtil::getTemplate('scaffold.controller.controller', 'laravel-generator');
if ($this->commandData->getAddOn('datatables')) {
$templateData = TemplateUtil::getTemplate('scaffold.controller.datatable_controller', 'laravel-generator');

$templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);

$paginate = $this->commandData->getOption('paginate');

if ($paginate) {
$templateData = str_replace('$RENDER_TYPE$', 'paginate('.$paginate.')', $templateData);
$this->generateDataTable();
} else {
$templateData = str_replace('$RENDER_TYPE$', 'all()', $templateData);
$templateData = TemplateUtil::getTemplate('scaffold.controller.controller', 'laravel-generator');

$paginate = $this->commandData->getOption('paginate');

if ($paginate) {
$templateData = str_replace('$RENDER_TYPE$', 'paginate('.$paginate.')', $templateData);
} else {
$templateData = str_replace('$RENDER_TYPE$', 'all()', $templateData);
}
}

$templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);

$fileName = $this->commandData->modelName.'Controller.php';

FileUtil::createFile($this->path, $fileName, $templateData);

$this->commandData->commandComment("\nController created: ");
$this->commandData->commandInfo($fileName);
}

private function generateDataTable()
{
$templateData = TemplateUtil::getTemplate('scaffold.data_table', 'laravel-generator');

$templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);

$path = $this->commandData->config->pathDataTables;

$fileName = $this->commandData->modelName.'DataTable.php';

FileUtil::createFile($path, $fileName, $templateData);

$this->commandData->commandComment("\n$fileName created: ");
$this->commandData->commandInfo($fileName);
}
}
82 changes: 65 additions & 17 deletions src/Generators/Scaffold/ViewGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,22 @@ public function generate()

private function generateTable()
{
$templateData = TemplateUtil::getTemplate('scaffold.views.table', $this->templateType);
$headerFieldTemplate = TemplateUtil::getTemplate('scaffold.views.table_header', $this->templateType);
$cellFieldTemplate = TemplateUtil::getTemplate('scaffold.views.table_cell', $this->templateType);
$fileName = 'table.blade.php';

$templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);
if ($this->commandData->getAddOn('datatables')) {
$templateData = $this->generateDataTableBody();
} else {
$templateData = $this->generateBladeTableBody();
}

$fileName = 'table.blade.php';
FileUtil::createFile($this->path, $fileName, $templateData);

$this->commandData->commandInfo('table.blade.php created');
}

private function generateTableHeaderFields()
{
$headerFieldTemplate = TemplateUtil::getTemplate('scaffold.views.table_header', $this->templateType);

$headerFields = [];

Expand All @@ -70,9 +79,18 @@ private function generateTable()
);
}

$headerFields = implode(PHP_EOL.str_repeat(' ', 8), $headerFields);
return implode(PHP_EOL.str_repeat(' ', 8), $headerFields);
}

$templateData = str_replace('$FIELD_HEADERS$', $headerFields, $templateData);
private function generateBladeTableBody()
{
$templateData = TemplateUtil::getTemplate('scaffold.views.blade_table_body', $this->templateType);

$templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);

$templateData = str_replace('$FIELD_HEADERS$', $this->generateTableHeaderFields(), $templateData);

$cellFieldTemplate = TemplateUtil::getTemplate('scaffold.views.table_cell', $this->templateType);

$tableBodyFields = [];

Expand All @@ -91,11 +109,37 @@ private function generateTable()

$tableBodyFields = implode(PHP_EOL.str_repeat(' ', 12), $tableBodyFields);

$templateData = str_replace('$FIELD_BODY$', $tableBodyFields, $templateData);
return str_replace('$FIELD_BODY$', $tableBodyFields, $templateData);
}

FileUtil::createFile($this->path, $fileName, $templateData);
private function generateDataTableBody()
{
$templateData = TemplateUtil::getTemplate('scaffold.views.datatable_body', $this->templateType);

$this->commandData->commandInfo('table.blade.php created');
$templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);

$templateData = str_replace('$FIELD_HEADERS$', $this->generateTableHeaderFields(), $templateData);

$cellFieldTemplate = TemplateUtil::getTemplate('scaffold.views.table_cell', $this->templateType);

$tableBodyFields = [];

foreach ($this->commandData->inputFields as $field) {
if (!$field['inIndex']) {
continue;
}

$tableBodyFields[] = TemplateUtil::fillTemplateWithFieldData(
$this->commandData->dynamicVars,
$this->commandData->fieldNamesMapping,
$cellFieldTemplate,
$field
);
}

$tableBodyFields = implode(PHP_EOL.str_repeat(' ', 12), $tableBodyFields);

return str_replace('$FIELD_BODY$', $tableBodyFields, $templateData);
}

private function generateIndex()
Expand All @@ -104,16 +148,20 @@ private function generateIndex()

$templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);

$paginate = $this->commandData->getOption('paginate');
if ($this->commandData->getOption('datatables')) {
$templateData = str_replace('$PAGINATE$', '', $templateData);
} else {
$paginate = $this->commandData->getOption('paginate');

if ($paginate) {
$paginateTemplate = TemplateUtil::getTemplate('scaffold.views.paginate', $this->templateType);
if ($paginate) {
$paginateTemplate = TemplateUtil::getTemplate('scaffold.views.paginate', $this->templateType);

$paginateTemplate = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $paginateTemplate);
$paginateTemplate = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $paginateTemplate);

$templateData = str_replace('$PAGINATE$', $paginateTemplate, $templateData);
} else {
$templateData = str_replace('$PAGINATE$', '', $templateData);
$templateData = str_replace('$PAGINATE$', $paginateTemplate, $templateData);
} else {
$templateData = str_replace('$PAGINATE$', '', $templateData);
}
}

$fileName = 'index.blade.php';
Expand Down
Loading

0 comments on commit 798061e

Please sign in to comment.