Skip to content

Commit 8c14393

Browse files
committed
Merge branch '140-use-NovaResource-class-in-NovaTestGenerator-instead-of-Model' into 159-ability-to-set-nova-resource-for-nova-tests-generator
2 parents 6330b23 + defdc96 commit 8c14393

File tree

60 files changed

+461
-668
lines changed

Some content is hidden

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

60 files changed

+461
-668
lines changed

ReadMe.md

Lines changed: 97 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,124 +4,104 @@
44

55
Laravel-Entity-Generator - This generator is used to create a standard class stack for a new entity.
66

7-
### Install
7+
## Installation
88

9-
We're highly recommending to install package for only dev environment
9+
Install package using `dev` mode
1010

1111
```bash
12-
composer require ronasit/laravel-entity-generator --dev
12+
composer require ronasit/laravel-entity-generator --dev
1313
```
1414

15-
If you're on Laravel 5.5 or later the package will be auto-discovered.
16-
Otherwise, you will need to manually configure it in your config/app.php.
15+
Publish package's resources.
1716

18-
```php
19-
'providers' => [
20-
// ...
21-
RonasIT\Support\EntityGeneratorServiceProvider::class,
22-
],
17+
```bash
18+
php artisan vendor:publish --provider="RonasIT\Support\EntityGeneratorServiceProvider"
2319
```
2420

25-
For dev installation provider should be registered optionally
26-
27-
```php
28-
class AppServiceProvider
29-
{
30-
public function boot(): void
31-
{
32-
// ...
33-
if (config('app.env') === 'local') {
34-
App::register(\RonasIT\Support\EntityGeneratorServiceProvider::class);
35-
}
36-
}
37-
}
38-
```
21+
## Usage
3922

40-
And publish.
23+
### Entity generation
4124

42-
```bash
43-
php artisan vendor:publish --provider="RonasIT\Support\EntityGeneratorServiceProvider"
44-
```
25+
Call `make:entity` artisan command to run the generation command for the full stack of the entity classes:
4526

46-
### Examples
4727
```bash
48-
php artisan make:entity EntityName \
49-
-S required_string_field \
50-
--integer=not_required_integer_field \
51-
--boolean-required=required_boolean_field \
52-
-j data \
53-
-e AnotherEntityName
28+
php artisan make:entity Post
5429
```
5530

56-
### Documentation
57-
58-
`make:entity` artisan command - generate stack of classes to work with the new entity in project.
59-
60-
Syntax:
31+
Entity name may contain the subfolder, in this case generated `model` and `nova resource` will be placed to
32+
the subfolder as well:
6133

6234
```bash
63-
> php artisan make:entity [entity-name] [options]
35+
php artisan make:entity Forum/Blog/Post
6436
```
6537

66-
`entity-name` - Name of the Entity, recommended to use `CamelCase` naming style e.g. `WhitelistedDomain`
67-
68-
`options` - one or more options from the lists below
69-
7038
#### Fields definition options
7139

72-
-i|--integer : Add integer field to entity.
73-
74-
-I|--integer-required : Add required integer field to entity. If you want to specify default value you have to do it manually.
75-
76-
-f|--float : Add float field to entity.
77-
78-
-F|--float-required : Add required float field to entity. If you want to specify default value you have to do it manually.
79-
80-
-s|--string : Add string field to entity. Default type is VARCHAR(255) but you can change it manually in migration.
40+
The `make:entity` provides an ability to set the entity's fields, which will be used in all created classes (e.g. Model, Create/Update requests, Test fixtures, etc.)
8141

82-
-S|--string-required : Add required string field to entity. If you want to specify default value ir size you have to do it manually.
83-
84-
-b|--boolean : Add boolean field to entity.
85-
86-
-B|--boolean-required : Add boolean field to entity. If you want to specify default value you have to do it manually.
87-
88-
-t|--timestamp : Add timestamp field to entity.
89-
90-
-T|--timestamp-required : Add timestamp field to entity. If you want to specify default value you have to do it manually.
42+
```bash
43+
php artisan make:entity Post -S title -S text -t published_at
44+
```
9145

92-
-j|--json : Add json field to entity.
46+
The following field types are available to use:
47+
48+
| Type | Modificator | Short Option | Full Option |
49+
| -------- | -------- | ------- | ------- |
50+
| `integer` | | `-i` | `--integer` |
51+
| `integer` | required | `-I` | `--integer-required` |
52+
| `float` | | `-f` | `--float` |
53+
| `float` | required | `-F` | `--float-required` |
54+
| `string` | | `-s` | `--string` |
55+
| `string` | required | `-S` | `--string-required` |
56+
| `boolean` | | `-b` | `--boolean` |
57+
| `boolean` | required | `-B` | `--boolean-required` |
58+
| `datetime` | | `-t` | `--timestamp` |
59+
| `datetime` | required | `-T` | `--timestamp-required` |
60+
| `json` | | `-j` | `--json` |
9361

9462
#### Relations definitions options
9563

96-
-a|--has-one : Set hasOne relations between you entity and existed entity.
97-
98-
-A|--has-many : Set hasMany relations between you entity and existed entity.
99-
100-
-e|--belongs-to : Set belongsTo relations between you entity and existed entity.
101-
102-
-E|--belongs-to-many : Set belongsToMany relations between you entity and existed entity.
103-
104-
#### Single class generation mode options
64+
Command also provides an ability to set relations, which will be added to the model
10565

106-
--only-model : Set this flag if you want to create only model. This flag is a higher priority than --only-migration, --only-tests and --only-repository.
66+
```bash
67+
php artisan make:entity Post -A Comment -A Reaction
68+
```
10769

108-
--only-repository : Set this flag if you want to create only repository. This flag is a higher priority than --only-tests and --only-migration.
70+
Relation may be placed in the subfolder, in this case - set the relative namespace from the model directory:
10971

110-
--only-service : Set this flag if you want to create only service.
72+
```bash
73+
php artisan make:entity Post -A Blog/Comment -A Forum/Common/Reaction
74+
```
11175

112-
--only-controller : Set this flag if you want to create only controller.
76+
The following options are available to set relations:
11377

114-
--only-requests : Set this flag if you want to create only requests.
78+
| Type | Short Option | Full Option |
79+
| -------- | ------- | ------- |
80+
| Has one | `-a` | `--has-one` |
81+
| Has many | `-A` | `--has-many` |
82+
| Belongs to | `-e` | `--belongs-to` |
83+
| Belongs to many | `-E` | `--belongs-to-many` |
11584

116-
--only-migration : Set this flag if you want to create only repository. This flag is a higher priority than --only-tests.
85+
#### Single class generation mode options
11786

118-
--only-factory : Set this flag if you want to create only factory.
87+
Command allows to generate only single entity-related class
11988

120-
--only-tests : Set this flag if you want to create only tests.
89+
```bash
90+
php artisan make:entity Post --only-tests
91+
```
12192

122-
--only-seeder : Set this flag if you want to create only seeder.
93+
The following options are available:
12394

124-
--only-resource : Set this flag if you want to create only resource.
95+
- `--only-model`
96+
- `--only-repository`
97+
- `--only-service`
98+
- `--only-controller`
99+
- `--only-requests`
100+
- `--only-migration`
101+
- `--only-factory`
102+
- `--only-tests`
103+
- `--only-seeder`
104+
- `--only-resource`
125105

126106
--only-nova-resource : Set this flag if you want to create only nova resource.
127107

@@ -133,18 +113,43 @@ Syntax:
133113

134114
#### Mode combination options
135115

136-
--only-entity : Generate stack of classes to work with entity inside the app (Migration/Model/Service/Repository)
116+
Sometimes you need to generate the stack of classes to work with some entity only inside the application without
117+
the ability to manipulate it using API, or you need to create only API for already existed entity.
118+
119+
For this task, there are two mutually exclusive options:
120+
121+
1. `--only-entity`, generate stack of classes to work with entity only inside the app, it includes:
122+
- `migration`
123+
- `model`
124+
- `service`
125+
- `repository`
137126

138-
--only-api : Generate stack of classes to implement API part to work with entity (routes/Controller/Requests/Tests)
127+
2. `--only-api`, generate stack of classes to implement API part to work with already existed entity:
128+
- `routes`
129+
- `controller`
130+
- `requests`
131+
- `tests`
139132

140-
#### Additional generation options
133+
#### Methods setting options
134+
135+
By default, the command generate all methods from the CRUD stack which includes:
136+
- create
137+
- update
138+
- delete
139+
- search
140+
- get by id
141+
142+
But what if we need to create the interface only for updating and reading the entity? Just use the `methods` option for it:
143+
144+
```bash
145+
php artisan make:entity Post --methods=RU
146+
```
141147

142-
--methods=[default: CRUD] : Don't work for `--only-entity` option. Will generate API classes (routes, controller's
143-
methods, requests, tests) only for choosed methods.
144-
C - created
145-
R - read (search and get by id)
146-
U - update
147-
D - delete
148+
Feel free to use any combinations of actions in any order. Each action has its own character:
149+
- `C` create
150+
- `U` update
151+
- `D` delete
152+
- `R` read (search and get by id)
148153

149154
## Release notes
150155

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
"psr-4": {
3636
"RonasIT\\Support\\Tests\\": "tests/",
3737
"RonasIT\\Support\\Tests\\Support\\": "tests/Support/",
38-
"App\\Nova\\": "tests/Support/Nova/",
39-
"Laravel\\Nova\\": "tests/Support/NovaResource/"
38+
"App\\Nova\\": "tests/Support/Nova/"
4039
},
4140
"files": [
4241
"tests/TestCase.php"

config/entity-generator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
'relation' => 'entity-generator::relation',
2323
'repository' => 'entity-generator::repository',
2424
'service' => 'entity-generator::service',
25-
'service_with_trait' => 'entity-generator::service_with_trait',
2625
'controller' => 'entity-generator::controller',
2726
'request' => 'entity-generator::request',
2827
'routes' => 'entity-generator::routes',

src/Commands/MakeEntityCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class MakeEntityCommand extends Command
9191
protected $rules = [
9292
'only' => [
9393
'only-api' => [ResourceGenerator::class, ControllerGenerator::class, RequestsGenerator::class, TestsGenerator::class],
94-
'only-entity' => [MigrationGenerator::class, ModelGenerator::class, ServiceGenerator::class, RepositoryGenerator::class, FactoryGenerator::class, SeederGenerator::class],
94+
'only-entity' => [MigrationGenerator::class, ModelGenerator::class, RepositoryGenerator::class, ServiceGenerator::class, FactoryGenerator::class, SeederGenerator::class],
9595
'only-model' => [ModelGenerator::class],
9696
'only-repository' => [RepositoryGenerator::class],
9797
'only-service' => [ServiceGenerator::class],

src/DTO/RelationsDTO.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,4 @@ public function __construct(
1111
public array $belongsToMany = [],
1212
) {
1313
}
14-
15-
public function toArray(): array
16-
{
17-
return get_object_vars($this);
18-
}
1914
}

src/Generators/AbstractTestsGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ abstract class AbstractTestsGenerator extends EntityGenerator
2525

2626
public function generate(): void
2727
{
28+
$this->createNamespace('factories');
29+
2830
if ($this->canGenerateUserData()) {
2931
$this->withAuth = true;
3032
}
@@ -173,7 +175,7 @@ protected function getMockModel($model): array
173175
return [];
174176
}
175177

176-
$factoryNamespace = "{$this->getOrCreateNamespace('factories')}\\{$model}Factory";
178+
$factoryNamespace = "{$this->getNamespace('factories')}\\{$model}Factory";
177179
$factory = $factoryNamespace::new();
178180

179181
return $factory

src/Generators/ControllerGenerator.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function generate(): void
3131
return;
3232
}
3333

34+
$this->createNamespace('controllers');
35+
3436
$controllerContent = $this->getControllerContent($this->model);
3537

3638
$this->saveClass('controllers', "{$this->model}Controller", $controllerContent);
@@ -45,10 +47,10 @@ protected function getControllerContent($model): string
4547
return $this->getStub('controller', [
4648
'entity' => $model,
4749
'requestsFolder' => $model,
48-
'namespace' => $this->getOrCreateNamespace('controllers'),
49-
'requestsNamespace' => $this->getOrCreateNamespace('requests'),
50-
'resourcesNamespace' => $this->getOrCreateNamespace('resources'),
51-
'servicesNamespace' => $this->getOrCreateNamespace('services'),
50+
'namespace' => $this->getNamespace('controllers'),
51+
'requestsNamespace' => $this->getNamespace('requests'),
52+
'resourcesNamespace' => $this->getNamespace('resources'),
53+
'servicesNamespace' => $this->getNamespace('services'),
5254
]);
5355
}
5456

@@ -98,7 +100,7 @@ protected function addUseController(string $routesPath): void
98100
$routesFileContent = file_get_contents($routesPath);
99101

100102
$stub = $this->getStub('use_routes', [
101-
'namespace' => $this->getOrCreateNamespace('controllers'),
103+
'namespace' => $this->getNamespace('controllers'),
102104
'entity' => $this->model
103105
]);
104106

0 commit comments

Comments
 (0)