1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ablunier\Laravel\Database\Manager\Eloquent; |
4
|
|
|
|
5
|
|
|
use Ablunier\Laravel\Database\Contracts\Manager\ModelManager as ModelManagerContract; |
6
|
|
|
use Ablunier\Laravel\Database\Contracts\Repository\HasCache; |
7
|
|
|
use Ablunier\Laravel\Database\Contracts\Repository\HasCustomRepository; |
8
|
|
|
use Ablunier\Laravel\Database\Dbal\Eloquent\AbstractionLayer; |
9
|
|
|
use Ablunier\Laravel\Database\Repository\Eloquent\Cache; |
10
|
|
|
use Ablunier\Laravel\Database\Repository\Eloquent\Repository; |
11
|
|
|
use Illuminate\Contracts\Foundation\Application; |
12
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
13
|
|
|
use Manager\Exceptions\InvalidModelException; |
14
|
|
|
use Manager\Exceptions\InvalidRepositoryException; |
15
|
|
|
|
16
|
|
|
class ModelManager implements ModelManagerContract |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Application |
20
|
|
|
*/ |
21
|
9 |
|
private $app; |
22
|
|
|
|
23
|
9 |
|
/** |
24
|
9 |
|
* @param Application $app |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Application $app) |
27
|
|
|
{ |
28
|
|
|
$this->app = $app; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get Eloquent Model instance. |
33
|
7 |
|
* |
34
|
|
|
* @param string $modelName |
35
|
7 |
|
* |
36
|
|
|
* @throws \Exception |
37
|
7 |
|
* |
38
|
1 |
|
* @return \Illuminate\Database\Eloquent\Model |
39
|
|
|
*/ |
40
|
1 |
|
public function getModelInstance($modelName) |
41
|
|
|
{ |
42
|
|
|
$modelInstance = new $modelName(); |
43
|
6 |
|
|
44
|
|
|
if (!$modelInstance instanceof EloquentModel) { |
45
|
|
|
$message = "Target [$modelName] is not an Illuminate\Database\Eloquent\Model instance."; |
46
|
|
|
|
47
|
|
|
throw new InvalidModelException($message); |
48
|
|
|
} |
49
|
4 |
|
|
50
|
|
|
return $modelInstance; |
51
|
4 |
|
} |
52
|
|
|
|
53
|
4 |
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
4 |
|
*/ |
56
|
2 |
|
public function getRepository($modelName) |
57
|
|
|
{ |
58
|
2 |
|
$modelInstance = $this->getModelInstance($modelName); |
59
|
1 |
|
|
60
|
|
|
if ($modelInstance instanceof HasCustomRepository) { |
61
|
1 |
|
$repositoryClass = $modelInstance->repository(); |
62
|
|
|
$repository = new $repositoryClass($modelInstance); |
63
|
1 |
|
|
64
|
2 |
|
if (!$repository instanceof Repository) { |
65
|
|
|
$message = "The [$modelName] custom repository must extend Ablunier\Laravel\Database\Repository\Eloquent\Repository."; |
66
|
|
|
|
67
|
3 |
|
throw new InvalidRepositoryException($message); |
68
|
1 |
|
} |
69
|
1 |
|
} else { |
70
|
1 |
|
$repository = new Repository($modelInstance); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
if ($modelInstance instanceof HasCache && $modelInstance->cache() === true) { |
74
|
2 |
|
return new Cache($repository, $this->app['cache.store']); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $repository; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
1 |
|
*/ |
83
|
|
|
public function getAbstractionLayer($modelName) |
84
|
1 |
|
{ |
85
|
|
|
$modelInstance = $this->getModelInstance($modelName); |
86
|
1 |
|
|
87
|
|
|
$dbal = new AbstractionLayer($modelInstance); |
88
|
1 |
|
|
89
|
|
|
return $dbal; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.