Skip to content

Commit ec7d6cc

Browse files
committed
feat: first working version of blade templates
1 parent 55dd552 commit ec7d6cc

File tree

74 files changed

+70
-4097
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+70
-4097
lines changed

config/laravel_generator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
'schema_files' => resource_path('model_schemas/'),
3939

40-
'templates_dir' => resource_path('infyom/infyom-generator-templates/'),
40+
'templates_dir' => resource_path('infyom/infyom-generator-stubs/'),
4141

4242
'seeder' => database_path('seeders/'),
4343

@@ -103,7 +103,7 @@
103103
|
104104
*/
105105

106-
'templates' => 'adminlte-templates',
106+
'stubs' => 'adminlte-stubs',
107107

108108
/*
109109
|--------------------------------------------------------------------------
@@ -140,7 +140,7 @@
140140

141141
'repository_pattern' => true,
142142

143-
'resources' => true,
143+
'resources' => false,
144144

145145
'factory' => false,
146146

src/Commands/Publish/GeneratorPublishCommand.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,6 @@ public function handle()
3434
}
3535
}
3636

37-
private function fillTemplate(string $templateData): string
38-
{
39-
$apiPrefix = config('laravel_generator.api_prefix', 'api');
40-
41-
$templateData = str_replace('$API_PREFIX$', $apiPrefix, $templateData);
42-
$appNamespace = $this->getLaravel()->getNamespace();
43-
$appNamespace = substr($appNamespace, 0, strlen($appNamespace) - 1);
44-
45-
return str_replace('$NAMESPACE_APP$', $appNamespace, $templateData);
46-
}
47-
4837
private function updateRouteServiceProvider()
4938
{
5039
$routeServiceProviderPath = app_path('Providers'.DIRECTORY_SEPARATOR.'RouteServiceProvider.php');
@@ -72,7 +61,7 @@ private function publishTestCases()
7261
$createdAtField = config('laravel_generator.timestamps.created_at', 'created_at');
7362
$updatedAtField = config('laravel_generator.timestamps.updated_at', 'updated_at');
7463

75-
$templateData = view('laravel-generator::api.tet.api_test_trait', [
64+
$templateData = view('laravel-generator::api.test.api_test_trait', [
7665
'timestamps' => "['$createdAtField', '$updatedAtField']",
7766
'namespacesTests' => $testsNameSpace,
7867
])->render();
@@ -101,39 +90,39 @@ private function publishTestCases()
10190

10291
private function publishBaseController()
10392
{
104-
$templateData = get_template('app_base_controller', 'laravel-generator');
105-
106-
$templateData = $this->fillTemplate($templateData);
107-
10893
$controllerPath = app_path('Http/Controllers/');
109-
11094
$fileName = 'AppBaseController.php';
11195

11296
if (file_exists($controllerPath.$fileName) && !$this->confirmOverwrite($fileName)) {
11397
return;
11498
}
11599

100+
$templateData = view('laravel-generator::stubs.app_base_controller', [
101+
'namespaceApp' => $this->getLaravel()->getNamespace(),
102+
'apiPrefix' => config('laravel_generator.api_prefix'),
103+
])->render();
104+
116105
g_filesystem()->createFile($controllerPath.$fileName, $templateData);
117106

118107
$this->info('AppBaseController created');
119108
}
120109

121110
private function publishBaseRepository()
122111
{
123-
$templateData = get_template('base_repository', 'laravel-generator');
124-
125-
$templateData = $this->fillTemplate($templateData);
126-
127112
$repositoryPath = app_path('Repositories/');
128113

129-
g_filesystem()->createDirectoryIfNotExist($repositoryPath);
130-
131114
$fileName = 'BaseRepository.php';
132115

133116
if (file_exists($repositoryPath.$fileName) && !$this->confirmOverwrite($fileName)) {
134117
return;
135118
}
136119

120+
g_filesystem()->createDirectoryIfNotExist($repositoryPath);
121+
122+
$templateData = view('laravel-generator::stubs.base_repository', [
123+
'namespaceApp' => $this->getLaravel()->getNamespace(),
124+
])->render();
125+
137126
g_filesystem()->createFile($repositoryPath.$fileName, $templateData);
138127

139128
$this->info('BaseRepository created');

src/Commands/Publish/PublishBaseCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace InfyOm\Generator\Commands\Publish;
44

5+
use Illuminate\Console\Command;
56
use Illuminate\Support\Facades\File;
6-
use InfyOm\Generator\Commands\BaseCommand;
77

8-
class PublishBaseCommand extends BaseCommand
8+
class PublishBaseCommand extends Command
99
{
1010
public function handle()
1111
{

src/Commands/Publish/PublishTablesCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function handle()
4343
public function publishLivewireTableViews()
4444
{
4545
$viewsPath = config('laravel_generator.path.views', resource_path('views/'));
46-
$templateType = config('infyom.laravel_generator.templates', 'adminlte-templates');
46+
$templateType = config('infyom.laravel_generator.stubs', 'adminlte-stubs');
4747

4848
$files = [
4949
'views/table/livewire/actions' => 'common/livewire-tables/actions.blade.php',

src/Commands/Publish/PublishTemplatesCommand.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ class PublishTemplatesCommand extends PublishBaseCommand
99
*
1010
* @var string
1111
*/
12-
protected $name = 'infyom.publish:templates';
12+
protected $name = 'infyom.publish:stubs';
1313

1414
/**
1515
* The console command description.
1616
*
1717
* @var string
1818
*/
19-
protected $description = 'Publishes api generator templates.';
19+
protected $description = 'Publishes api generator stubs.';
2020

2121
private string $templatesDir;
2222

2323
public function handle()
2424
{
2525
$this->templatesDir = config(
2626
'laravel_generator.path.templates_dir',
27-
resource_path('infyom/infyom-generator-templates/')
27+
resource_path('infyom/infyom-generator-stubs/')
2828
);
2929

3030
if ($this->publishGeneratorTemplates()) {
@@ -35,23 +35,23 @@ public function handle()
3535

3636
public function publishGeneratorTemplates(): bool
3737
{
38-
$templatesPath = __DIR__.'/../../../templates';
38+
$templatesPath = __DIR__ . '/../../../stubs';
3939

40-
return $this->publishDirectory($templatesPath, $this->templatesDir, 'infyom-generator-templates');
40+
return $this->publishDirectory($templatesPath, $this->templatesDir, 'infyom-generator-stubs');
4141
}
4242

4343
public function publishScaffoldTemplates(): bool
4444
{
45-
$templateType = config('laravel_generator.templates', 'adminlte-templates');
45+
$templateType = config('laravel_generator.stubs', 'adminlte-stubs');
4646

47-
$templatesPath = get_templates_package_path($templateType).'/templates/scaffold';
47+
$templatesPath = get_templates_package_path($templateType).'/stubs/scaffold';
4848

49-
return $this->publishDirectory($templatesPath, $this->templatesDir.'scaffold', 'infyom-generator-templates/scaffold', true);
49+
return $this->publishDirectory($templatesPath, $this->templatesDir.'scaffold', 'infyom-generator-stubs/scaffold', true);
5050
}
5151

5252
public function publishSwaggerTemplates(): bool
5353
{
54-
$templatesPath = base_path('vendor/infyomlabs/swagger-generator/templates');
54+
$templatesPath = base_path('vendor/infyomlabs/swagger-generator/stubs');
5555

5656
return $this->publishDirectory($templatesPath, $this->templatesDir, 'swagger-generator', true);
5757
}

src/Commands/Publish/PublishUserCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function handle()
3434
private function copyViews()
3535
{
3636
$viewsPath = config('laravel_generator.path.views', resource_path('views/'));
37-
$templateType = config('laravel_generator.templates', 'adminlte-templates');
37+
$templateType = config('laravel_generator.stubs', 'adminlte-stubs');
3838

3939
$this->createDirectories($viewsPath.'users');
4040

@@ -83,7 +83,7 @@ private function updateRoutes()
8383
private function updateMenu()
8484
{
8585
$viewsPath = config('laravel_generator.path.views', resource_path('views/'));
86-
$templateType = config('laravel_generator.templates', 'adminlte-templates');
86+
$templateType = config('laravel_generator.stubs', 'adminlte-stubs');
8787
$path = $viewsPath.'layouts/menu.blade.php';
8888
$menuContents = g_filesystem()->getFile($path);
8989
$sourceFile = g_filesystem()->getFile(get_template_file_path('scaffold/users/menu', $templateType));

src/Generators/Scaffold/ControllerGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private function generateDataTableColumns(): array
108108
}
109109

110110
$dataTableColumns[] = trim(view(
111-
$this->templateType.'::templates.scaffold.table.datatable.column',
111+
$this->templateType.'::stubs.scaffold.table.datatable.column',
112112
$field->variables()
113113
)->render());
114114
}

src/Generators/Scaffold/MenuGenerator.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,40 @@ class MenuGenerator extends BaseGenerator
99
{
1010
private string $templateType;
1111

12-
private string $menuContents;
13-
14-
private string $menuTemplate;
15-
1612
public function __construct()
1713
{
1814
parent::__construct();
1915

2016
$this->path = config('laravel_generator.path.menu_file', resource_path('views/layouts/menu.blade.php'));
2117
$this->templateType = config('laravel_generator.templates', 'adminlte-templates');
22-
23-
$this->menuContents = g_filesystem()->getFile($this->path);
24-
25-
$templateName = 'menu_template';
26-
27-
if ($this->config->isLocalizedTemplates()) {
28-
$templateName .= '_locale';
29-
}
30-
31-
$this->menuTemplate = get_template('scaffold.layouts.'.$templateName, $this->templateType);
32-
33-
$this->menuTemplate = fill_template($this->config->dynamicVars, $this->menuTemplate);
3418
}
3519

3620
public function generate()
3721
{
38-
$this->menuContents .= $this->menuTemplate.infy_nl();
39-
$existingMenuContents = g_filesystem()->getFile($this->path);
40-
// adminlte uses <p> tab and coreui+stisla uses <span> tag for menu
41-
if (Str::contains($existingMenuContents, '<p>'.$this->config->modelNames->humanPlural.'</p>') or
42-
Str::contains($existingMenuContents, '<span>'.$this->config->modelNames->humanPlural.'</span>')) {
22+
$menuContents = g_filesystem()->getFile($this->path);
23+
24+
$menu = view($this->templateType.'::templates.layouts.menu_template')->render();
25+
26+
if (Str::contains($menuContents, $menu)) {
4327
$this->config->commandInfo(PHP_EOL.'Menu '.$this->config->modelNames->humanPlural.' already exists, Skipping Adjustment.');
4428

4529
return;
4630
}
4731

48-
g_filesystem()->createFile($this->path, $this->menuContents);
32+
$menuContents .= infy_nl().$menu;
33+
34+
g_filesystem()->createFile($this->path, $menuContents);
4935
$this->config->commandComment(PHP_EOL.$this->config->modelNames->dashedPlural.' menu added.');
5036
}
5137

5238
public function rollback()
5339
{
54-
if (Str::contains($this->menuContents, $this->menuTemplate)) {
55-
g_filesystem()->createFile($this->path, str_replace($this->menuTemplate, '', $this->menuContents));
40+
$menuContents = g_filesystem()->getFile($this->path);
41+
42+
$menu = view($this->templateType.'::templates.layouts.menu_template')->render();
43+
44+
if (Str::contains($menuContents, $menu)) {
45+
g_filesystem()->createFile($this->path, str_replace($menuContents, '', $menu));
5646
$this->config->commandComment('menu deleted');
5747
}
5848
}

src/Generators/Scaffold/RoutesGenerator.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,40 @@
77

88
class RoutesGenerator extends BaseGenerator
99
{
10-
private string $routeContents;
11-
12-
private string $routesTemplate;
13-
1410
public function __construct()
1511
{
1612
parent::__construct();
1713

1814
$this->path = $this->config->paths->routes;
19-
$this->routeContents = g_filesystem()->getFile($this->path);
20-
if (!empty($this->config->prefixes->route)) {
21-
$this->routesTemplate = get_template('scaffold.routes.prefix_routes', 'laravel-generator');
22-
} else {
23-
$this->routesTemplate = get_template('scaffold.routes.routes', 'laravel-generator');
24-
}
25-
$this->routesTemplate = fill_template($this->config->dynamicVars, $this->routesTemplate);
2615
}
2716

2817
public function generate()
2918
{
30-
$this->routeContents .= "\n\n".$this->routesTemplate;
31-
$existingRouteContents = g_filesystem()->getFile($this->path);
32-
if (Str::contains($existingRouteContents, "Route::resource('".$this->config->modelNames->dashedPlural."',")) {
19+
$routeContents = g_filesystem()->getFile($this->path);
20+
21+
$routes = view('laravel-generator::templates.scaffold.routes')->render();
22+
23+
if (Str::contains($routeContents, $routes)) {
3324
$this->config->commandInfo(PHP_EOL.'Route '.$this->config->modelNames->dashedPlural.' already exists, Skipping Adjustment.');
3425

3526
return;
3627
}
3728

38-
g_filesystem()->createFile($this->path, $this->routeContents);
29+
$routeContents .= infy_nl().$routes;
30+
31+
g_filesystem()->createFile($this->path, $routeContents);
3932
$this->config->commandComment(PHP_EOL.$this->config->modelNames->dashedPlural.' routes added.');
4033
}
4134

4235
public function rollback()
4336
{
44-
if (Str::contains($this->routeContents, $this->routesTemplate)) {
45-
$this->routeContents = str_replace($this->routesTemplate, '', $this->routeContents);
46-
g_filesystem()->createFile($this->path, $this->routeContents);
37+
$routeContents = g_filesystem()->getFile($this->path);
38+
39+
$routes = view('laravel-generator::templates.scaffold.routes')->render();
40+
41+
if (Str::contains($routeContents, $routes)) {
42+
$routeContents = str_replace($routeContents, '', $routes);
43+
g_filesystem()->createFile($this->path, $routeContents);
4744
$this->config->commandComment('scaffold routes deleted');
4845
}
4946
}

src/helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function get_template_file_path(string $templateName, string $templateType): str
5656

5757
$templatesPath = config(
5858
'laravel_generator.path.templates_dir',
59-
resource_path('infyom/infyom-generator-templates/')
59+
resource_path('infyom/infyom-generator-stubs/')
6060
);
6161

6262
$path = $templatesPath.$templateName.'.stub';
@@ -65,7 +65,7 @@ function get_template_file_path(string $templateName, string $templateType): str
6565
return $path;
6666
}
6767

68-
return get_templates_package_path($templateType).'/templates/'.$templateName.'.stub';
68+
return get_templates_package_path($templateType).'/stubs/'.$templateName.'.stub';
6969
}
7070
}
7171

0 commit comments

Comments
 (0)