Skip to content

Commit ca0c1a5

Browse files
committed
Added criteria system into repository
1 parent 6da646f commit ca0c1a5

13 files changed

+344
-23
lines changed

config/repository.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
|--------------------------------------------------------------------------
1919
|
2020
*/
21-
'cache' => [
21+
'cache' => [
2222
/*
2323
|--------------------------------------------------------------------------
2424
| Cache Status
@@ -27,7 +27,7 @@
2727
| Enable or disable cache
2828
|
2929
*/
30-
'enabled' => false,
30+
'enabled' => false,
3131

3232
/*
3333
|--------------------------------------------------------------------------
@@ -37,7 +37,7 @@
3737
| Time of expiration cache
3838
|
3939
*/
40-
'minutes' => 30,
40+
'minutes' => 60,
4141

4242
/*
4343
|--------------------------------------------------------------------------
@@ -57,7 +57,7 @@
5757
|
5858
|
5959
*/
60-
'clean' => [
60+
'clean' => [
6161

6262
/*
6363
|--------------------------------------------------------------------------
@@ -77,14 +77,14 @@
7777
| delete : Clear Cache on delete Entry in repository
7878
|
7979
*/
80-
'on' => [
80+
'on' => [
8181
'create' => true,
8282
'update' => true,
8383
'delete' => true,
8484
]
8585
],
8686

87-
'params' => [
87+
'params' => [
8888
/*
8989
|--------------------------------------------------------------------------
9090
| Skip Cache Params
@@ -112,8 +112,8 @@
112112
|
113113
| 'except' =>['find'],
114114
*/
115-
'allowed' => [
116-
'only' => null,
115+
'allowed' => [
116+
'only' => ['all', 'paginate', 'find', 'findByField', 'findWhere', 'getByCriteria'],
117117
'except' => null
118118
]
119119
],

src/CacheRepositoryServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodeOfDigital\CacheRepository;
44

5+
use CodeOfDigital\CacheRepository\Commands\CriteriaCommand;
56
use CodeOfDigital\CacheRepository\Commands\RepositoryCommand;
67
use Illuminate\Support\ServiceProvider;
78

@@ -24,7 +25,9 @@ public function register()
2425
{
2526
$this->app->register(EventServiceProvider::class);
2627

27-
if ($this->app->runningInConsole())
28+
if ($this->app->runningInConsole()) {
2829
$this->commands([RepositoryCommand::class]);
30+
$this->commands([CriteriaCommand::class]);
31+
}
2932
}
3033
}

src/Commands/CriteriaCommand.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace CodeOfDigital\CacheRepository\Commands;
4+
5+
class CriteriaCommand extends BaseCommand
6+
{
7+
/**
8+
* The name of the command
9+
*
10+
* @var string
11+
*/
12+
protected $signature = 'make:criteria {criteria}';
13+
14+
/**
15+
* The description of command
16+
*
17+
* @var string
18+
*/
19+
protected $description = 'Create a new criteria';
20+
21+
/**
22+
* Stub paths
23+
*
24+
* @var array|string[]
25+
*/
26+
protected array $stubs = [
27+
'criteria' => __DIR__ . '/stubs/criteria.stub'
28+
];
29+
30+
/**
31+
* Name of criteria
32+
*
33+
* @var string
34+
*/
35+
protected string $criteriaName;
36+
37+
public function __construct()
38+
{
39+
parent::__construct();
40+
}
41+
42+
public function handle()
43+
{
44+
$criteria = $this->argument('criteria');
45+
$criteriaParts = explode('\\', $criteria);
46+
$this->criteriaName = $criteriaParts[array_key_last($criteriaParts)];
47+
48+
$content = $this->fileManager->get($this->stubs['criteria']);
49+
50+
$replacements = [
51+
'%namespace%' => "{$this->appNamespace}Criteria\\{$criteria}",
52+
'%criteriaName%' => $this->criteriaName
53+
];
54+
55+
$content = str_replace(array_keys($replacements), array_values($replacements), $content);
56+
57+
$fileName = "{$criteria}";
58+
$fileDirectory = app()->basePath() . "/App/Criteria";
59+
$filePath = "{$fileDirectory}/{$fileName}.php";
60+
61+
if (!$this->fileManager->exists($fileDirectory))
62+
$this->fileManager->makeDirectory($fileDirectory, 0755, true);
63+
64+
if ($this->laravel->runningInConsole() && $this->fileManager->exists($filePath)) {
65+
$response = $this->ask("The criteria [{$this->criteriaName}] has already exists. Do you want to overwrite it?", 'Yes');
66+
67+
if (!$this->isResponsePositive($response)) {
68+
$this->info("The interface [{$this->criteriaName}] will not be overwritten.");
69+
return;
70+
}
71+
}
72+
73+
$this->fileManager->put($filePath, $content);
74+
75+
$this->info("The criteria [{$this->criteriaName}] has been created.");
76+
}
77+
}

src/Commands/RepositoryCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected function createInterface()
118118

119119
$this->fileManager->put($filePath, $content);
120120

121-
$this->info("The interface [{$fileName}] has been created");
121+
$this->info("The interface [{$fileName}] has been created.");
122122

123123
return ["{$this->appNamespace}Repository\\{$this->modelName}\\{$fileName}", $fileName];
124124
}

src/Commands/stubs/criteria.stub

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace %namespace%;
4+
5+
use CodeOfDigital\CacheRepository\Contracts\CriteriaInterface;
6+
use CodeOfDigital\CacheRepository\Contracts\RepositoryInterface;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class %criteriaName% implements CriteriaInterface
10+
{
11+
/**
12+
* Apply conditions into current query building
13+
*
14+
* @param Model $model
15+
* @param RepositoryInterface $repository
16+
* @return mixed
17+
*/
18+
public function apply(Model $model, RepositoryInterface $repository): mixed
19+
{
20+
// TODO: Write your model queries
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace CodeOfDigital\CacheRepository\Contracts;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
interface CriteriaInterface
8+
{
9+
/**
10+
* Apply a query criteria in repository
11+
*
12+
* @param Model $model
13+
* @param RepositoryInterface $repository
14+
* @return mixed
15+
*/
16+
public function apply(Model $model, RepositoryInterface $repository): mixed;
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace CodeOfDigital\CacheRepository\Contracts;
4+
5+
use Illuminate\Support\Collection;
6+
7+
interface RepositoryCriteriaInterface
8+
{
9+
public function pushCriteria($criteria): static;
10+
11+
public function popCriteria($criteria): static;
12+
13+
public function getCriteria(): Collection;
14+
15+
public function getByCriteria(CriteriaInterface $criteria): mixed;
16+
17+
public function skipCriteria(bool $status = true): static;
18+
19+
public function resetCriteria(): static;
20+
}

src/Contracts/RepositoryInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public function simplePaginate($limit = null, $columns = ['*']): mixed;
3333

3434
public function find(int $id, $columns = ['*']): mixed;
3535

36-
public function findByField(string $field, $value, $columns = ['*']): Collection|array;
36+
public function findByField(string $field, $value, $columns = ['*']): mixed;
3737

38-
public function findWhere(array $where, $columns = ['*']): Collection|array;
38+
public function findWhere(array $where, $columns = ['*']): mixed;
3939

40-
public function findWhereIn($field, array $values, $columns = ['*']): Collection|array;
40+
public function findWhereIn($field, array $values, $columns = ['*']): mixed;
4141

42-
public function findWhereNotIn($field, array $values, $columns = ['*']): Collection|array;
42+
public function findWhereNotIn($field, array $values, $columns = ['*']): mixed;
4343

44-
public function findWhereBetween($field, array $values, $columns = ['*']): Collection|array;
44+
public function findWhereBetween($field, array $values, $columns = ['*']): mixed;
4545

4646
public function create(array $attributes): mixed;
4747

src/Contracts/TransformerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44

55
interface TransformerInterface
66
{
7+
public function transformSingle(): array;
8+
79
public function transform(): array;
810
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace CodeOfDigital\CacheRepository\Criteria;
4+
5+
use CodeOfDigital\CacheRepository\Contracts\CriteriaInterface;
6+
use CodeOfDigital\CacheRepository\Contracts\RepositoryInterface;
7+
use CodeOfDigital\CacheRepository\Exceptions\RepositoryException;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\SoftDeletes;
10+
11+
class OnlyTrashedCriteria implements CriteriaInterface
12+
{
13+
/**
14+
* Apply conditions into current query building
15+
*
16+
* @param Model $model
17+
* @param RepositoryInterface $repository
18+
* @return mixed
19+
* @throws RepositoryException
20+
*/
21+
public function apply(Model $model, RepositoryInterface $repository): mixed
22+
{
23+
if (!in_array(SoftDeletes::class, class_uses($model)))
24+
throw new RepositoryException('Model must implement SoftDeletes Trait to use this criteria.');
25+
26+
return $model->onlyTrashed();
27+
}
28+
}

0 commit comments

Comments
 (0)