Skip to content

Commit c6f2a2c

Browse files
authored
Merge pull request #14 from Swatinfo/development
Resolved Code Errors and added missing functions
2 parents f1217b1 + 36b9d0d commit c6f2a2c

24 files changed

+891
-439
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ use SwatTech\Crud\Generators\ModelGenerator as BaseModelGenerator;
159159

160160
class CustomModelGenerator extends BaseModelGenerator
161161
{
162-
protected function getStub()
162+
protected function getStub(string $view = "")
163163
{
164164
return resource_path('stubs/custom-model.stub');
165165
}

code_generation_based_on_priority.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ src/Generators/ViewGenerator.php**
890890
- Implement constructor with StringHelper and RelationshipAnalyzer
891891
- Add generate(string $table, array $options = []) method
892892
- Include getViewName(string $table, string $view) method
893-
- Add getPath(string $view) method
893+
- Add getPath(string $path = "") method
894894
- Include getStub(string $view) method to get view stubs
895895
- Add buildView(string $table, string $view, array $options) method
896896
- Include generateIndexView(string $table, array $columns, array $options) method

docs/advanced-usage.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use SwatTech\Crud\Generators\ModelGenerator as BaseModelGenerator;
6565

6666
class CustomModelGenerator extends BaseModelGenerator
6767
{
68-
protected function getStub()
68+
protected function getStub(string $view)
6969
{
7070
return resource_path('stubs/custom-model.stub');
7171
}

docs/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ use SwatTech\Crud\Generators\ModelGenerator as BaseModelGenerator;
386386

387387
class CustomModelGenerator extends BaseModelGenerator
388388
{
389-
protected function getStub()
389+
protected function getStub(string $view)
390390
{
391391
return resource_path('stubs/custom-model.stub');
392392
}

docs/extending.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CustomModelGenerator extends BaseModelGenerator
3636
*
3737
* @return string
3838
*/
39-
protected function getStub()
39+
protected function getStub(string $view)
4040
{
4141
return resource_path('stubs/custom-model.stub');
4242
}

src/Analyzers/RelationshipAnalyzer.php

+41
Original file line numberDiff line numberDiff line change
@@ -857,4 +857,45 @@ public function setConnection(string $connection)
857857
$this->connection = $connection;
858858
return $this;
859859
}
860+
/**
861+
* Get the database schema instance.
862+
*
863+
* @return \Illuminate\Database\Schema\Builder
864+
*/
865+
public function getSchema()
866+
{
867+
return $this->schemaHelper->getSchema($this->connection);
868+
}
869+
870+
/**
871+
* Get the database name.
872+
*
873+
* @return string
874+
*/
875+
public function getDatabaseName(): string
876+
{
877+
return $this->schemaHelper->getDatabaseName($this->connection);
878+
}
879+
880+
/**
881+
* Get the supported relationship types.
882+
*
883+
* @return array
884+
*/
885+
public function supportedRelationships() : array
886+
{
887+
return [
888+
'hasMany',
889+
'hasOne',
890+
'belongsTo',
891+
'belongsToMany',
892+
'morphTo',
893+
'morphMany',
894+
'morphOne',
895+
'hasManyThrough',
896+
'hasOneThrough',
897+
'morphToMany',
898+
'morphedByMany'
899+
];
900+
}
860901
}

src/Contracts/GeneratorInterface.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function setOptions(array $options): self;
4545
*
4646
* @return string The stub template content
4747
*/
48-
public function getStub(): string;
48+
public function getStub(string $filename): string;
4949

5050
/**
5151
* Get the namespace for generated classes.
@@ -65,7 +65,7 @@ public function getNamespace(): string;
6565
*
6666
* @return string The file path for generated files
6767
*/
68-
public function getPath(): string;
68+
public function getPath(string $filename): string;
6969

7070
/**
7171
* Get the class name for the generated file.
@@ -76,7 +76,7 @@ public function getPath(): string;
7676
* @param string $table The database table name
7777
* @return string The generated class name
7878
*/
79-
public function getClassName(string $table): string;
79+
public function getClassName(string $table, string $action): string;
8080

8181
/**
8282
* Get a list of all generated file paths.
@@ -97,4 +97,4 @@ public function getGeneratedFiles(): array;
9797
* @return bool True if the generator supports customization, false otherwise
9898
*/
9999
public function supportsCustomization(): bool;
100-
}
100+
}

src/Generators/ControllerGenerator.php

+47-15
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function generate(string $table, array $options = []): array
102102
* @param string $table The database table name
103103
* @return string The controller class name
104104
*/
105-
public function getClassName(string $table): string
105+
public function getClassName(string $table, string $action = ""): string
106106
{
107107
$modelName = Str::studly(Str::singular($table));
108108
return $modelName . 'Controller';
@@ -123,7 +123,7 @@ public function getNamespace(): string
123123
*
124124
* @return string The controller file path
125125
*/
126-
public function getPath(): string
126+
public function getPath(string $path = ""): string
127127
{
128128
return base_path(Config::get('crud.paths.controllers', 'app/Http/Controllers'));
129129
}
@@ -185,7 +185,7 @@ protected function generateApiController(string $table, array $options): string
185185
{
186186
$modelName = Str::studly(Str::singular($table));
187187
$className = 'Api' . $modelName . 'Controller';
188-
188+
189189
$apiOptions = array_merge($options, ['is_api' => true]);
190190
$content = $this->buildClass($table, $apiOptions, 'api');
191191

@@ -218,19 +218,19 @@ public function buildClass(string $table, array $options, string $type = 'web'):
218218
{
219219
$modelName = Str::studly(Str::singular($table));
220220
$modelVariable = Str::camel($modelName);
221-
221+
222222
if ($type === 'api') {
223223
$className = 'Api' . $modelName . 'Controller';
224224
$namespace = $this->getNamespace() . '\\Api';
225225
} else {
226226
$className = $this->getClassName($table);
227227
$namespace = $this->getNamespace();
228228
}
229-
229+
230230
$modelNamespace = Config::get('crud.namespaces.models', 'App\\Models');
231231
$serviceClass = $modelName . 'Service';
232232
$serviceNamespace = Config::get('crud.namespaces.services', 'App\\Services');
233-
233+
234234
$stub = $this->getStub($type);
235235

236236
// Setup service injection
@@ -343,7 +343,7 @@ public function __construct({$serviceClass} \$service)
343343
public function setupRequestValidation(array $requestClasses): string
344344
{
345345
$validationCode = "";
346-
346+
347347
if (!empty($requestClasses)) {
348348
$validationCode = " /**
349349
* Get the appropriate request class for the given action.
@@ -361,7 +361,7 @@ protected function getRequestClass(string \$action): string
361361
$validationCode .= " ][\$action] ?? FormRequest::class;
362362
}";
363363
}
364-
364+
365365
return $validationCode;
366366
}
367367

@@ -395,7 +395,7 @@ protected function authorizeAction(\$ability, \$arguments = [])
395395
public function generateViewRenderingMethods(string $table): string
396396
{
397397
$viewPrefix = Str::kebab(Str::plural($table));
398-
398+
399399
return " /**
400400
* Get the view for the given action.
401401
*
@@ -586,7 +586,7 @@ public function generateCrudMethods(string $table, string $type, array $options)
586586
$modelPluralVariable = Str::camel(Str::plural($modelName));
587587
$requestStore = $modelName . 'StoreRequest';
588588
$requestUpdate = $modelName . 'UpdateRequest';
589-
589+
590590
if ($type === 'api') {
591591
return " /**
592592
* Display a listing of the resource.
@@ -960,7 +960,7 @@ protected function getRequiredImports(
960960
"{$requestNamespace}\\{$modelName}StoreRequest",
961961
"{$requestNamespace}\\{$modelName}UpdateRequest",
962962
];
963-
963+
964964
if ($type === 'api') {
965965
$apiImports = [
966966
'Illuminate\Http\JsonResponse',
@@ -974,15 +974,47 @@ protected function getRequiredImports(
974974
];
975975
$imports = array_merge($commonImports, $webImports);
976976
}
977-
977+
978978
$imports = array_unique($imports);
979979
sort($imports);
980-
980+
981981
$importStatements = '';
982982
foreach ($imports as $import) {
983983
$importStatements .= "use {$import};\n";
984984
}
985-
985+
986986
return $importStatements;
987987
}
988-
}
988+
989+
/**
990+
* Set configuration options for the generator.
991+
*
992+
* @param array $options Configuration options
993+
* @return self Returns the generator instance for method chaining
994+
*/
995+
public function setOptions(array $options): self
996+
{
997+
$this->options = array_merge($this->options ?? [], $options);
998+
return $this;
999+
}
1000+
1001+
/**
1002+
* Get a list of all generated file paths.
1003+
*
1004+
* @return array List of generated file paths
1005+
*/
1006+
public function getGeneratedFiles(): array
1007+
{
1008+
return $this->generatedFiles ?? [];
1009+
}
1010+
1011+
/**
1012+
* Determine if the generator supports customization.
1013+
*
1014+
* @return bool True if the generator supports customization
1015+
*/
1016+
public function supportsCustomization(): bool
1017+
{
1018+
return true;
1019+
}
1020+
}

0 commit comments

Comments
 (0)