Skip to content

Commit 08829ef

Browse files
authored
Merge pull request #7 from RonasIT/dpankratov/version-update
Dpankratov/version update
2 parents a2de505 + 3dc8e28 commit 08829ef

File tree

9 files changed

+229
-40
lines changed

9 files changed

+229
-40
lines changed

ReadMe.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@ Laravel-Entity-Generator - This generator is used to create a standard class sta
88
composer require ronasit/laravel-entity-generator: 1.*
99
```
1010

11-
Add `RonasIT\Support\EntityGeneratorServiceProvider::class` to `app/config/app.php`,
12-
into 'providers' variable. And publish.
11+
If you're on Laravel 5.5 or later the package will be auto-discovered.
12+
Otherwise you will need to manually configure it in your config/app.php.
13+
14+
```php
15+
'providers' => [
16+
// ...
17+
RonasIT\Support\EntityGeneratorServiceProvider::class,
18+
],
19+
```
20+
21+
And publish.
1322

1423
```bash
1524
php artisan vendor:publish

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,12 @@
2929
"src/helpers.php"
3030
]
3131
},
32-
"minimum-stability": "beta"
32+
"minimum-stability": "beta",
33+
"extra": {
34+
"laravel": {
35+
"providers": [
36+
"RonasIT\\Support\\EntityGeneratorServiceProvider"
37+
]
38+
}
39+
}
3340
}

src/Commands/MakeEntityCommand.php

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Facades\Config;
78
use RonasIT\Support\Events\SuccessCreateMessage;
9+
use RonasIT\Support\Exceptions\ClassNotExistsException;
810
use RonasIT\Support\Exceptions\EntityCreateException;
911
use RonasIT\Support\Generators\ControllerGenerator;
1012
use RonasIT\Support\Generators\EntityGenerator;
@@ -18,6 +20,7 @@
1820
use RonasIT\Support\Generators\TranslationsGenerator;
1921
use RonasIT\Support\Generators\SeederGenerator;
2022
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
23+
use UnexpectedValueException;
2124

2225
/**
2326
* @property ControllerGenerator $controllerGenerator
@@ -34,6 +37,10 @@
3437
*/
3538
class MakeEntityCommand extends Command
3639
{
40+
const CRUD_OPTIONS = [
41+
'C', 'R', 'U', 'D'
42+
];
43+
3744
/**
3845
* The name and signature of the console command.
3946
*
@@ -50,6 +57,8 @@ class MakeEntityCommand extends Command
5057
{--without-tests : Set this flag if you don\'t want to create tests. This flag is a lower priority than --only-tests.}
5158
{--without-seeder : Set this flag if you don\'t want to create seeder.}
5259
60+
{--only-api : Set this flag if you want to create controller, route, requests, tests.}
61+
{--only-entity : Set this flag if you want to create migration, model, repository, service, factory, seeder.}
5362
{--only-model : Set this flag if you want to create only model. This flag is a higher priority than --without-model, --only-migration, --only-tests and --only-repository.}
5463
{--only-repository : Set this flag if you want to create only repository. This flag is a higher priority than --without-repository, --only-tests and --only-migration.}
5564
{--only-service : Set this flag if you want to create only service.}
@@ -60,6 +69,8 @@ class MakeEntityCommand extends Command
6069
{--only-tests : Set this flag if you want to create only tests. This flag is a higher priority than --without-tests.}
6170
{--only-seeder : Set this flag if you want to create only seeder.}
6271
72+
{--methods=CRUD : Set types of methods to create. Affect on routes, requests classes, controller\'s methods and tests methods.}
73+
6374
{--i|integer=* : Add integer field to entity.}
6475
{--I|integer-required=* : Add required integer field to entity. If you want to specify default value you have to do it manually.}
6576
{--f|float=* : Add float field to entity.}
@@ -98,6 +109,8 @@ class MakeEntityCommand extends Command
98109

99110
protected $rules = [
100111
'only' => [
112+
'only-api' => [ControllerGenerator::class, RequestsGenerator::class, TestsGenerator::class],
113+
'only-entity' => [MigrationGenerator::class, ModelGenerator::class, ServiceGenerator::class, RepositoryGenerator::class, FactoryGenerator::class, SeederGenerator::class],
101114
'only-model' => [ModelGenerator::class],
102115
'only-repository' => [RepositoryGenerator::class],
103116
'only-service' => [ServiceGenerator::class],
@@ -150,6 +163,8 @@ public function __construct()
150163
*/
151164
public function handle()
152165
{
166+
$this->validateInput();
167+
$this->checkConfigs();
153168
$this->eventDispatcher->listen(SuccessCreateMessage::class, $this->getSuccessMessageCallback());
154169

155170
try {
@@ -159,6 +174,74 @@ public function handle()
159174
}
160175
}
161176

177+
protected function checkConfigs()
178+
{
179+
$packageConfigPath = __DIR__ . '/../../config/entity-generator.php';
180+
$packageConfigs = require $packageConfigPath;
181+
182+
$projectConfigs = config('entity-generator');
183+
184+
$newConfig = $this->outputNewConfig($packageConfigs, $projectConfigs);
185+
186+
if ($newConfig !== $projectConfigs) {
187+
$this->info('Config has been updated');
188+
Config::set('entity-generator', $newConfig);
189+
file_put_contents(config_path('entity-generator.php'), "<?php\n\nreturn" . $this->customVarExport($newConfig) . ';');
190+
}
191+
}
192+
193+
protected function outputNewConfig($packageConfigs, $projectConfigs)
194+
{
195+
$flattenedPackageConfigs = Arr::dot($packageConfigs);
196+
$flattenedProjectConfigs = Arr::dot($projectConfigs);
197+
198+
$newConfig = array_merge($flattenedPackageConfigs, $flattenedProjectConfigs);
199+
200+
$differences = array_diff_key($newConfig, $flattenedProjectConfigs);
201+
202+
foreach ($differences as $differenceKey => $differenceValue) {
203+
$this->info("Key '{$differenceKey}' was missing in your config, we added it with the value '{$differenceValue}'");
204+
}
205+
206+
return array_undot($newConfig);
207+
}
208+
209+
protected function customVarExport($expression)
210+
{
211+
$defaultExpression = var_export($expression, true);
212+
213+
$patterns = [
214+
'/array/' => '',
215+
'/\(/' => '[',
216+
'/\)/' => ']',
217+
'/=> \\n/' => '=>',
218+
'/=>.+\[/' => '=> [',
219+
'/^ {8}/m' => "\t\t\t\t",
220+
'/^ {6}/m' => "\t\t\t",
221+
'/^ {4}/m' => "\t\t",
222+
'/^ {2}/m' => "\t",
223+
];
224+
225+
return preg_replace(array_keys($patterns), array_values($patterns), $defaultExpression);
226+
}
227+
228+
protected function classExists($path, $name)
229+
{
230+
$paths = config('entity-generator.paths');
231+
232+
$entitiesPath = $paths[$path];
233+
234+
$classPath = base_path("{$entitiesPath}/{$name}.php");
235+
236+
return file_exists($classPath);
237+
}
238+
239+
protected function validateInput()
240+
{
241+
$this->validateOnlyApiOption();
242+
$this->validateCrudOptions();
243+
}
244+
162245
protected function generate()
163246
{
164247
foreach ($this->rules['only'] as $option => $generators) {
@@ -198,9 +281,15 @@ protected function runGeneration($generator)
198281
->setModel($this->argument('name'))
199282
->setFields($this->getFields())
200283
->setRelations($this->getRelations())
284+
->setCrudOptions($this->getCrudOptions())
201285
->generate();
202286
}
203287

288+
protected function getCrudOptions()
289+
{
290+
return str_split($this->option('methods'));
291+
}
292+
204293
protected function getRelations()
205294
{
206295
return [
@@ -222,5 +311,25 @@ protected function getFields()
222311
{
223312
return Arr::only($this->options(), EntityGenerator::AVAILABLE_FIELDS);
224313
}
225-
}
226314

315+
protected function validateCrudOptions()
316+
{
317+
$crudOptions = $this->getCrudOptions();
318+
319+
foreach ($crudOptions as $crudOption) {
320+
if (!in_array($crudOption, MakeEntityCommand::CRUD_OPTIONS)) {
321+
throw new UnexpectedValueException("Invalid method {$crudOption}.");
322+
}
323+
}
324+
}
325+
326+
protected function validateOnlyApiOption()
327+
{
328+
if ($this->option('only-api')) {
329+
$modelName = $this->argument('name');
330+
if (!$this->classExists('services', "{$modelName}Service")) {
331+
throw new ClassNotExistsException('Cannot create API without entity.');
332+
}
333+
}
334+
}
335+
}

src/Generators/EntityGenerator.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ abstract class EntityGenerator
1919
protected $model;
2020
protected $fields;
2121
protected $relations;
22+
protected $crudOptions;
23+
24+
/**
25+
* @param array $crudOptions
26+
* @return $this
27+
*/
28+
public function setCrudOptions($crudOptions)
29+
{
30+
$this->crudOptions = $crudOptions;
31+
32+
return $this;
33+
}
2234

2335
/**
2436
* @param string $model
@@ -101,6 +113,8 @@ protected function getStub($stub, $data = [])
101113
{
102114
$stubPath = config("entity-generator.stubs.$stub");
103115

116+
$data['options'] = $this->crudOptions;
117+
104118
return view($stubPath)->with($data)->render();
105119
}
106120

src/Generators/RequestsGenerator.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,38 @@ public function setRelations($relations)
2727

2828
public function generate()
2929
{
30-
$this->createRequest(
31-
self::GET_METHOD,
32-
true,
33-
$this->getGetValidationParameters()
34-
);
35-
36-
$this->createRequest(self::DELETE_METHOD);
30+
if (in_array('R', $this->crudOptions)) {
31+
$this->createRequest(
32+
self::GET_METHOD,
33+
true,
34+
$this->getGetValidationParameters()
35+
);
36+
$this->createRequest(
37+
self::SEARCH_METHOD,
38+
false,
39+
$this->getSearchValidationParameters()
40+
);
41+
}
3742

38-
$this->createRequest(
39-
self::CREATE_METHOD,
40-
false,
41-
$this->getValidationParameters($this->fields, true)
42-
);
43+
if (in_array('D', $this->crudOptions)) {
44+
$this->createRequest(self::DELETE_METHOD);
45+
}
4346

44-
$this->createRequest(
45-
self::UPDATE_METHOD,
46-
true,
47-
$this->getValidationParameters($this->fields, false)
48-
);
47+
if (in_array('C', $this->crudOptions)) {
48+
$this->createRequest(
49+
self::CREATE_METHOD,
50+
false,
51+
$this->getValidationParameters($this->fields, true)
52+
);
53+
}
4954

50-
$this->createRequest(
51-
self::SEARCH_METHOD,
52-
false,
53-
$this->getSearchValidationParameters()
54-
);
55+
if (in_array('U', $this->crudOptions)) {
56+
$this->createRequest(
57+
self::UPDATE_METHOD,
58+
true,
59+
$this->getValidationParameters($this->fields, false)
60+
);
61+
}
5562
}
5663

5764
protected function createRequest($method, $needToValidate = true, $parameters = [])

src/Generators/TestsGenerator.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,25 @@ protected function generateExistedEntityFixture()
179179
$entity = Str::snake($this->model);
180180

181181
foreach (self::FIXTURE_TYPES as $type => $modifications) {
182-
foreach ($modifications as $modification) {
183-
$excepts = [];
184-
if ($modification === 'request') {
185-
$excepts = ['id'];
182+
if ($this->isFixtureNeeded($type)) {
183+
foreach ($modifications as $modification) {
184+
$excepts = [];
185+
if ($modification === 'request') {
186+
$excepts = ['id'];
187+
}
188+
$this->generateFixture("{$type}_{$entity}_{$modification}.json", Arr::except($object, $excepts));
186189
}
187-
$this->generateFixture("{$type}_{$entity}_{$modification}.json", Arr::except($object, $excepts));
188190
}
189191
}
190192
}
191193

194+
protected function isFixtureNeeded($type)
195+
{
196+
$firstLetter = strtoupper($type[0]);
197+
198+
return in_array($firstLetter, $this->crudOptions);
199+
}
200+
192201
protected function generateFixture($fixtureName, $data)
193202
{
194203
$fixturePath = $this->getFixturesPath($fixtureName);

0 commit comments

Comments
 (0)