Skip to content

Commit e62700b

Browse files
committed
Revert "first commit"
This reverts commit 4cd8b4b.
1 parent 4cd8b4b commit e62700b

File tree

11 files changed

+84
-210
lines changed

11 files changed

+84
-210
lines changed

src/Classes/ControllerGenerator.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Http\Controllers\Controller;
66
use Illuminate\Http\Request;
7-
use Illuminate\Support\Str;
87
use Nette\PhpGenerator\ClassType;
98
use Nette\PhpGenerator\PhpNamespace;
109
use Shetabit\ModuleGenerator\Helpers\Helper;
@@ -28,14 +27,11 @@ class ControllerGenerator
2827
*/
2928
protected $baseRelationName;
3029
protected $attributes;
31-
protected $config;
3230

3331
public function __construct($module, $models)
3432
{
3533
$this->models = $models['Models'];
3634
$this->module = $module;
37-
$this->config = \config()->get('moduleConfig');
38-
3935
}
4036

4137
public function generate(): string
@@ -78,16 +74,16 @@ public function generateControllerTemplates($option): PhpNamespace
7874

7975
public function setMethodToController($class, $option, $namespace)
8076
{
81-
if (str_contains($option , 'R')) {
77+
if (strpos($option , 'R') == true) {
8278
$this->indexAndShowMethodGenerator($class);
8379
}
84-
if (str_contains($option, 'C')) {
80+
if (strpos($option, 'C') == true) {
8581
$this->createAndStoreMethodGenerator($class);
8682
}
87-
if (str_contains($option, 'U')) {
88-
$this->editAndUpdateMethodGenerator($class , $namespace);
83+
if (strpos($option, 'U') == true) {
84+
$this->editAndUpdateMethodGenerator($class, $namespace);
8985
}
90-
if (str_contains($option, 'D')) {
86+
if (strpos($option, 'D') == true) {
9187
$this->destroyMethodGenerator($class);
9288
}
9389
}
@@ -97,14 +93,14 @@ public function indexAndShowMethodGenerator(classType $class)
9793
$method = $class->addMethod('index');
9894
if (key_exists('Relations', $this->attributes)) {
9995
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::withCommonRelations()->get();' . PHP_EOL)
100-
->addBody($this->config['return']);
96+
->addBody('return response()->json($' . strtolower($this->modelName) . 's);');
10197
} else {
10298
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();' . PHP_EOL)
103-
->addBody($this->config['return']);
99+
->addBody('return response()->json($' . strtolower($this->modelName) . 's);');
104100
}
105101
$class->addMethod('show')
106102
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
107-
->addBody($this->config['return'])
103+
->addBody('return response()->json($' . strtolower($this->modelName) . ');')
108104
->addParameter('id')->setType('Int');
109105
}
110106

@@ -133,9 +129,6 @@ public function associateInStore($method)
133129
{
134130
if (key_exists('Relations', $this->attributes)) {
135131
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
136-
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
137-
return '';
138-
}
139132
foreach ($relations as $value) {
140133
$this->baseRelationName = explode('::', $value)[1];
141134
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
@@ -145,21 +138,21 @@ public function associateInStore($method)
145138
}
146139
}
147140

148-
public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
141+
public function editAndUpdateMethodGenerator(ClassType $class, $namespace)
149142
{
150143
$method = $class->addMethod('edit');
151144
if (key_exists('Relations', $this->attributes)) {
152145
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::withCommonRelations()->findOrFail($id);' . PHP_EOL)
153-
->addBody($this->config['return']);
146+
->addBody('return response()->json($' . strtolower($this->modelName) . ');');
154147
} else {
155148
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
156-
->addBody($this->config['return']);
149+
->addBody('return response()->json($' . strtolower($this->modelName) . ');');
157150
};
158151
$method->addParameter('id')->setType('Int');
159152

160153
$method = $class->addMethod('update')
161154
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);');
162-
$this->UpdateMethodFindIntoRelation($method , $namespace);
155+
$this->UpdateMethodFindIntoRelation($method, $namespace);
163156
$this->associateInUpdate($method);
164157
$method->addBody('$' . strtolower($this->modelName) . '->fill($request->all());')
165158
->addBody('$' . strtolower($this->modelName) . '->save();'.PHP_EOL)
@@ -171,13 +164,10 @@ public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
171164
$method->addParameter('id')->setType('Int');
172165
}
173166

174-
public function UpdateMethodFindIntoRelation($method ,$namespace)
167+
public function UpdateMethodFindIntoRelation($method, $namespace)
175168
{
176169
if (key_exists('Relations', $this->attributes)) {
177170
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
178-
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
179-
return '';
180-
}
181171
foreach ($relations as $value) {
182172
$this->baseRelationName = explode('::', $value)[1];
183173
$method->addBody('$' . strtolower($this->baseRelationName) . ' = ' . ucfirst($this->baseRelationName) . '::query()->findOrFail($request->' . strtolower($this->baseRelationName) . '_id);');
@@ -191,9 +181,6 @@ public function associateInUpdate($method)
191181
{
192182
if (key_exists('Relations', $this->attributes)) {
193183
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
194-
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
195-
return '';
196-
}
197184
foreach ($relations as $value) {
198185
$this->baseRelationName = explode('::', $value)[1];
199186
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
@@ -207,7 +194,7 @@ public function destroyMethodGenerator(ClassType $class)
207194
{
208195
$class->addMethod('destroy')
209196
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::destroy($id);' . PHP_EOL)
210-
->addBody($this->config['return'])
197+
->addBody('return response()->json($' . strtolower($this->modelName) . ');')
211198
->addParameter('id')->setType('Int');
212199
}
213200

src/Classes/ForeignKeyGenerator.php

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

33
namespace Shetabit\ModuleGenerator\Classes;
44

5-
use Carbon\Carbon;
65
use Illuminate\Database\Migrations\Migration;
76
use Illuminate\Database\Schema\Blueprint;
87
use Illuminate\Support\Facades\Schema;
98
use Illuminate\Support\Str;
9+
use Nette\PhpGenerator\ClassType;
1010
use Nette\PhpGenerator\PhpNamespace;
11-
use Shetabit\ModuleGenerator\Traits\CreatePivotTable;
11+
use Shetabit\ModuleGenerator\Helpers\Helper;
1212
use Symfony\Component\Finder\Finder;
1313

1414
class ForeignKeyGenerator
1515
{
16-
17-
use CreatePivotTable;
18-
1916
protected string $message = '';
2017
protected $models;
2118
protected $module;
2219
protected $fields;
20+
protected $nameOfMigration;
2321
protected $pathOfMigration;
2422
protected $nameOfModel;
2523

@@ -33,18 +31,16 @@ public function generate(): string
3331
{
3432
if (!key_exists('Models', $this->models)) return '';
3533
$namespace = new PhpNamespace('');
36-
34+
$namespace->addUse(Migration::class)->addUse(Blueprint::class)->addUse(Schema::class);
35+
$class = $namespace->addClass('CreateForeignKeyTable');
36+
$class->setExtends(Migration::class);
3737

3838
foreach ($this->models as $key => $model) {
39-
foreach ($model as $fields) {
40-
if (!key_exists('Relations', $fields)) return '';
41-
}
42-
$namespace->addUse(Migration::class)->addUse(Blueprint::class)->addUse(Schema::class);
43-
$class = $namespace->addClass('CreateForeignKeysTable');
44-
$class->setExtends(Migration::class);
4539
$this->pathOfMigration = module_path($this->module) . "/Database/Migrations/";
46-
$this->foreignKeyGenerator($model , $class);
40+
$this->nameOfMigration .= $this->foreignKeyGenerator($model , $class);
4741
}
42+
echo $this->nameOfMigration;
43+
die();
4844
$template = '<?php' . PHP_EOL . $namespace;
4945
$this->touchAndPutContent($template);
5046
$this->message .= "|-- ForeignKey successfully generate" . PHP_EOL;
@@ -54,59 +50,62 @@ public function generate(): string
5450

5551
public function foreignKeyGenerator($model , $class)
5652
{
57-
58-
$methodUp = $class->addMethod('up');
5953
foreach ($model as $key => $fields) {
60-
$this->nameOfModel = $key;
6154
if (!key_exists('Relations', $fields)) return '';
62-
$this->generateModelTemplates($methodUp , $fields['Relations']);
63-
55+
return $this->generateModelTemplates($class , $fields['Relations']);
6456
}
6557

6658
}
6759

68-
public function generateModelTemplates($methodUp, $fields)
60+
public function generateModelTemplates($class, $fields)
6961
{
7062
foreach ($fields as $key => $field) {
71-
if (!is_array($field) && Str::camel($field) == 'morphTo'){
72-
return '';
73-
}
7463
foreach ($field as $item) {
75-
$this->addMethodsInMigration($item , $methodUp);
76-
if(Str::camel($key) == 'belongsToMany'){
77-
$this->createPivot($this->nameOfModel , $field);
78-
}
64+
return $this->addMethodsInMigration($class , $item , $key);
7965
}
80-
66+
// if($key == 'belongsToMany'){
67+
////povit
68+
// }
8169
// if ($key == 'morphToMany'){
82-
////pivot
70+
//povit
8371
// }
84-
72+
//
73+
// (new Helper)->manyToManyTableName('blog' , 'category');
8574
}
75+
}
76+
77+
public function addMethodsInMigration(ClassType $class , $fields , $model)
78+
{
79+
$methodUp = $class->addMethod('up')
80+
->addBody("Schema::create('foreign_key', function (Blueprint \$table) {".PHP_EOL."\t \$table->id();");
81+
$methodUp->addBody($this->addFieldsInMethod($fields , $model));
82+
$methodUp->addBody(PHP_EOL."});");
83+
84+
$class->addMethod('down')
85+
->addBody("Schema::dropIfExists('foreign_key');");
86+
87+
return $class;
8688

8789
}
8890

89-
public function addMethodsInMigration($fields , $methodUp)
91+
public function addFieldsInMethod($fields , $model)
9092
{
9193
$model = explode('::', $fields)[0];
9294
$model2 = explode('::', $fields)[1];
93-
94-
$methodUp->addBody("Schema::table('".Str::plural(Str::snake($this->nameOfModel))."', function (Blueprint \$table) {");
95-
$methodUp->addBody("\t \$table->foreignId('".strtolower($model2)."_id')->constrained('".Str::plural(Str::snake($model2))."');");
96-
$methodUp->addBody("});");
95+
// $table->foreignId('user_id')->constrained('users');
96+
$field = "\t \$table->foreignId('".strtolower($model)."_id')->constrained('".Str::plural(Str::snake($model2))."');";
97+
return $field;
9798
}
9899

99100

100-
101101
public function touchAndPutContent($template): bool
102102
{
103-
foreach (Finder::create()->files()
104-
->name("*create_foreign_keys_table.php")
105-
->in($this->pathOfMigration) as $file) {
106-
unlink($file->getPathname());
107-
}
108-
$tableFileName = Carbon::now()->addYears(1)->format('Y_m_d_His_') ."create_foreign_keys_table.php";
109-
103+
// foreach (Finder::create()->files()
104+
// ->name("*create_".Str::plural(Str::snake($this->nameOfMigration))."_table.php")
105+
// ->in($this->pathOfMigration) as $file) {
106+
// unlink($file->getPathname());
107+
// }
108+
$tableFileName = "create_foreign_key_table.php";
110109
$pathOfFile = $this->pathOfMigration.$tableFileName;
111110
touch($pathOfFile);
112111
file_put_contents($pathOfFile, $template);
@@ -117,6 +116,4 @@ public function __toString(): string
117116
{
118117
return $this->message;
119118
}
120-
121-
122119
}

src/Classes/MigrationGenerator.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,26 @@ public function generate(): string
3838
public function migrationsGenerator($model): string
3939
{
4040
foreach ($model as $key => $fields) {
41-
$relation = null;
42-
if (key_exists('Relations', $fields)) {
43-
$relation = $fields['Relations'];
44-
}
4541
$this->nameOfMigration = $key;
4642
$namespace = new PhpNamespace('');
4743
$namespace->addUse(Migration::class)->addUse(Blueprint::class)->addUse(Schema::class);
4844
$class = $namespace->addClass('Create' . Str::plural($this->nameOfMigration) . 'Table');
4945
$class->setExtends(Migration::class);
50-
$this->addMethodsInMigration($class, $fields['Fields'] , $relation);
46+
$this->addMethodsInMigration($class, $fields['Fields']);
5147
$template = '<?php' . PHP_EOL . $namespace;
5248
$this->touchAndPutContent($template);
5349
$this->message .= "|-- Migration " . $this->nameOfMigration . " successfully generate" . PHP_EOL;
5450
}
5551
return $this->message;
5652
}
5753

58-
public function addMethodsInMigration(ClassType $class , $fields , $relation)
54+
public function addMethodsInMigration(ClassType $class , $fields)
5955
{
60-
6156
$methodUp = $class->addMethod('up')
6257
->addBody("Schema::create('".Str::plural(Str::snake($this->nameOfMigration))."', function (Blueprint \$table) {".PHP_EOL."\t \$table->id();");
6358
$methodUp->addBody($this->addFieldsInMethod($fields));
64-
if ($relation != null) {
65-
foreach ($relation as $key => $item) {
66-
if (!is_array($item) && Str::camel($item) == 'morphTo') {
67-
$methodUp->addBody("\t \$table->integer('" . strtolower($this->nameOfMigration) . "able_id');");
68-
$methodUp->addBody("\t \$table->string('" . strtolower($this->nameOfMigration) . "able_type');");
69-
70-
}
71-
}
72-
}
7359
$methodUp->addBody("\t \$table->timestamps();".PHP_EOL."});");
60+
7461
$class->addMethod('down')
7562
->addBody("Schema::dropIfExists('".Str::plural(Str::snake($this->nameOfMigration))."');");
7663
return $class;

src/Classes/ModelGenerator.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class ModelGenerator
1717
protected $module;
1818
protected $pathOfModel;
1919
protected array $withRelation = [];
20-
protected $modelName;
2120

2221
public function __construct($module , $models)
2322
{
@@ -34,7 +33,6 @@ public function generate(): string
3433
public function generateModels($models): string
3534
{
3635
foreach ($models as $model => $attribute) {
37-
$this->modelName = $model;
3836
$this->pathOfModel = module_path($this->module) . "/Entities/" . $model . '.php';
3937

4038
$template = $this->generateModelTemplates($model, $attribute);
@@ -89,10 +87,6 @@ public function touchAndPutContent($template)
8987
public function setRelationsInModel($namespace, ClassType $class , $attribute)
9088
{
9189
foreach ($attribute['Relations'] as $typeRelation => $relations) {
92-
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
93-
$this->morphRelation($class);
94-
return '';
95-
}
9690
foreach ($relations as $value) {
9791
/**
9892
* @variable relationName Return name of relation example => Category
@@ -128,13 +122,4 @@ public function addWithCommonRelations(ClassType $class)
128122
->addBody('}')
129123
->addParameter('query');
130124
}
131-
132-
public function morphRelation($class)
133-
{
134-
$relationName = strtolower(Helper::configurationRelationsName($this->modelName, 'morphTo'));
135-
$this->withRelation[] = $relationName;
136-
$class->addMethod($relationName)
137-
->addBody('return $this->morphTo();')
138-
->setReturnType('Illuminate\Database\Eloquent\Relations\morphTo');
139-
}
140125
}

src/Classes/ModuleGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public function ModulesGenerator($modules)
5454
$this->result .= app(\Shetabit\ModuleGenerator\Contracts\RequestGenerator::class, [$module, $model])
5555
->generate();
5656

57-
$this->result .= app(\Shetabit\ModuleGenerator\Contracts\ForeignKeyGenerator::class, [$module, $model])
58-
->generate();
57+
// $this->result .= app(\Shetabit\ModuleGenerator\Contracts\ForeignKeyGenerator::class, [$module, $model])
58+
// ->generate();
5959
}
6060
}
6161
print($this->result);

0 commit comments

Comments
 (0)