1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Port\Repository\Abstracts; |
4
|
|
|
|
5
|
|
|
use Prettus\Repository\Contracts\CacheableInterface as PrettusCacheableInterface; |
6
|
|
|
use Prettus\Repository\Criteria\RequestCriteria as PrettusRequestCriteria; |
7
|
|
|
use Prettus\Repository\Eloquent\BaseRepository as PrettusRepository; |
8
|
|
|
use Prettus\Repository\Traits\CacheableRepository as PrettusCacheableRepository; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Repository. |
12
|
|
|
* |
13
|
|
|
* @author Mahmoud Zalt <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class Repository extends PrettusRepository implements PrettusCacheableInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
use PrettusCacheableRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Boot up the repository, pushing criteria. |
22
|
|
|
*/ |
23
|
|
|
public function boot() |
24
|
|
|
{ |
25
|
|
|
$this->pushCriteria(app(PrettusRequestCriteria::class)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* This function relies on the convention. |
30
|
|
|
* Conventions: |
31
|
|
|
* - Repository name should be same like it's model name (model: Foo -> repository: FooRepository). |
32
|
|
|
* - If the container contains Models with names different than the container name, the repository class must |
33
|
|
|
* set `$container='ContainerName'` property for this function to work properly |
34
|
|
|
* Specify Model class name. |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function model() |
39
|
|
|
{ |
40
|
|
|
// 1_ get the full namespace of the child class who's extending this class. |
41
|
|
|
// 2_ remove the namespace and keep the class name |
42
|
|
|
// 3_ remove the word Repository from the class name |
43
|
|
|
// 4_ check if the container name is set on the repository to indicate that the |
44
|
|
|
// model has different name than the container holding it |
45
|
|
|
// 5_ build the namespace of the Model based on the conventions |
46
|
|
|
|
47
|
|
|
$fullName = get_called_class(); |
48
|
|
|
$className = substr($fullName, strrpos($fullName, '\\') + 1); |
49
|
|
|
$classOnly = str_replace('Repository', '', $className); |
50
|
|
|
$container = isset($this->container) ? $this->container : $classOnly; |
51
|
|
|
$modelNamespace = 'App\Containers\\' . $container . '\\Models\\' . $classOnly; |
52
|
|
|
|
53
|
|
|
return $modelNamespace; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|