Skip to content

Commit d23da38

Browse files
committed
feat: working version with prefix
1 parent c2481b0 commit d23da38

28 files changed

+355
-997
lines changed

config/laravel_generator.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,7 @@
166166

167167
'namespace' => '', // e.g. Admin or Admin\Shipping or Admin\Shipping\Logistics
168168

169-
'path' => '', // e.g. admin or admin/shipping or admin/shipping/logistics
170-
171169
'view' => '', // e.g. admin or admin/shipping or admin/shipping/logistics
172-
173-
'public' => '', // e.g. admin or admin/shipping or admin/shipping/logistics
174170
],
175171

176172
/*

src/Common/FileSystem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function createFile(string $file, string $contents): bool|int
1818
$path = dirname($file);
1919

2020
if (!empty($path) && !file_exists($path)) {
21-
return mkdir($path, 0755, true);
21+
mkdir($path, 0755, true);
2222
}
2323

2424
return file_put_contents($file, $contents);

src/Common/GeneratorConfig.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,14 @@ public function loadPrefixes()
7979

8080
$prefixes->route = config('laravel_generator.prefixes.route', '');
8181
$prefixes->namespace = config('laravel_generator.prefixes.namespace', '');
82-
$prefixes->path = config('laravel_generator.prefixes.path', '');
8382
$prefixes->view = config('laravel_generator.prefixes.view', '');
84-
$prefixes->public = config('laravel_generator.prefixes.public', '');
8583

8684
if ($this->getOption('prefix')) {
8785
$multiplePrefixes = explode('/', $this->getOption('prefix'));
8886

8987
$prefixes->mergeRoutePrefix($multiplePrefixes);
9088
$prefixes->mergeNamespacePrefix($multiplePrefixes);
91-
$prefixes->mergePathPrefix($multiplePrefixes);
9289
$prefixes->mergeViewPrefix($multiplePrefixes);
93-
$prefixes->mergePublicPrefix($multiplePrefixes);
9490
}
9591

9692
$this->prefixes = $prefixes;
@@ -100,20 +96,28 @@ public function loadPaths()
10096
{
10197
$paths = new GeneratorPaths();
10298

103-
$pathPrefix = $this->prefixes->path;
99+
$namespacePrefix = $this->prefixes->namespace;
104100
$viewPrefix = $this->prefixes->view;
105101

102+
if (!empty($namespacePrefix)) {
103+
$namespacePrefix .= '/';
104+
}
105+
106+
if (!empty($viewPrefix)) {
107+
$viewPrefix .= '/';
108+
}
109+
106110
$paths->repository = config(
107111
'laravel_generator.path.repository',
108112
app_path('Repositories/')
109-
).$pathPrefix;
113+
).$namespacePrefix;
110114

111-
$paths->model = config('laravel_generator.path.model', app_path('Models/')).$pathPrefix;
115+
$paths->model = config('laravel_generator.path.model', app_path('Models/')).$namespacePrefix;
112116

113117
$paths->dataTables = config(
114118
'laravel_generator.path.datatables',
115119
app_path('DataTables/')
116-
).$pathPrefix;
120+
).$namespacePrefix;
117121

118122
$paths->livewireTables = config(
119123
'laravel_generator.path.livewire_tables',
@@ -123,17 +127,17 @@ public function loadPaths()
123127
$paths->apiController = config(
124128
'laravel_generator.path.api_controller',
125129
app_path('Http/Controllers/API/')
126-
).$pathPrefix;
130+
).$namespacePrefix;
127131

128132
$paths->apiResource = config(
129133
'laravel_generator.path.api_resource',
130134
app_path('Http/Resources/')
131-
).$pathPrefix;
135+
).$namespacePrefix;
132136

133137
$paths->apiRequest = config(
134138
'laravel_generator.path.api_request',
135139
app_path('Http/Requests/API/')
136-
).$pathPrefix;
140+
).$namespacePrefix;
137141

138142
$paths->apiRoutes = config(
139143
'laravel_generator.path.api_routes',
@@ -145,9 +149,9 @@ public function loadPaths()
145149
$paths->controller = config(
146150
'laravel_generator.path.controller',
147151
app_path('Http/Controllers/')
148-
).$pathPrefix;
152+
).$namespacePrefix;
149153

150-
$paths->request = config('laravel_generator.path.request', app_path('Http/Requests/')).$pathPrefix;
154+
$paths->request = config('laravel_generator.path.request', app_path('Http/Requests/')).$namespacePrefix;
151155

152156
$paths->routes = config('laravel_generator.path.routes', base_path('routes/web.php'));
153157
$paths->factory = config('laravel_generator.path.factory', database_path('factories/'));
@@ -157,30 +161,24 @@ public function loadPaths()
157161
resource_path('views/')
158162
).$viewPrefix.$this->modelNames->snakePlural.'/';
159163

160-
$paths->assets = config(
161-
'laravel_generator.path.assets',
162-
resource_path('assets/')
163-
);
164-
165164
$paths->seeder = config('laravel_generator.path.seeder', database_path('seeders/'));
166165
$paths->databaseSeeder = config('laravel_generator.path.database_seeder', database_path('seeders/DatabaseSeeder.php'));
167166
$paths->viewProvider = config(
168167
'laravel_generator.path.view_provider',
169168
app_path('Providers/ViewServiceProvider.php')
170169
);
171170

172-
$paths->modelJsPath = config(
173-
'laravel_generator.path.modelsJs',
174-
resource_path('assets/js/models/')
175-
);
176-
177171
$this->paths = $paths;
178172
}
179173

180174
public function loadNamespaces()
181175
{
182176
$prefix = $this->prefixes->namespace;
183177

178+
if (!empty($prefix)) {
179+
$prefix = '\\'.$prefix;
180+
}
181+
184182
$namespaces = new GeneratorNamespaces();
185183

186184
$namespaces->app = $this->command->getLaravel()->getNamespace();

src/DTOs/GeneratorPaths.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@ class GeneratorPaths
2323
public string $request;
2424
public string $routes;
2525
public string $views;
26-
public string $assets;
27-
public string $modelJsPath;
2826
}

src/DTOs/GeneratorPrefixes.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,32 @@
77
class GeneratorPrefixes
88
{
99
public string $route = '';
10-
public string $path = '';
1110
public string $view = '';
12-
public string $public = '';
1311
public string $namespace = '';
1412

13+
public function getRoutePrefixWith($append)
14+
{
15+
if ($this->route) {
16+
return $this->route.$append;
17+
}
18+
19+
return '';
20+
}
21+
22+
public function getViewPrefixWith($append)
23+
{
24+
if ($this->view) {
25+
return $this->view.$append;
26+
}
27+
28+
return '';
29+
}
30+
31+
public function getViewPrefixForInclude()
32+
{
33+
return Str::replace('/', '.', $this->view).'.';
34+
}
35+
1536
public function mergeRoutePrefix(array $prefixes)
1637
{
1738
foreach ($prefixes as $prefix) {
@@ -38,31 +59,16 @@ public function mergeNamespacePrefix(array $prefixes)
3859
$this->namespace = ltrim($this->namespace, '\\');
3960
}
4061

41-
private function mergeForwardSlashPrefix(string $initialString, array $prefixes): string
62+
public function mergeViewPrefix(array $prefixes)
4263
{
4364
foreach ($prefixes as $prefix) {
4465
if (empty($prefix)) {
4566
continue;
4667
}
4768

48-
$initialString .= '/'.Str::title($prefix);
69+
$this->view .= '/'.Str::snake($prefix);
4970
}
5071

51-
return ltrim($initialString, '/');
52-
}
53-
54-
public function mergePathPrefix(array $prefixes)
55-
{
56-
$this->path = $this->mergeForwardSlashPrefix($this->path, $prefixes);
57-
}
58-
59-
public function mergeViewPrefix(array $prefixes)
60-
{
61-
$this->view = $this->mergeForwardSlashPrefix($this->view, $prefixes);
62-
}
63-
64-
public function mergePublicPrefix(array $prefixes)
65-
{
66-
$this->public = $this->mergeForwardSlashPrefix($this->public, $prefixes);
72+
$this->view = ltrim($this->view, '/');
6773
}
6874
}

src/Generators/Scaffold/ControllerGenerator.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,36 @@ public function generate()
2727
switch ($this->config->tableType) {
2828
case 'blade':
2929
if ($this->config->options->repositoryPattern) {
30-
$viewName = 'repository.controller';
30+
$indexMethodView = 'index_method_repository';
3131
} else {
32-
$viewName = 'model.controller';
32+
$indexMethodView = 'index_method';
3333
}
34-
3534
$variables['renderType'] = 'paginate(10)';
3635
break;
3736

3837
case 'datatables':
39-
if ($this->config->options->repositoryPattern) {
40-
$viewName = 'repository.datatable.controller';
41-
} else {
42-
$viewName = 'model.datatable.controller';
43-
}
44-
38+
$indexMethodView = 'index_method_datatable';
4539
$this->generateDataTable();
4640
break;
4741

4842
case 'livewire':
49-
if ($this->config->options->repositoryPattern) {
50-
$viewName = 'repository.livewire.controller';
51-
} else {
52-
$viewName = 'model.livewire.controller';
53-
}
54-
43+
$indexMethodView = 'index_method_livewire';
5544
$this->generateLivewireTable();
5645
break;
5746

5847
default:
5948
throw new Exception('Invalid Table Type');
6049
}
6150

51+
if ($this->config->options->repositoryPattern) {
52+
$viewName = 'controller_repository';
53+
} else {
54+
$viewName = 'controller';
55+
}
56+
57+
$variables['indexMethod'] = view('laravel-generator::scaffold.controller.'.$indexMethodView, $variables)
58+
->render();
59+
6260
$templateData = view('laravel-generator::scaffold.controller.'.$viewName, $variables)->render();
6361

6462
g_filesystem()->createFile($this->path.$this->fileName, $templateData);

src/Generators/Scaffold/ViewGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private function generateIndex()
162162
switch ($this->config->tableType) {
163163
case 'datatables':
164164
case 'blade':
165-
$tableReplaceString = "@include('".$this->config->modelNames->snakePlural.".table')";
165+
$tableReplaceString = "@include('".$this->config->prefixes->getViewPrefixForInclude().$this->config->modelNames->snakePlural.".table')";
166166
break;
167167

168168
case 'livewire':

src/helpers.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,20 @@ function model_name_from_table_name(string $tableName): string
148148
return Str::ucfirst(Str::camel(Str::singular($tableName)));
149149
}
150150
}
151+
152+
function createResourceRouteNames($name, $isScaffold = false) {
153+
$result = [
154+
"'index' => '$name.index'",
155+
"'store' => '$name.store'",
156+
"'show' => '$name.show'",
157+
"'update' => '$name.update'",
158+
"'destroy' => '$name.destroy'",
159+
];
160+
161+
if ($isScaffold) {
162+
$result[] = "'create' => '$name.create'";
163+
$result[] = "'edit' => '$name.edit'";
164+
}
165+
166+
return $result;
167+
}

views/api/routes.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Route::resource('{{ $config->modelNames->dashedPlural }}', {{ $config->namespaces->apiController }}\{{ $config->modelNames->name }}APIController::class);
1+
Route::resource('{{ $config->prefixes->getRoutePrefixWith('/') }}{{ $config->modelNames->dashedPlural }}', {{ $config->namespaces->apiController }}\{{ $config->modelNames->name }}APIController::class){!! infy_nl_tab() !!}->except(['create', 'edit'])@if(!$config->prefixes->route);@endif
2+
@if($config->prefixes->route){!! infy_nl_tab().'->names(['.infy_nl_tab(1,2).implode(','.infy_nl_tab(1, 2), createResourceRouteNames($config->prefixes->getRoutePrefixWith('.').$config->modelNames->camelPlural)).infy_nl_tab().']);' !!}@endif

views/migration.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Database\Schema\Blueprint;
77
use Illuminate\Support\Facades\Schema;
88

9-
class Create{{ $config->modelNames->name }}Table extends Migration
9+
return new class extends Migration
1010
{
1111
/**
1212
* Run the migrations.
@@ -29,4 +29,4 @@ public function down()
2929
{
3030
Schema::drop('{{ $config->tableName }}');
3131
}
32-
}
32+
};

0 commit comments

Comments
 (0)