Skip to content

Commit ba9ea6a

Browse files
authored
Merge pull request #2 from webfactor/2.0
2.0
2 parents 3cb5180 + 9132c5f commit ba9ea6a

37 files changed

+981
-202
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2121
### Security
2222
- Nothing
2323

24+
## 2.0.0 - 2018-03-23
25+
26+
### Added
27+
- `LanguageService` generates models.php translation file (if not exists) and fill singular/plural translation
28+
- `OpenIdeService` opens all generated file with PhpStorm if command is called with `--ide={ide}`
29+
- `RouteService` adds Backpack Crud route to admin.php
30+
31+
### Changed
32+
33+
- `BackpackCrudModelService` will fill `$fillable` in Model automatically from scheme (if given)
34+
- `BackpackCrudRequestService` will fill `rules()` in Request automatically from scheme (if given)
35+
- `BackpackCrudControllerService` will add CrudFields and CrudColumns in Controller automatically from scheme (if given) - very rudimentary for now, more functionality planned
36+
2437
## 1.2.0 - 2018-03-18
2538

2639
### Added

README.md

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,71 @@
88
[![Quality Score][ico-code-quality]][link-code-quality]
99
[![Total Downloads][ico-downloads]][link-downloads]
1010

11+
This is a package developed by us for internal use. It is supposed to help us during development and save plenty of time by automating many steps while creating typical CRUD entities with [Laravel Backpack](https://laravel-backpack.readme.io/docs). You can write your own Services (they have to implement `Webfactor\Laravel\Generators\Contracts\ServiceInterface`) and register them in the `generators.php` config file, or use this package as an inspiration for your own implementation.
12+
1113
## Install
1214

13-
Via Composer
15+
### Via Composer
16+
17+
This package is indended to be used only for development, not for production. Because of that we recommend to use `require-dev`:
1418

1519
``` bash
16-
$ composer require webfactor/laravel-generators
20+
composer require-dev webfactor/laravel-generators
1721
```
1822

1923
## Usage
2024

2125
``` bash
22-
php artisan make:entity {entity_name}
26+
php artisan make:entity {entity_name} {--schema=} {--ide=}
2327
```
2428

25-
Use *singular* for entity. This will automatically create (while respecting naming conventions):
29+
`--schema` currently uses syntax from [Laravel 5 Extended Generators](https://github.com/laracasts/Laravel-5-Generators-Extended)
30+
31+
Use *singular* for entity. This will automatically create (while respecting our internal naming conventions):
2632

2733
* Migration
2834
* Factory
2935
* Seeder
30-
* Backpack CRUD (Model, Controller, Request)
36+
* Backpack CRUD (modified Backpack Generator):
37+
* Model (incl. `$fillable`)
38+
* Request (incl. `rules()`)
39+
* Controller (incl. CrudColumns and CrudFields, basic for now)
40+
* Language File
41+
* Route to Backpack CRUD in admin.php
3142

32-
## Change log
43+
### Open files in IDE
3344

34-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
45+
With `{--ide=}` option you can define your preferred IDE to open all automatically generated files. This package comes with an implementation for PhpStorm (`Webfactor\Laravel\Generators\RecipesPhpStormOpener`) which is defined in `generators.php`. The keys in the `ides`-Array are possible values for the command option. You can add other IDE-Opener classes, they have to implement `Webfactor\Laravel\Generators\Contracts\OpenInIdeInterface`.`
3546

36-
## Testing
47+
In your service class you have to define the path to the file generated by this service. Then add `$this->addLatestFileToIdeStack();` and all files of the stack will be opened by `Webfactor\Laravel\Generators\Services\OpenIdeService` (should be placed at the end of the `services`-array in `generators.php)
3748

38-
``` bash
39-
$ composer test
49+
Example:
50+
51+
```php
52+
<?php
53+
54+
class ExampleService extends ServiceAbstract implements ServiceInterface
55+
{
56+
protected $relativeToBasePath = 'path/to/file';
57+
58+
public function call()
59+
{
60+
// do some magic
61+
62+
// searches for latest file (using modified date) in given directory and adds it to the stack
63+
$this->addLatestFileToIdeStack();
64+
}
65+
}
4066
```
4167

68+
## Adaption
69+
70+
Feel free to write your own Services that fit your purposes!
71+
72+
## Change log
73+
74+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
75+
4276
## Contributing
4377

4478
Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) for details.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
]
4949
},
5050
"branch-alias": {
51-
"dev-master": "1.0-dev"
51+
"dev-master": "2.0-dev"
5252
}
5353
},
5454
"config": {

config/generators.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@
66
* have to implement Webfactor\Laravel\Generators\Contracts\MakeServiceInterface.
77
*/
88
'services' => [
9-
Webfactor\Laravel\Generators\Services\MakeBackpackCrudModelService::class,
10-
Webfactor\Laravel\Generators\Services\MakeBackpackCrudControllerService::class,
11-
Webfactor\Laravel\Generators\Services\MakeBackpackCrudRequestService::class,
12-
Webfactor\Laravel\Generators\Services\MakeMigrationService::class,
13-
Webfactor\Laravel\Generators\Services\MakeFactoryService::class,
14-
Webfactor\Laravel\Generators\Services\MakeSeederService::class,
9+
Webfactor\Laravel\Generators\Services\MigrationService::class,
10+
Webfactor\Laravel\Generators\Services\LanguageService::class,
11+
Webfactor\Laravel\Generators\Services\BackpackCrudModelService::class,
12+
Webfactor\Laravel\Generators\Services\BackpackCrudControllerService::class,
13+
Webfactor\Laravel\Generators\Services\BackpackCrudRequestService::class,
14+
Webfactor\Laravel\Generators\Services\FactoryService::class,
15+
Webfactor\Laravel\Generators\Services\SeederService::class,
16+
Webfactor\Laravel\Generators\Services\RouteService::class,
17+
Webfactor\Laravel\Generators\Services\OpenIdeService::class,
18+
],
19+
20+
/*
21+
* Recipe classes for opening all generated files directly in IDE if the option --ide={key}
22+
* is used. Have to implement Webfactor\Laravel\Generators\Contracts\OpenIdeInterface.
23+
*/
24+
'ides' => [
25+
'pstorm' => Webfactor\Laravel\Generators\Recipes\PhpStormOpener::class,
1526
],
1627
];

src/Commands/MakeBackpackCrudController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ protected function replaceNameStrings(&$stub, $name)
8383
$table = str_plural(ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_'));
8484

8585
$stub = str_replace('DummyTable', $table, $stub);
86+
$stub = str_replace('dummy_name', snake_case($this->argument('name')), $stub);
8687
$stub = str_replace('dummy_class', strtolower(str_replace($this->getNamespace($name).'\\', '', $name)), $stub);
8788

8889
return $this;
@@ -99,7 +100,9 @@ protected function buildClass($name)
99100
{
100101
$stub = $this->files->get($this->getStub());
101102

102-
return $this->replaceNamespace($stub, $name)->replaceNameStrings($stub, $name)->replaceClass($stub, $name);
103+
return $this->replaceNamespace($stub, $name)
104+
->replaceNameStrings($stub, $name)
105+
->replaceClass($stub, $name);
103106
}
104107

105108
/**

src/Commands/MakeEntity.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,69 @@
33
namespace Webfactor\Laravel\Generators\Commands;
44

55
use Illuminate\Console\Command;
6+
use Symfony\Component\Finder\SplFileInfo;
67
use Webfactor\Laravel\Generators\MakeServices;
8+
use Webfactor\Laravel\Generators\Schemas\MigrationSchema;
79

810
class MakeEntity extends Command
911
{
12+
/**
13+
* Paths to files which should automatically be opened in IDE if the
14+
* option --ide is set (and IDE capable).
15+
*
16+
* @var array
17+
*/
18+
public $filesToBeOpened = [];
19+
20+
/**
21+
* The name of the entity being created.
22+
*
23+
* @var string
24+
*/
25+
public $entity;
26+
27+
/**
28+
* The migration schema object.
29+
*
30+
* @var MigrationSchema
31+
*/
32+
public $schema;
33+
1034
/**
1135
* The name and signature of the console command.
1236
*
1337
* @var string
1438
*/
15-
protected $signature = 'make:entity {entity} {--schema=}';
39+
protected $signature = 'make:entity {entity} {--schema="name:string"} {--migrate} {--ide=}';
40+
1641
/**
1742
* The console command description.
1843
*
1944
* @var string
2045
*/
2146
protected $description = 'Make Entity';
47+
2248
/**
23-
* Create a new command instance.
49+
* Execute the console command.
2450
*
25-
* @return void
51+
* @return mixed
2652
*/
27-
public function __construct()
53+
public function handle()
2854
{
29-
parent::__construct();
55+
$this->entity = $this->argument('entity');
56+
$this->schema = new MigrationSchema($this->option('schema'));
57+
58+
(new MakeServices($this))->call();
3059
}
3160

3261
/**
33-
* Execute the console command.
62+
* Adds file to $filesToBeOpened stack.
3463
*
35-
* @return mixed
64+
* @param $file
65+
* @return void
3666
*/
37-
public function handle()
67+
public function addFile(SplFileInfo $file): void
3868
{
39-
(new MakeServices($this))->call();
69+
array_push($this->filesToBeOpened, $file);
4070
}
4171
}

src/Contracts/MakeServiceAbstract.php

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Contracts;
4+
5+
use Webfactor\Laravel\Generators\Commands\MakeEntity;
6+
7+
abstract class OpenInIdeAbstract
8+
{
9+
protected $files;
10+
11+
public function __construct(array $files)
12+
{
13+
$this->files = $files;
14+
}
15+
}

src/Contracts/MakeServiceInterface.php renamed to src/Contracts/OpenInIdeInterface.php

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

33
namespace Webfactor\Laravel\Generators\Contracts;
44

5-
interface MakeServiceInterface
5+
interface OpenInIdeInterface
66
{
7-
public function make();
7+
public function open();
88
}

src/Contracts/ServiceAbstract.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Contracts;
4+
5+
use Illuminate\Filesystem\Filesystem;
6+
use Symfony\Component\Finder\SplFileInfo;
7+
use Webfactor\Laravel\Generators\Commands\MakeEntity;
8+
9+
abstract class ServiceAbstract
10+
{
11+
protected $command;
12+
13+
protected $filesystem;
14+
15+
protected $relativeToBasePath;
16+
17+
public function __construct(MakeEntity $command)
18+
{
19+
$this->command = $command;
20+
$this->filesystem = new Filesystem();
21+
}
22+
23+
protected function addLatestFileToIdeStack()
24+
{
25+
$this->command->addFile($this->latestCreatedFileIn(base_path($this->relativeToBasePath)));
26+
}
27+
28+
/**
29+
* Returns the latest created File in given Path to use it for $filesToBeOpened stack
30+
*
31+
* @param string $path
32+
*
33+
* @return SplFileInfo
34+
*/
35+
protected function latestCreatedFileIn(string $path): SplFileInfo
36+
{
37+
$sortedByMTime = array_sort($this->filesystem->files($path), function ($file) {
38+
return $file->getMTime();
39+
});
40+
41+
return end($sortedByMTime);
42+
}
43+
}

0 commit comments

Comments
 (0)