1 | <?php |
||
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) |
|
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 | 7 | if (! $modelInstance instanceof EloquentModel) { |
|
38 | 1 | $message = "Target [$modelName] is not an Illuminate\Database\Eloquent\Model instance."; |
|
39 | |||
40 | 1 | 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) |
|
90 | } |
||
91 |