Skip to content

Commit 98c7376

Browse files
committed
[RF/EH] refactor almost everything
1 parent 759c20e commit 98c7376

12 files changed

+83
-32
lines changed

config/generators.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
* have to implement Webfactor\Laravel\Generators\Contracts\MakeServiceInterface.
77
*/
88
'services' => [
9-
/*Webfactor\Laravel\Generators\Services\MakeMigrationService::class,
10-
Webfactor\Laravel\Generators\Services\MakeLanguageService::class,
11-
Webfactor\Laravel\Generators\Services\MakeBackpackCrudModelService::class,
12-
Webfactor\Laravel\Generators\Services\MakeBackpackCrudControllerService::class,
13-
Webfactor\Laravel\Generators\Services\MakeBackpackCrudRequestService::class,
14-
Webfactor\Laravel\Generators\Services\MakeFactoryService::class,
15-
Webfactor\Laravel\Generators\Services\MakeSeederService::class,
16-
Webfactor\Laravel\Generators\Services\MakeRouteService::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,
1717
Webfactor\Laravel\Generators\Services\OpenIdeService::class,
1818
],
1919

src/Commands/MakeEntity.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Webfactor\Laravel\Generators\Commands;
44

55
use Illuminate\Console\Command;
6+
use Symfony\Component\Finder\SplFileInfo;
67
use Webfactor\Laravel\Generators\MakeServices;
78
use Webfactor\Laravel\Generators\MigrationSchema;
89

@@ -56,4 +57,15 @@ public function handle()
5657

5758
(new MakeServices($this))->call();
5859
}
60+
61+
/**
62+
* Adds file to $filesToBeOpened stack.
63+
*
64+
* @param $file
65+
* @return void
66+
*/
67+
public function addFile(SplFileInfo $file): void
68+
{
69+
array_push($this->filesToBeOpened, $file);
70+
}
5971
}

src/Contracts/ServiceAbstract.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,42 @@
22

33
namespace Webfactor\Laravel\Generators\Contracts;
44

5+
use Illuminate\Filesystem\Filesystem;
6+
use Symfony\Component\Finder\SplFileInfo;
57
use Webfactor\Laravel\Generators\Commands\MakeEntity;
68

79
abstract class ServiceAbstract
810
{
911
protected $command;
1012

13+
protected $filesystem;
14+
15+
protected $relativeToBasePath;
16+
1117
public function __construct(MakeEntity $command)
1218
{
1319
$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);
1442
}
1543
}

src/Recipes/PhpStormOpener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class PhpStormOpener extends OpenInIdeAbstract implements OpenInIdeInterface
1010
public function open()
1111
{
1212
foreach ($this->files as $file) {
13-
exec('pstorm ' . $file);
13+
exec('pstorm ' . $file->getPathname());
1414
}
1515
}
1616
}

src/Services/BackpackCrudControllerService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
class BackpackCrudControllerService extends ServiceAbstract implements ServiceInterface
99
{
10+
protected $relativeToBasePath = 'app/Http/Controllers/Admin';
11+
1012
public function call()
1113
{
1214
$this->command->call('make:crud-controller', [
1315
'name' => $this->getName($this->command->entity),
1416
]);
17+
18+
$this->addLatestFileToIdeStack();
1519
}
1620

1721
public function getName(string $entity): string

src/Services/BackpackCrudModelService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
class BackpackCrudModelService extends ServiceAbstract implements ServiceInterface
99
{
10+
protected $relativeToBasePath = 'app/Models';
11+
1012
public function call()
1113
{
1214
$this->command->call('make:crud-model', [
1315
'name' => $this->getName($this->command->entity),
1416
]);
17+
18+
$this->addLatestFileToIdeStack();
1519
}
1620

1721
public function getName(string $entity): string

src/Services/BackpackCrudRequestService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
class BackpackCrudRequestService extends ServiceAbstract implements ServiceInterface
99
{
10+
protected $relativeToBasePath = 'app/Http/Requests/Admin';
11+
1012
public function call()
1113
{
1214
$this->command->call('make:crud-request', [
1315
'name' => $this->getName($this->command->entity),
1416
]);
17+
18+
$this->addLatestFileToIdeStack();
1519
}
1620

1721
public function getName(string $entity): string

src/Services/FactoryService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77

88
class FactoryService extends ServiceAbstract implements ServiceInterface
99
{
10+
protected $relativeToBasePath = 'database/factories';
11+
1012
public function call()
1113
{
1214
$this->command->call('make:factory', [
1315
'name' => $this->getName($this->command->entity),
1416
'--model' => 'Models\\' . $this->getModelName($this->command->entity),
1517
]);
18+
19+
$this->addLatestFileToIdeStack();
1620
}
1721

1822
public function getName(string $entity): string

src/Services/LanguageService.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,15 @@ class LanguageService extends ServiceAbstract implements ServiceInterface
1515

1616
private $currentLanguage;
1717

18-
private $files;
19-
20-
public function __construct(MakeEntity $command)
21-
{
22-
parent::__construct($command);
23-
24-
$this->files = new Filesystem();
25-
}
26-
2718
public function call()
2819
{
2920
$this->currentLanguage = \Lang::locale();
21+
$this->relativeToBasePath = 'resources/lang/' . $this->currentLanguage;
3022
$this->languageFile = $this->getFilePath();
3123

3224
$this->writeFile($this->getName($this->command->entity));
25+
26+
$this->addLatestFileToIdeStack();
3327
}
3428

3529
public function getName(string $entity): string
@@ -46,7 +40,7 @@ public function getName(string $entity): string
4640
*/
4741
private function writeFile($name)
4842
{
49-
if ($this->files->exists($this->languageFile)) {
43+
if ($this->filesystem->exists($this->languageFile)) {
5044
$this->translation = include $this->languageFile;
5145
}
5246

@@ -55,7 +49,7 @@ private function writeFile($name)
5549
'plural' => ucfirst(str_plural($name)),
5650
]);
5751

58-
return $this->files->put($this->languageFile, $this->getTranslationFileContent());
52+
return $this->filesystem->put($this->languageFile, $this->getTranslationFileContent());
5953
}
6054

6155
private function getFilePath()

src/Services/MigrationService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88

99
class MigrationService extends ServiceAbstract implements ServiceInterface
1010
{
11+
protected $relativeToBasePath = 'database/migrations';
12+
1113
public function call()
1214
{
13-
dd($this->command->schema);
14-
1515
$this->command->call('make:migration:schema', [
1616
'name' => $this->getName($this->command->entity),
1717
'--model' => 0,
1818
'--schema' => $this->command->option('schema'),
1919
]);
20+
21+
$this->addLatestFileToIdeStack();
2022
}
2123

2224
public function getName(string $entity): string

0 commit comments

Comments
 (0)