Skip to content

Commit e5a1e5f

Browse files
committed
refactor: updated generator commands. make:repo command updates models and API Resources
1 parent 72cb989 commit e5a1e5f

File tree

8 files changed

+109
-52
lines changed

8 files changed

+109
-52
lines changed

app/Console/Generators/EnumMakeCommand.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: lexxyungcarter
5-
* Date: 23/10/2019
6-
* Time: 12:18
7-
*/
82

93
namespace App\Console\Generators;
104

app/Console/Generators/FacadeMakeCommand.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: lexxyungcarter
5-
* Date: 23/10/2019
6-
* Time: 12:18
7-
*/
82

93
namespace App\Console\Generators;
104

app/Console/Generators/FilterMakeCommand.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: lexxyungcarter
5-
* Date: 13/10/2019
6-
* Time: 21:04
7-
*/
82

93
namespace App\Console\Generators;
104

app/Console/Generators/RepositoryMakeCommand.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Console\Generators;
44

5+
use Str;
6+
57
class RepositoryMakeCommand extends GeneratorCommand
68
{
79
/**
@@ -16,7 +18,7 @@ class RepositoryMakeCommand extends GeneratorCommand
1618
*
1719
* @var string
1820
*/
19-
protected $description = 'Create a new AceLords project event class';
21+
protected $description = 'Create a repository class';
2022

2123
/**
2224
* The type of class being generated.
@@ -56,4 +58,70 @@ protected function getDefaultNamespace($rootNamespace)
5658
{
5759
return $rootNamespace.'\Repositories';
5860
}
61+
62+
/**
63+
* Replace the namespace for the given stub.
64+
*
65+
* @param string $stub
66+
* @param string $name
67+
* @return $this
68+
*/
69+
protected function replaceNamespace(&$stub, $name)
70+
{
71+
$stub = str_replace(
72+
[
73+
'DummyNamespace',
74+
'DummyRootNamespace',
75+
'NamespacedDummyUserModel',
76+
'DummyModelTableName',
77+
'DummySeederClassName',
78+
'DummyModel',
79+
'DummyResource',
80+
'DummyResourceCollection',
81+
],
82+
[
83+
$this->getNamespace($name),
84+
$this->rootNamespace(),
85+
$this->userProviderModel(),
86+
$this->getDefaultModelTableName($name),
87+
$this->getSeederNameFromClassName(),
88+
$this->getModelNameFromClassName(),
89+
$this->getResourceNameFromClassName(),
90+
$this->getResourceCollectionNameFromClassName(),
91+
],
92+
$stub
93+
);
94+
95+
return $this;
96+
}
97+
98+
/**
99+
* Get model name.
100+
*
101+
* @return string
102+
*/
103+
protected function getModelNameFromClassName()
104+
{
105+
return Str::replace('Repository', '', $this->className);
106+
}
107+
108+
/**
109+
* Get resource name.
110+
*
111+
* @return string
112+
*/
113+
protected function getResourceNameFromClassName()
114+
{
115+
return $this->getModelNameFromClassName() . 'Resource';
116+
}
117+
118+
/**
119+
* Get resource collection name.
120+
*
121+
* @return string
122+
*/
123+
protected function getResourceCollectionNameFromClassName()
124+
{
125+
return $this->getModelNameFromClassName() . 'ResourceCollection';
126+
}
59127
}

app/Console/Generators/ResourceMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ResourceMakeCommand extends GeneratorCommand
1919
*
2020
* @var string
2121
*/
22-
protected $description = 'Create a new AceLords project resource';
22+
protected $description = 'Create a new resource';
2323

2424
/**
2525
* The type of class being generated.

app/Console/Generators/ServiceMakeCommand.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: lexxyungcarter
5-
* Date: 23/10/2019
6-
* Time: 12:22
7-
*/
82

93
namespace App\Console\Generators;
104

@@ -22,7 +16,7 @@ class ServiceMakeCommand extends GeneratorCommand
2216
*
2317
* @var string
2418
*/
25-
protected $description = 'Create a new AceLords project service class for a facade class';
19+
protected $description = 'Create a new service class for a facade class';
2620

2721
/**
2822
* The type of class being generated.

app/Console/Generators/WidgetMakeCommand.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: lexxyungcarter
5-
* Date: 14/10/2019
6-
* Time: 16:32
7-
*/
82

93
namespace App\Console\Generators;
104

@@ -22,7 +16,7 @@ class WidgetMakeCommand extends GeneratorCommand
2216
*
2317
* @var string
2418
*/
25-
protected $description = 'Create a new AceLords project widget class';
19+
protected $description = 'Create a new widget class';
2620

2721
/**
2822
* The type of class being generated.

app/Console/Generators/stubs/repository.stub

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
namespace DummyNamespace;
44

55
use DB;
6+
use Exception;
7+
use App\Models\DummyModel;
8+
use App\Events\CRUDErrorOccurred;
9+
use App\Http\Resources\DummyResource;
10+
use App\Http\Resources\DummyResourceCollection;
611

712
class DummyClass
813
{
@@ -13,7 +18,7 @@ class DummyClass
1318
*
1419
* @return void
1520
*/
16-
public function __construct(Model $model)
21+
public function __construct(DummyModel $model)
1722
{
1823
$this->model = $model;
1924
}
@@ -23,35 +28,35 @@ class DummyClass
2328
*
2429
* @param string $pagination
2530
*
26-
* @return ModelResourceCollection
31+
* @return DummyResourceCollection
2732
*/
2833
public function get($pagination = 'm')
2934
{
3035
$data = $this->model->latest()
31-
->paginate(core_paginate($pagination));
36+
->paginate(pagination($pagination));
3237

33-
return new ModelResourceCollection($data);
38+
return new DummyResourceCollection($data);
3439
}
3540

3641
/**
3742
* find model record
3843
* @param $id
3944
*
40-
* @return ModelResource
45+
* @return DummyResource
4146
*/
4247
public function find($id)
4348
{
4449
$item = $this->model->findOrFail($id);
4550

46-
return new ModelResource($item);
51+
return new DummyResource($item);
4752
}
4853

4954
/**
5055
* search records
5156
*
5257
* @param string $pagination
5358
*
54-
* @return ModelResourceCollection
59+
* @return DummyResourceCollection
5560
*/
5661
public function search($pagination = 'm')
5762
{
@@ -60,15 +65,15 @@ class DummyClass
6065
$data = $this->model
6166
->where('name', 'LIKE', '%' . $term . '%')
6267
->latest()
63-
->paginate(core_paginate($pagination));
68+
->paginate(pagination($pagination));
6469

65-
return new ModelResourceCollection($data);
70+
return new DummyResourceCollection($data);
6671
}
6772

6873
/**
6974
* create a new record
7075
*
71-
* @return ModelResource
76+
* @return DummyResource
7277
*/
7378
public function create()
7479
{
@@ -80,21 +85,23 @@ class DummyClass
8085
]);
8186

8287
DB::commit();
83-
} catch (\Exception $e) {
88+
} catch (Exception $e) {
8489
DB::rollBack();
8590

86-
debugOn() ? dde($e->getMessage()) : false;
91+
ddOnError($e);
92+
93+
event(new CRUDErrorOccurred($e->getMessage()));
8794

8895
return false;
8996
}
9097

91-
return new ModelResource($data);
98+
return new DummyResource($data);
9299
}
93100

94101
/**
95102
* update a record
96103
*
97-
* @return ModelResource
104+
* @return DummyResource
98105
*/
99106
public function update($id)
100107
{
@@ -108,15 +115,17 @@ class DummyClass
108115
]);
109116

110117
DB::commit();
111-
} catch (\Exception $e) {
118+
} catch (Exception $e) {
112119
DB::rollBack();
113120

114-
debugOn() ? dde($e->getMessage()) : false;
121+
ddOnError($e);
122+
123+
event(new CRUDErrorOccurred($e->getMessage()));
115124

116125
return false;
117126
}
118127

119-
return new ModelResource($data);
128+
return new DummyResource($data);
120129
}
121130

122131
/**
@@ -128,6 +137,16 @@ class DummyClass
128137
*/
129138
public function delete($id)
130139
{
131-
return $this->model->findOrFail($id)->delete();
140+
try {
141+
$resp = $this->model->findOrFail($id)->delete();
142+
} catch (Exception $e) {
143+
event(new CRUDErrorOccurred($e->getMessage()));
144+
145+
return false;
146+
}
147+
148+
// event - alert
149+
150+
return $resp;
132151
}
133152
}

0 commit comments

Comments
 (0)