1
|
|
|
<?php |
2
|
|
|
namespace ANavallaSuiza\Laravel\Database\Manager\Eloquent; |
3
|
|
|
|
4
|
|
|
use ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager as ModelManagerContract; |
5
|
|
|
use ANavallaSuiza\Laravel\Database\Contracts\Repository\HasCustomRepository; |
6
|
|
|
use ANavallaSuiza\Laravel\Database\Contracts\Repository\HasCache; |
7
|
|
|
use ANavallaSuiza\Laravel\Database\Repository\Eloquent\Repository; |
8
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
9
|
|
|
use Illuminate\Contracts\Foundation\Application; |
10
|
|
|
|
11
|
|
|
class ModelManager implements ModelManagerContract |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Application |
15
|
|
|
*/ |
16
|
|
|
private $app; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Application $app |
20
|
|
|
*/ |
21
|
9 |
|
public function __construct(Application $app) |
22
|
|
|
{ |
23
|
9 |
|
$this->app = $app; |
24
|
9 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get Eloquent Model instance |
28
|
|
|
* |
29
|
|
|
* @param string $modelName |
30
|
|
|
* |
31
|
|
|
* @return EloquentModel |
32
|
|
|
*/ |
33
|
7 |
|
public function getModelInstance($modelName) |
34
|
|
|
{ |
35
|
7 |
|
$modelInstance = $this->app->make($modelName); |
36
|
|
|
|
37
|
6 |
|
if (! $modelInstance instanceof EloquentModel) { |
38
|
|
|
$message = "Target [$modelName] is not an Illuminate\Database\Eloquent\Model instance."; |
39
|
|
|
|
40
|
|
|
throw new \Exception($message); |
41
|
|
|
} |
42
|
|
|
|
43
|
6 |
|
return $modelInstance; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
4 |
|
public function getRepository($modelName) |
50
|
|
|
{ |
51
|
4 |
|
$modelInstance = $this->getModelInstance($modelName); |
52
|
|
|
|
53
|
4 |
|
$args = ['model' => $modelInstance]; |
54
|
|
|
|
55
|
4 |
|
if ($modelInstance instanceof HasCustomRepository) { |
56
|
2 |
|
$repository = $this->app->make($modelInstance->repository(), $args); |
57
|
|
|
|
58
|
2 |
|
if (! $repository instanceof Repository) { |
59
|
1 |
|
$message = "The [$modelName] custom repository must extend ANavallaSuiza\Laravel\Database\Repository\Eloquent\Repository."; |
60
|
|
|
|
61
|
1 |
|
throw new \Exception($message); |
62
|
|
|
} |
63
|
1 |
|
} else { |
64
|
2 |
|
$repository = $this->app->make('ANavallaSuiza\Laravel\Database\Repository\Eloquent\Repository', $args); |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
if ($modelInstance instanceof HasCache && $modelInstance->cache() === true) { |
68
|
1 |
|
return $this->app->make('ANavallaSuiza\Laravel\Database\Repository\Eloquent\Cache', [ |
69
|
1 |
|
'repository' => $repository, |
70
|
1 |
|
'cache' => $this->app['cache.store'] |
71
|
1 |
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
return $repository; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
1 |
|
public function getAbstractionLayer($modelName) |
81
|
|
|
{ |
82
|
1 |
|
$modelInstance = $this->getModelInstance($modelName); |
83
|
|
|
|
84
|
1 |
|
$args = ['model' => $modelInstance]; |
85
|
|
|
|
86
|
1 |
|
$dbal = $this->app->make('ANavallaSuiza\Laravel\Database\Dbal\Eloquent\AbstractionLayer', $args); |
87
|
|
|
|
88
|
1 |
|
return $dbal; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|