Skip to content

Commit c04e8c6

Browse files
authored
Merge pull request #4 from adhenrique/develop
new features and fix bugs
2 parents 1367389 + 72f15aa commit c04e8c6

File tree

12 files changed

+219
-21
lines changed

12 files changed

+219
-21
lines changed

src/Builder.php

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace LaravelDomainOriented;
44

55
use Carbon\Carbon;
6-
use Illuminate\Filesystem\Filesystem;
76
use Illuminate\Support\Collection;
7+
use Illuminate\Support\Facades\File;
88
use Illuminate\Support\Str;
99

1010
class Builder
1111
{
12-
private Filesystem $filesystem;
1312
private array $filesPaths = [
1413
'Controller' => 'app/Http/Controllers/',
1514
'Resource' => 'app/Domain/%s/',
@@ -22,14 +21,10 @@ class Builder
2221
'Migration' => 'database/migrations/',
2322
'Factory' => 'database/factories/',
2423
'Seeder' => 'database/seeders/',
24+
'Policy' => 'app/Domain/%s/',
2525
];
2626
private Collection $names;
2727

28-
public function __construct(Filesystem $filesystem)
29-
{
30-
$this->filesystem = $filesystem;
31-
}
32-
3328
public function prepare(): array
3429
{
3530
$exists = [];
@@ -41,6 +36,8 @@ public function prepare(): array
4136
}
4237
}
4338

39+
clearstatcache();
40+
4441
return array_filter($exists);
4542
}
4643

@@ -49,18 +46,22 @@ public function run(): void
4946
foreach ($this->filesPaths as $stubName => $finalPath) {
5047
$this->createFile($stubName, $finalPath);
5148
}
49+
50+
$this->insertDomain();
5251
}
5352

5453
public function clear()
5554
{
5655
foreach ($this->filesPaths as $stubName => $finalPath) {
5756
$this->removeFile($stubName, $finalPath);
5857
}
58+
59+
$this->removeDomain();
5960
}
6061

6162
public function createFile(string $stubName, string $finalPath): void
6263
{
63-
$stubFile = $this->filesystem->get($this->getStub($stubName));
64+
$stubFile = File::get($this->getStub($stubName));
6465
$content = $this->replacePlaceholders($stubFile);
6566
$path = base_path(sprintf($finalPath, $this->getDomainName()));
6667
$fileName = $this->getName('singularName', 'Dummy') . $stubName . '.php';
@@ -71,7 +72,7 @@ public function createFile(string $stubName, string $finalPath): void
7172
}
7273

7374
$file = $path.$fileName;
74-
$this->filesystem->put($file, $content);
75+
File::put($file, $content);
7576
}
7677

7778
// todo - we can use glob function as well, for the other files
@@ -83,10 +84,10 @@ public function removeFile(string $stubName, string $finalPath)
8384
$file = $path.$fileName;
8485

8586
if ($stubName === 'Migration') {
86-
$file = $this->filesystem->glob($path.'*_create_'.$this->getName('tableName').'_table.php');
87+
$file = File::glob($path.'*_create_'.$this->getName('tableName').'_table.php');
8788
}
8889

89-
$this->filesystem->delete($file);
90+
File::delete($file);
9091
}
9192

9293
public function getStub($stubName): string
@@ -144,7 +145,7 @@ public function replacePlaceholders($stubFile)
144145
private function checkMigrationExists(): string
145146
{
146147
$migrationPath = base_path('database/migrations');
147-
$migrationFiles = $this->filesystem->glob($migrationPath.'/*.php');
148+
$migrationFiles = File::glob($migrationPath.'/*.php');
148149

149150
$exists = false;
150151

@@ -163,11 +164,36 @@ private function checkClassExists(string $stubName, string $finalPath): string
163164
$fileName = $this->getName('singularName', 'Dummy') . $stubName . '.php';
164165
$file = $path.$fileName;
165166

166-
return $this->filesystem->exists($file) ? $file : false;
167+
return File::exists($file) ? $file : false;
167168
}
168169

169170
public function createDomainFolder()
170171
{
171-
$this->filesystem->ensureDirectoryExists($this->getDomainFolder());
172+
File::ensureDirectoryExists($this->getDomainFolder());
173+
}
174+
175+
public function insertDomain(): void
176+
{
177+
$path = app_path('domains.php');
178+
$domains = require ($path);
179+
180+
array_push($domains, $this->getDomainName());
181+
$domains = array_unique($domains);
182+
183+
$content = "<?php \n\nreturn " . preg_replace("/[0-9]+ \=\>/i", '',var_export($domains, true)). ';';
184+
File::replace($path, $content);
185+
}
186+
187+
public function removeDomain()
188+
{
189+
$path = app_path('domains.php');
190+
$domains = require ($path);
191+
192+
if (($key = array_search($this->getDomainName(), $domains)) !== false) {
193+
unset($domains[$key]);
194+
}
195+
196+
$content = "<?php \n\nreturn " . preg_replace("/[0-9]+ \=\>/i", '',var_export($domains, true)). ';';
197+
File::replace($path, $content);
172198
}
173199
}

src/Commands/Command.php

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

33
namespace LaravelDomainOriented\Commands;
44

5-
use Illuminate\Filesystem\Filesystem;
65
use LaravelDomainOriented\Builder;
76

87
class Command extends \Illuminate\Console\Command
98
{
109
protected Builder $builder;
1110

12-
public function __construct(Filesystem $filesystem)
11+
public function __construct()
1312
{
1413
parent::__construct();
15-
$this->builder = new Builder($filesystem);
14+
$this->builder = new Builder();
1615
}
1716

1817
protected function getNameInput(): string

src/Controller/Controller.php

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

33
namespace LaravelDomainOriented\Controller;
44

5+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6+
use Illuminate\Foundation\Bus\DispatchesJobs;
57
use Illuminate\Http\JsonResponse;
68
use Illuminate\Http\Request;
79
use Illuminate\Routing\Controller as BaseController;
@@ -11,6 +13,8 @@
1113

1214
class Controller extends BaseController
1315
{
16+
use AuthorizesRequests, DispatchesJobs;
17+
1418
protected SearchService $searchService;
1519

1620
protected PersistenceService $persistenceService;
@@ -26,12 +30,16 @@ public function response(array $data = [], $status = 200): JsonResponse
2630

2731
public function index(Request $request)
2832
{
33+
$this->authorize('index', $this->searchService->getModel());
34+
2935
$data = $this->searchService->all($request);
3036
return $this->resource::collection($data);
3137
}
3238

3339
public function show(Request $request, int $id)
3440
{
41+
$this->authorize('show', $this->searchService->getModel());
42+
3543
$request = $request->merge(['id' => $id]);
3644
$validatedData = $this->validateService->handle($request->all(), ValidateService::SHOW);
3745

@@ -41,13 +49,17 @@ public function show(Request $request, int $id)
4149

4250
public function store(Request $request): JsonResponse
4351
{
52+
$this->authorize('store', $this->searchService->getModel());
53+
4454
$validatedData = $this->validateService->handle($request->all(), ValidateService::STORE);
4555
$id = $this->persistenceService->store($validatedData);
4656
return $this->response(['id' => $id]);
4757
}
4858

4959
public function update(Request $request, $id): JsonResponse
5060
{
61+
$this->authorize('update', $this->searchService->getModel());
62+
5163
$request = $request->merge(['id' => $id]);
5264
$validatedData = $this->validateService->handle($request->all(), ValidateService::UPDATE);
5365

@@ -57,6 +69,8 @@ public function update(Request $request, $id): JsonResponse
5769

5870
public function destroy(Request $request, int $id): JsonResponse
5971
{
72+
$this->authorize('destroy', $this->searchService->getModel());
73+
6074
$request = $request->merge(['id' => $id]);
6175
$validatedData = $this->validateService->handle($request->all(), ValidateService::DESTROY);
6276

src/Policy/Policy.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace LaravelDomainOriented\Policy;
4+
5+
use Illuminate\Auth\Access\HandlesAuthorization;
6+
use Illuminate\Contracts\Auth\Authenticatable as AuthUser;
7+
8+
class Policy
9+
{
10+
use HandlesAuthorization;
11+
12+
public function before(?AuthUser $user, string $ability): bool
13+
{
14+
return true;
15+
}
16+
17+
public function index(AuthUser $user): bool
18+
{
19+
return true;
20+
}
21+
22+
public function show(AuthUser $user): bool
23+
{
24+
return true;
25+
}
26+
27+
public function store(AuthUser $user): bool
28+
{
29+
return true;
30+
}
31+
32+
public function update(AuthUser $user): bool
33+
{
34+
return true;
35+
}
36+
37+
public function destroy(AuthUser $user): bool
38+
{
39+
return true;
40+
}
41+
}
42+

src/ServiceProvider.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace LaravelDomainOriented;
44

5+
use Illuminate\Support\Facades\File;
6+
use Illuminate\Support\Facades\Gate;
57
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
68
use LaravelDomainOriented\Commands\CreateDomain;
79
use LaravelDomainOriented\Commands\RemoveDomain;
@@ -19,10 +21,34 @@ public function boot()
1921
CreateDomain::class,
2022
RemoveDomain::class,
2123
]);
24+
25+
$this->createDomainsFile();
26+
$this->registerPolicies();
2227
}
2328

2429
public function register()
2530
{
2631
//
2732
}
33+
34+
private function registerPolicies()
35+
{
36+
$domainNames = require (app_path('domains.php'));
37+
38+
foreach ($domainNames as $domainName) {
39+
$policy = 'App\\Domain\\'.$domainName.'\\'.$domainName.'Policy';
40+
Gate::policy('Illuminate\Database\Eloquent\Model', $policy);
41+
}
42+
}
43+
44+
private function createDomainsFile()
45+
{
46+
$filePath = app_path('domains.php');
47+
$exists = File::exists($filePath);
48+
$stubFile = File::get(__DIR__.'/Stubs/domains.stub');
49+
50+
if (!$exists) {
51+
File::put($filePath, $stubFile);
52+
}
53+
}
2854
}

src/Services/SearchService.php

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

33
namespace LaravelDomainOriented\Services;
44

5+
use Illuminate\Contracts\Auth\Guard;
6+
use Illuminate\Database\Eloquent\Builder;
57
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Auth;
69
use LaravelDomainOriented\Models\SearchModel;
710

811
class SearchService
@@ -19,7 +22,9 @@ public function __construct(SearchModel $model, FilterService $filterService)
1922

2023
public function all(Request $request)
2124
{
22-
$builder = $this->filterService->apply($this->model->query(), $request);
25+
$builder = $this->beforeSearch($this->model->query(), Auth::guard());
26+
27+
$builder = $this->filterService->apply($builder, $request);
2328
$paginate = $request->get('paginate');
2429

2530
$page = $paginate['page'] ?? 0;
@@ -34,6 +39,18 @@ public function all(Request $request)
3439

3540
public function findById(int $id)
3641
{
37-
return $this->model->findOrFail($id);
42+
$builder = $this->beforeSearch($this->model->query(), Auth::guard());
43+
44+
return $builder->findOrFail($id);
45+
}
46+
47+
public function getModel(): SearchModel
48+
{
49+
return $this->model;
50+
}
51+
52+
public function beforeSearch(Builder $builder, Guard $auth): Builder
53+
{
54+
return $builder;
3855
}
3956
}

src/Stubs/Policy.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Domain\{{singularName}};
4+
5+
use LaravelDomainOriented\Policy\Policy;
6+
7+
class {{singularName}}Policy extends Policy
8+
{
9+
//
10+
}

src/Stubs/domains.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return array();

tests/Feature/CreateDomainTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CreateDomainTest extends BasicTestCase
2828
'Migration' => 'database/migrations/',
2929
'Factory' => 'database/factories/',
3030
'Seeder' => 'database/seeders/',
31+
'Policy' => 'app/Domain/%s/',
3132
];
3233
private string $domainName = 'Test';
3334
private string $tableName = '';

tests/Feature/RemoveDomainTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class RemoveDomainTest extends BasicTestCase
2828
'Migration' => 'database/migrations/',
2929
'Factory' => 'database/factories/',
3030
'Seeder' => 'database/seeders/',
31+
'Policy' => 'app/Domain/%s/',
3132
];
3233

3334
public function setUp(): void

0 commit comments

Comments
 (0)